|
| 1 | +-- SPDX-License-Identifier: MPL-2.0 |
| 2 | +-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +-- |
| 4 | +||| Memory-layout proofs for the TypedQLiser ABI. |
| 5 | +||| |
| 6 | +||| The proof certificate descriptor crosses the Zig/C FFI boundary; this module |
| 7 | +||| pins its field layout and proves it is C-ABI aligned. |
| 8 | + |
| 9 | +module Typedqliser.ABI.Layout |
| 10 | + |
| 11 | +import Typedqliser.ABI.Types |
| 12 | +import Data.Vect |
| 13 | +import Data.So |
| 14 | +import Data.Nat |
| 15 | +import Decidable.Equality |
| 16 | + |
| 17 | +%default total |
| 18 | + |
| 19 | +public export |
| 20 | +paddingFor : (offset : Nat) -> (alignment : Nat) -> Nat |
| 21 | +paddingFor offset alignment = |
| 22 | + if offset `mod` alignment == 0 |
| 23 | + then 0 |
| 24 | + else minus alignment (offset `mod` alignment) |
| 25 | + |
| 26 | +public export |
| 27 | +alignUp : (size : Nat) -> (alignment : Nat) -> Nat |
| 28 | +alignUp size alignment = size + paddingFor size alignment |
| 29 | + |
| 30 | +||| `m = k * n` — n divides m. |
| 31 | +public export |
| 32 | +data Divides : Nat -> Nat -> Type where |
| 33 | + DivideBy : (k : Nat) -> {n : Nat} -> {m : Nat} -> (m = k * n) -> Divides n m |
| 34 | + |
| 35 | +||| Sound divisibility decision: returns a real witness when n divides m. |
| 36 | +public export |
| 37 | +decDivides : (n : Nat) -> (m : Nat) -> Maybe (Divides n m) |
| 38 | +decDivides Z _ = Nothing |
| 39 | +decDivides (S k) m = |
| 40 | + let q = m `div` (S k) in |
| 41 | + case decEq m (q * (S k)) of |
| 42 | + Yes prf => Just (DivideBy q prf) |
| 43 | + No _ => Nothing |
| 44 | + |
| 45 | +public export |
| 46 | +record Field where |
| 47 | + constructor MkField |
| 48 | + name : String |
| 49 | + offset : Nat |
| 50 | + size : Nat |
| 51 | + alignment : Nat |
| 52 | + |
| 53 | +public export |
| 54 | +record StructLayout where |
| 55 | + constructor MkStructLayout |
| 56 | + fields : Vect k Field |
| 57 | + totalSize : Nat |
| 58 | + alignment : Nat |
| 59 | + {auto 0 sizeCorrect : So (totalSize >= sum (map (\f => f.size) fields))} |
| 60 | + {auto 0 aligned : Divides alignment totalSize} |
| 61 | + |
| 62 | +||| The proof-certificate descriptor as it crosses the FFI (40 bytes, 8-aligned). |
| 63 | +public export |
| 64 | +certificateDescLayout : StructLayout |
| 65 | +certificateDescLayout = |
| 66 | + MkStructLayout |
| 67 | + [ MkField "query_hash_ptr" 0 8 8 |
| 68 | + , MkField "schema_ver_ptr" 8 8 8 |
| 69 | + , MkField "max_level" 16 4 4 |
| 70 | + , MkField "num_proofs" 20 4 4 |
| 71 | + , MkField "proofs_ptr" 24 8 8 |
| 72 | + , MkField "result_code" 32 4 4 |
| 73 | + , MkField "padding" 36 4 4 |
| 74 | + ] |
| 75 | + 40 |
| 76 | + 8 |
| 77 | + {sizeCorrect = Oh} |
| 78 | + {aligned = DivideBy 5 Refl} |
| 79 | + |
| 80 | +||| Every field offset in a layout is correctly aligned. |
| 81 | +public export |
| 82 | +data FieldsAligned : Vect len Field -> Type where |
| 83 | + NoFields : FieldsAligned [] |
| 84 | + ConsField : |
| 85 | + (f : Field) -> |
| 86 | + (rest : Vect len Field) -> |
| 87 | + Divides f.alignment f.offset -> |
| 88 | + FieldsAligned rest -> |
| 89 | + FieldsAligned (f :: rest) |
| 90 | + |
| 91 | +||| Decide field alignment, building a real witness from per-field divisibility. |
| 92 | +public export |
| 93 | +decFieldsAligned : (fs : Vect len Field) -> Maybe (FieldsAligned fs) |
| 94 | +decFieldsAligned [] = Just NoFields |
| 95 | +decFieldsAligned (f :: fs) = |
| 96 | + case decDivides f.alignment f.offset of |
| 97 | + Nothing => Nothing |
| 98 | + Just dvd => case decFieldsAligned fs of |
| 99 | + Nothing => Nothing |
| 100 | + Just rest => Just (ConsField f fs dvd rest) |
| 101 | + |
| 102 | +public export |
| 103 | +data CABICompliant : StructLayout -> Type where |
| 104 | + CABIOk : (layout : StructLayout) -> FieldsAligned layout.fields -> CABICompliant layout |
| 105 | + |
| 106 | +||| Verify a layout against the C-ABI alignment rules; real proof or error. |
| 107 | +public export |
| 108 | +checkCABI : (layout : StructLayout) -> Either String (CABICompliant layout) |
| 109 | +checkCABI layout = |
| 110 | + case decFieldsAligned layout.fields of |
| 111 | + Just prf => Right (CABIOk layout prf) |
| 112 | + Nothing => Left "Field offsets are not correctly aligned for the C ABI" |
| 113 | + |
| 114 | +||| Decide whether a field lies within a struct's byte bounds (honest Maybe; |
| 115 | +||| the property is false for an arbitrary field, so it is decided not asserted). |
| 116 | +public export |
| 117 | +offsetInBounds : (layout : StructLayout) -> (f : Field) -> |
| 118 | + Maybe (So (f.offset + f.size <= layout.totalSize)) |
| 119 | +offsetInBounds layout f = |
| 120 | + case choose (f.offset + f.size <= layout.totalSize) of |
| 121 | + Left ok => Just ok |
| 122 | + Right _ => Nothing |
0 commit comments