|
| 1 | +-- SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) |
| 3 | +-- |
| 4 | +-- FFIOwnership.idr - V12: FFI pointer validity + ownership discipline. |
| 5 | +-- |
| 6 | +-- Scope (spec V12): |
| 7 | +-- (1) Non-null before dereference. |
| 8 | +-- (2) Ownership state is explicit in the type. |
| 9 | +-- (3) Double-free is blocked by type shape (`free` only accepts Alive). |
| 10 | + |
| 11 | +module FFIOwnership |
| 12 | + |
| 13 | +%default total |
| 14 | + |
| 15 | +------------------------------------------------------------------------ |
| 16 | +-- Ownership state |
| 17 | +------------------------------------------------------------------------ |
| 18 | + |
| 19 | +public export |
| 20 | +data PtrState : Type where |
| 21 | + Alive : PtrState |
| 22 | + Freed : PtrState |
| 23 | + |
| 24 | +------------------------------------------------------------------------ |
| 25 | +-- Raw pointer model + non-null witness |
| 26 | +------------------------------------------------------------------------ |
| 27 | + |
| 28 | +public export |
| 29 | +record RawPtr where |
| 30 | + constructor MkRawPtr |
| 31 | + addr : Nat |
| 32 | + |
| 33 | +public export |
| 34 | +data NonNull : Nat -> Type where |
| 35 | + IsNonNull : (k : Nat) -> NonNull (S k) |
| 36 | + |
| 37 | +------------------------------------------------------------------------ |
| 38 | +-- Owned pointer token |
| 39 | +------------------------------------------------------------------------ |
| 40 | + |
| 41 | +||| Ownership token indexed by pointer state. |
| 42 | +||| |
| 43 | +||| - `Owned Alive` carries a non-null proof. |
| 44 | +||| - `Owned Freed` is a tombstone token that cannot be dereferenced or freed. |
| 45 | +public export |
| 46 | +record Owned (st : PtrState) where |
| 47 | + constructor MkOwned |
| 48 | + raw : RawPtr |
| 49 | + nonNull : case st of |
| 50 | + Alive => NonNull raw.addr |
| 51 | + Freed => () |
| 52 | + |
| 53 | +------------------------------------------------------------------------ |
| 54 | +-- Core API |
| 55 | +------------------------------------------------------------------------ |
| 56 | + |
| 57 | +||| Allocate an owned pointer token from a non-zero address witness. |
| 58 | +public export |
| 59 | +alloc : (addr : Nat) -> NonNull addr -> Owned Alive |
| 60 | +alloc addr nn = MkOwned (MkRawPtr addr) nn |
| 61 | + |
| 62 | +||| Free consumes an `Alive` token and returns a `Freed` tombstone token. |
| 63 | +||| No function in this module turns `Freed` back into `Alive`. |
| 64 | +public export |
| 65 | +free : Owned Alive -> Owned Freed |
| 66 | +free (MkOwned rp _) = MkOwned rp () |
| 67 | + |
| 68 | +||| Dereference is only available for `Alive` tokens. |
| 69 | +public export |
| 70 | +derefAddr : Owned Alive -> Nat |
| 71 | +derefAddr (MkOwned rp _) = rp.addr |
| 72 | + |
| 73 | +------------------------------------------------------------------------ |
| 74 | +-- Proof obligations |
| 75 | +------------------------------------------------------------------------ |
| 76 | + |
| 77 | +||| Non-null invariant for dereference: every dereference target is provably non-zero. |
| 78 | +public export |
| 79 | +derefNonNull : (o : Owned Alive) -> NonNull (derefAddr o) |
| 80 | +derefNonNull (MkOwned _ nn) = nn |
| 81 | + |
| 82 | +||| Capability witness: only `Alive` pointers are freeable. |
| 83 | +public export |
| 84 | +data CanFree : PtrState -> Type where |
| 85 | + FreeAlive : CanFree Alive |
| 86 | + |
| 87 | +||| There is no capability to free a `Freed` token. |
| 88 | +public export |
| 89 | +noCanFreeFreed : CanFree Freed -> Void |
| 90 | +noCanFreeFreed FreeAlive impossible |
| 91 | + |
| 92 | +||| Freeing requires the `Alive` capability at the type level. |
| 93 | +public export |
| 94 | +freeWithCap : (st : PtrState) -> CanFree st -> Owned st -> Owned Freed |
| 95 | +freeWithCap Alive FreeAlive o = free o |
| 96 | + |
| 97 | +||| "Double free is impossible" witness: a second free would need `CanFree Freed`, |
| 98 | +||| but that type is uninhabited. |
| 99 | +public export |
| 100 | +doubleFreeImpossible : (o : Owned Freed) -> CanFree Freed -> Void |
| 101 | +doubleFreeImpossible _ cap = noCanFreeFreed cap |
| 102 | + |
| 103 | +------------------------------------------------------------------------ |
| 104 | +-- Sanity checks |
| 105 | +------------------------------------------------------------------------ |
| 106 | + |
| 107 | +public export |
| 108 | +allocOneNonNull : derefNonNull (alloc 1 (IsNonNull 0)) = IsNonNull 0 |
| 109 | +allocOneNonNull = Refl |
| 110 | + |
| 111 | +public export |
| 112 | +freePreservesAddress : (o : Owned Alive) -> (free o).raw.addr = o.raw.addr |
| 113 | +freePreservesAddress (MkOwned _ _) = Refl |
0 commit comments