blob: c24edbd825265d6f99166eccafbd1d7a3d676d68 [file]
-- Copyright 2026 The Fuchsia Authors
--
-- Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
-- <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
-- license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
-- This file may not be copied, modified, or distributed except according to
-- those terms.
prelude
import Init.NotationExtra
import Trusted.MirAst
import Trusted.MirModel
import Trusted.MirSemantics.Memory
/-!
# Negative Refutations for `Trusted.MirSemantics.Memory` UB Guards
Exercises every UB guard in `Trusted/MirSemantics/Memory.lean`:
- `deallocateStack`: non-zero offset, missing allocation, use-after-free,
immutable allocation.
- `readBytes`: missing allocation, use-after-free, out-of-bounds, misaligned.
- `writeBytes`: missing allocation, use-after-free, out-of-bounds, misaligned,
write to read-only allocation.
- `isAligned`: allocation alignment lower than access alignment, and access
offset not divisible by access alignment.
Profile version 1 MIR instructions cannot yet construct arbitrary pointer
offsets or immutable allocations, so these refutations exercise the memory
operations directly. Importing only `Trusted.MirSemantics.Memory` also keeps
each `Memory.lean` mutant check under a quarter of a second.
-/
namespace GuardMutation
open Trusted.MirAst Trusted.MirSemantics
/-- Live mutable 4-byte, 4-byte-aligned (`alignLog2 = 2`) allocation with id
`1`. -/
noncomputable def liveMut4Alloc : Allocation := {
allocId := 1
alignLog2 := 2
mutability := .mut
bytes := [.init 1 none, .init 2 none, .init 3 none, .init 4 none]
live := true
}
/-- Live mutable 8-byte, 4-byte-aligned (`alignLog2 = 2`) allocation with id
`1`, used for offset-misalignment checks that stay in bounds. -/
noncomputable def liveMut8Alloc : Allocation := {
allocId := 1
alignLog2 := 2
mutability := .mut
bytes := List.replicate 8 (.init 0 none)
live := true
}
/-- Live mutable 4-byte, 1-byte-aligned (`alignLog2 = 0`) allocation with id
`1`, used for allocation-alignment checks at offset `0`. -/
noncomputable def liveMut4Align1Alloc : Allocation := {
allocId := 1
alignLog2 := 0
mutability := .mut
bytes := List.replicate 4 (.init 0 none)
live := true
}
/-- Already deallocated (`live = false`) 4-byte allocation with id `1`. -/
noncomputable def deadMut4Alloc : Allocation :=
{ liveMut4Alloc with live := false }
/-- Live immutable (`mutability = .shared`) 4-byte allocation with id `1`. -/
noncomputable def shared4Alloc : Allocation :=
{ liveMut4Alloc with mutability := .shared }
/-- Helper constructing a `MemoryState` containing a single allocation. -/
noncomputable def singleAllocMem (a : Allocation) : MemoryState :=
{ nextAllocId := 2, allocations := [a] }
/-- Four initialized bytes for `writeBytes` refutations. -/
noncomputable def fourBytes : List AllocByte :=
[.init 9 none, .init 8 none, .init 7 none, .init 6 none]
/-- Guard `mem_dealloc_nonzero_offset`: deallocating at non-zero offset `1`
faults with `.undefined`. -/
theorem refute_mem_dealloc_nonzero_offset :
deallocateStack (singleAllocMem liveMut4Alloc) ⟨1, 1⟩ =
.error .undefined :=
rfl
/-- Guard `mem_dealloc_missing_alloc`: deallocating a nonexistent allocation id
faults with `.undefined`. -/
theorem refute_mem_dealloc_missing_alloc :
deallocateStack (singleAllocMem liveMut4Alloc) ⟨99, 0⟩ =
.error .undefined :=
rfl
/-- Guard `mem_dealloc_use_after_free`: double deallocation of an already dead
allocation faults with `.undefined`. -/
theorem refute_mem_dealloc_use_after_free :
deallocateStack (singleAllocMem deadMut4Alloc) ⟨1, 0⟩ =
.error .undefined :=
rfl
/-- Guard `mem_dealloc_immutable`: deallocating a `.shared` allocation faults
with `.undefined`. -/
theorem refute_mem_dealloc_immutable :
deallocateStack (singleAllocMem shared4Alloc) ⟨1, 0⟩ =
.error .undefined :=
rfl
/-- Guard `mem_read_missing_alloc`: reading from a nonexistent allocation id
faults with `.undefined`. -/
theorem refute_mem_read_missing_alloc :
readBytes (singleAllocMem liveMut4Alloc) ⟨99, 0⟩ 4 2 =
.error .undefined :=
rfl
/-- Guard `mem_read_use_after_free`: reading from a deallocated allocation
faults with `.undefined`. -/
theorem refute_mem_read_use_after_free :
readBytes (singleAllocMem deadMut4Alloc) ⟨1, 0⟩ 4 2 =
.error .undefined :=
rfl
/-- Guard `mem_read_out_of_bounds`: reading 4 bytes at offset 4 of a 4-byte
allocation (aligned, live, but out of bounds) faults with `.undefined`. -/
theorem refute_mem_read_out_of_bounds :
readBytes (singleAllocMem liveMut4Alloc) ⟨1, 4⟩ 4 2 =
.error .undefined :=
rfl
/-- Guard `mem_read_misaligned`: reading 4 bytes with `alignLog2 = 2` at
offset `1` of a live 8-byte allocation faults with `.undefined`. -/
theorem refute_mem_read_misaligned :
readBytes (singleAllocMem liveMut8Alloc) ⟨1, 1⟩ 4 2 =
.error .undefined :=
rfl
/-- Guard `mem_write_missing_alloc`: writing to a nonexistent allocation id
faults with `.undefined`. -/
theorem refute_mem_write_missing_alloc :
writeBytes (singleAllocMem liveMut4Alloc) ⟨99, 0⟩ 2 fourBytes =
.error .undefined :=
rfl
/-- Guard `mem_write_use_after_free`: writing to a deallocated allocation
faults with `.undefined`. -/
theorem refute_mem_write_use_after_free :
writeBytes (singleAllocMem deadMut4Alloc) ⟨1, 0⟩ 2 fourBytes =
.error .undefined :=
rfl
/-- Guard `mem_write_out_of_bounds`: writing 4 bytes at offset 4 of a 4-byte
allocation (aligned, live, mutable, but out of bounds) faults with
`.undefined`. -/
theorem refute_mem_write_out_of_bounds :
writeBytes (singleAllocMem liveMut4Alloc) ⟨1, 4⟩ 2 fourBytes =
.error .undefined :=
rfl
/-- Guard `mem_write_misaligned`: writing 4 bytes with `alignLog2 = 2` at
offset `1` of a live mutable 8-byte allocation faults with `.undefined`. -/
theorem refute_mem_write_misaligned :
writeBytes (singleAllocMem liveMut8Alloc) ⟨1, 1⟩ 2 fourBytes =
.error .undefined :=
rfl
/-- Guard `mem_write_readonly`: writing to a live, in-bounds, aligned `.shared`
allocation faults with `.undefined`. -/
theorem refute_mem_write_readonly :
writeBytes (singleAllocMem shared4Alloc) ⟨1, 0⟩ 2 fourBytes =
.error .undefined :=
rfl
/-- Guard `mem_align_alloc_align_check`: reading with `alignLog2 = 2` at
offset `0` from an allocation whose own alignment is `alignLog2 = 0` faults
with `.undefined`. -/
theorem refute_mem_align_alloc_align_check :
readBytes (singleAllocMem liveMut4Align1Alloc) ⟨1, 0⟩ 4 2 =
.error .undefined :=
rfl
/-- Guard `mem_align_offset_mod_check`: reading with `alignLog2 = 2` at
offset `1` from an `alignLog2 = 2` allocation faults with `.undefined`. -/
theorem refute_mem_align_offset_mod_check :
readBytes (singleAllocMem liveMut8Alloc) ⟨1, 1⟩ 4 2 =
.error .undefined :=
rfl
/-- Live mutable 4-byte allocation whose bytes are all `.uninit`. -/
noncomputable def uninitMut4Alloc : Allocation := {
allocId := 1
alignLog2 := 0
mutability := .mut
bytes := List.replicate 4 .uninit
live := true
}
/-- Guard `mem_v2_ptr_add_null_nonzero`: offsetting a provenance-free (null)
pointer by a nonzero byte delta faults with `.undefined`. -/
theorem refute_mem_v2_ptr_add_null_nonzero :
ptrAddInMem MemoryState.empty 1 ⟨none, 0, .isize⟩ 1 = .error .undefined :=
rfl
/-- Guard `mem_v2_ptr_add_oob`: offsetting a live pointer past the end of its
allocation faults with `.undefined`. -/
theorem refute_mem_v2_ptr_add_oob :
ptrAddInMem (singleAllocMem liveMut4Alloc) 1 ⟨some 1, 2, .isize⟩ 3 =
.error .undefined :=
rfl
/-- Guard `mem_v2_slice_null`: constructing even a zero-length slice from a
null (`⟨none, 0, .isize⟩`) pointer faults with `.undefined` in both
`sliceFromRawPartsCheck` and `sliceRefRawCheck`. -/
theorem refute_mem_v2_slice_null :
sliceFromRawPartsCheck MemoryState.empty false 1 0 ⟨none, 0, .isize⟩ 0 =
.error .undefined ∧
sliceRefRawCheck MemoryState.empty false 0 0 ⟨none, 0, .isize⟩ 0 =
.error .undefined :=
⟨rfl, rfl⟩
/-- Guard `mem_v2_slice_readonly`: constructing a non-empty mutable slice
(`&mut [u8]` or non-integer `&mut [T]`) over a `.shared` allocation faults with
`.undefined`. -/
theorem refute_mem_v2_slice_readonly :
sliceFromRawPartsCheck (singleAllocMem shared4Alloc) true 1 0 ⟨some 1, 0, .isize⟩ 4 =
.error .undefined ∧
sliceRefRawCheck (singleAllocMem shared4Alloc) true 1 0 ⟨some 1, 0, .isize⟩ 1 =
.error .undefined :=
⟨rfl, rfl⟩
/-- Guard `mem_v2_slice_readonly_zero_len`: constructing a zero-byte mutable
slice (`len = 0` `&mut [u8]` or ZST element `&mut [()]`) over a `.shared`
`.rodata` allocation succeeds (`totalBytes == 0`). -/
theorem refute_mem_v2_slice_readonly_zero_len :
sliceFromRawPartsCheck (singleAllocMem shared4Alloc) true 1 0 ⟨some 1, 0, .isize⟩ 0 =
.ok (.slicePtr ⟨some 1, 0, .isize⟩ 0) ∧
sliceRefRawCheck (singleAllocMem shared4Alloc) true 0 0 ⟨some 1, 0, .isize⟩ 4 =
.ok (some shared4Alloc) :=
⟨rfl, rfl⟩
/-- Guard `mem_v2_slice_uninit`: constructing a byte slice over uninitialized
bytes faults with `.undefined`. -/
theorem refute_mem_v2_slice_uninit :
sliceFromRawPartsCheck (singleAllocMem uninitMut4Alloc) false 1 0
⟨some 1, 0, .isize⟩ 4 = .error .undefined :=
rfl
/-- Guard `mem_v2_slice_get_unchecked_oob`: subslicing with `endOff > len`
faults with `.undefined` even when the underlying allocation has extra bytes. -/
theorem refute_mem_v2_slice_get_unchecked_oob :
sliceGetUncheckedCheck (singleAllocMem liveMut8Alloc) ⟨some 1, 0, .isize⟩ 4 1 6 =
.error .undefined :=
rfl
/-- Guard `mem_read_zst_misaligned`: zero-size `readBytes` at a misaligned
offset (`alignLog2 = 2`, `offset = 1`) faults with `.undefined`. -/
theorem refute_mem_read_zst_misaligned :
readBytes (singleAllocMem liveMut8Alloc) ⟨1, 1⟩ 0 2 =
.error .undefined :=
rfl
/-- Guard `mem_write_zst_misaligned`: zero-size `writeBytes` at a misaligned
offset (`alignLog2 = 2`, `offset = 1`) faults with `.undefined`. -/
theorem refute_mem_write_zst_misaligned :
writeBytes (singleAllocMem liveMut8Alloc) ⟨1, 1⟩ 2 [] =
.error .undefined :=
rfl
/-- Zero-size `readBytes` at offset `0` from an allocation whose own alignment
(`alignLog2 = 0`) is below the requested `alignLog2 = 2` faults with
`.undefined`. -/
theorem refute_mem_read_zst_alloc_align :
readBytes (singleAllocMem liveMut4Align1Alloc) ⟨1, 0⟩ 0 2 =
.error .undefined :=
rfl
/-- Zero-size `readBytes` on a deallocated (`live = false`) allocation still
checks alignment and faults with `.undefined` when misaligned. -/
theorem refute_mem_read_zst_dead_misaligned :
readBytes (singleAllocMem deadMut4Alloc) ⟨1, 1⟩ 0 2 =
.error .undefined :=
rfl
/-- Zero-size `writeUninit` at a misaligned offset faults with `.undefined`. -/
theorem refute_mem_uninit_zst_misaligned :
writeUninit (singleAllocMem liveMut8Alloc) ⟨1, 1⟩ 0 2 =
.error .undefined :=
rfl
/-- Zero-size `readBytes` / `writeBytes` against a nonexistent allocation id
faults with `.undefined`. -/
theorem refute_mem_zst_missing_alloc :
readBytes (singleAllocMem liveMut4Alloc) ⟨99, 0⟩ 0 0 =
.error .undefined ∧
writeBytes (singleAllocMem liveMut4Alloc) ⟨99, 0⟩ 0 [] =
.error .undefined :=
⟨rfl, rfl⟩
/-- Anti-overreach positive canaries matching `rustc` `get_place_alloc` /
`get_place_alloc_mut` and `lean-miri` (`checkAndDerefPtr` size-0 skip +
alignment-only check): aligned `size == 0` accesses succeed even when the
allocation is deallocated (`live = false`), read-only (`.shared`), or the
offset is out of bounds. -/
theorem positive_zst_alignment_only_canaries :
readBytes (singleAllocMem deadMut4Alloc) ⟨1, 0⟩ 0 2 = .ok [] ∧
writeBytes (singleAllocMem shared4Alloc) ⟨1, 0⟩ 2 [] =
.ok (singleAllocMem shared4Alloc) ∧
readBytes (singleAllocMem liveMut4Alloc) ⟨1, 8⟩ 0 2 = .ok [] ∧
writeBytes (singleAllocMem liveMut4Alloc) ⟨1, 8⟩ 2 [] =
.ok (singleAllocMem liveMut4Alloc) :=
⟨rfl, rfl, rfl, rfl⟩
/-- Guard `mem_v2_entry_byte_prov_fresh`: `entryMemoryWithCaller` advances
`nextAllocId` past provenance `AllocId`s stored inside caller allocation bytes
(`some 5` inside `allocId = 2`), so callee stack allocation gets `allocId = 6`
and never aliases `some 5`. -/
theorem refute_mem_v2_entry_byte_prov_fresh :
let cm : CallerMemory := {
allocations := [{
allocId := 2
alignLog2 := 0
mutability := .mut
bytes := [.init 0 (some 5)]
live := true
}]
}
(allocateStack (entryMemoryWithCaller cm) ⟨1, 0⟩).2.allocId = 6 :=
rfl
/-- Guard `mem_v2_entry_raw_ptr_arg_fresh`: `entryMemoryForArgsV2` advances
`nextAllocId` past provenance `AllocId`s carried by raw-pointer arguments (even
dangling `some 5` absent from `cm.allocations`), while reducing to
`entryMemoryWithCaller` when no parameter has a raw-pointer type. -/
theorem refute_mem_v2_entry_raw_ptr_arg_fresh :
let emptyCm : CallerMemory := { allocations := [] }
(allocateStack
(entryMemoryForArgsV2 emptyCm
[.rawPtr .shared (.int .i8 false)]
[.ptr ⟨some 5, 0, .isize⟩]) ⟨1, 0⟩).2.allocId = 6 ∧
(entryMemoryForArgsV2 emptyCm [.int .i32 false]
[.scalar ⟨.i32, false, 0⟩]).nextAllocId = 2 :=
⟨rfl, rfl⟩
/-- Guard `mem_v2_entry_option_raw_ptr_ty`: `tyHasRawPtrV2` recurses through
`Option`, so an `Option<*const i8>` parameter (bare or inside a tuple) makes
`entryMemoryForArgsV2` advance `nextAllocId` past the dangling `some 5` it
carries, and the first stack allocation gets `AllocId` `6`. -/
theorem refute_mem_v2_entry_option_raw_ptr_ty :
let emptyCm : CallerMemory := { allocations := [] }
(allocateStack
(entryMemoryForArgsV2 emptyCm
[.option (.rawPtr .shared (.int .i8 false))]
[.optionVal (some (.ptr ⟨some 5, 0, .isize⟩))]) ⟨1, 0⟩).2.allocId = 6 ∧
(allocateStack
(entryMemoryForArgsV2 emptyCm
[.tuple [.int .i8 false, .option (.rawPtr .shared (.int .i8 false))]]
[.tuple [.scalar ⟨.i8, false, 0⟩,
.optionVal (some (.ptr ⟨some 5, 0, .isize⟩))]])
⟨1, 0⟩).2.allocId = 6 :=
⟨rfl, rfl⟩
/-- Guard `mem_v2_entry_option_raw_ptr_val`: `maxValPtrAllocId` looks inside
`Some` payloads, so a dangling `some 5` pointer inside an `Option` argument
is counted, while `None` contributes nothing. -/
theorem refute_mem_v2_entry_option_raw_ptr_val :
maxValPtrAllocId (.optionVal (some (.ptr ⟨some 5, 0, .isize⟩))) = 5 ∧
maxValPtrAllocId (.tuple [.scalar ⟨.i8, false, 0⟩,
.optionVal (some (.ptr ⟨some 5, 0, .isize⟩))]) = 5 ∧
maxValPtrAllocId (.optionVal none) = 0 :=
⟨rfl, rfl, rfl⟩
/-- Guard `mem_v2_adt_raw_ptr_ty`: `tyHasRawPtrV2` looks inside the variants
of a payload enum, so one whose variant holds a `*const u8` counts as holding
a raw pointer, and one whose variant holds only a `u8` does not. -/
theorem refute_mem_v2_adt_raw_ptr_ty :
tyHasRawPtrV2 (.adtEnum ⟨8, 8, 3, .isize, true, [0], 0, .i8, false, 0, 0,
.single⟩ [.struct_ ⟨81, 8, 3, [0]⟩ [.rawPtr .shared (.int .i8 false)]]) =
true ∧
tyHasRawPtrV2 (.adtEnum ⟨9, 1, 0, .isize, true, [0], 0, .i8, false, 0, 0,
.single⟩ [.struct_ ⟨91, 1, 0, [0]⟩ [.int .i8 false]]) = false :=
⟨rfl, rfl⟩
/-- Guard `mem_v2_entry_adt_raw_ptr_val`: `maxValPtrAllocId` looks inside the
payload of a payload enum value, so a dangling `some 7` pointer held by a
variant is counted. -/
theorem refute_mem_v2_entry_adt_raw_ptr_val :
maxValPtrAllocId (.adtVal 0 (.tuple [.ptr ⟨some 7, 0, .isize⟩])) = 7 ∧
maxValPtrAllocId (.tuple [.scalar ⟨.i8, false, 0⟩,
.adtVal 1 (.tuple [.ptr ⟨some 7, 0, .isize⟩])]) = 7 ∧
maxValPtrAllocId (.adtVal 1 (.tuple [])) = 0 :=
⟨rfl, rfl, rfl⟩
/-- Guard `mem_v2_slice_get_unchecked_elem_size`: `sliceGetUncheckedCheck`
scales `startOff` by `elemSize` in `ptrAddInMem` (`2 * 4 = 8 > 4` on a 4-byte
allocation faults `.error .undefined`, whereas `elemSize = 4` with
`startOff = 1` on a 4-byte allocation succeeds at byte offset `4`). -/
theorem refute_mem_v2_slice_get_unchecked_elem_size :
sliceGetUncheckedCheck (singleAllocMem liveMut4Alloc)
⟨some 1, 0, .isize⟩ 2 2 2 4 = .error .undefined ∧
sliceGetUncheckedCheck (singleAllocMem liveMut4Alloc)
⟨some 1, 0, .isize⟩ 1 1 1 4 =
.ok (.slicePtr ⟨some 1, 4, .isize⟩ 0) :=
⟨rfl, rfl⟩
/-- Guard `mem_reserve_alloc_id_monotonic`: two consecutive `allocateStack`
calls must receive distinct allocation ids so deallocating the first never
kills the second. -/
theorem refute_mem_reserve_alloc_id_monotonic :
(let (m1, p1) := allocateStack MemoryState.empty ⟨4, 2⟩
let (_, p2) := allocateStack m1 ⟨4, 2⟩
Nat.beq p1.allocId p2.allocId = false) :=
rfl
/-- Guard `mem_alloc_initializes_uninit`: freshly allocated stack bytes start
`.uninit` rather than zero-initialized. -/
theorem refute_mem_alloc_initializes_uninit :
(let (m1, p1) := allocateStack MemoryState.empty ⟨4, 2⟩
readBytes m1 p1 4 2 = .ok [.uninit, .uninit, .uninit, .uninit]) :=
rfl
/-- Guard `mem_dealloc_marks_dead`: `deallocateStack` marks the freed
allocation `live := false` so a subsequent `readBytes` faults with
`.undefined`. -/
theorem refute_mem_dealloc_marks_dead :
(match deallocateStack (singleAllocMem liveMut4Alloc) ⟨1, 0⟩ with
| .error _ => False
| .ok m1 => readBytes m1 ⟨1, 0⟩ 4 2 = .error .undefined) :=
rfl
/-- Guard `mem_in_bounds_includes_size`: a partial-overlap read starting
in-bounds at offset `2` with `size = 4` in a 4-byte allocation faults with
`.undefined`. -/
theorem refute_mem_in_bounds_includes_size :
readBytes (singleAllocMem liveMut4Alloc) ⟨1, 2⟩ 4 1 =
.error .undefined :=
rfl
/-- Guard `mem_write_uninit_marks_uninit`: `writeUninit` overwrites bytes with
`.uninit` rather than zero bytes. -/
theorem refute_mem_write_uninit_marks_uninit :
(match writeUninit (singleAllocMem liveMut4Alloc) ⟨1, 0⟩ 4 2 with
| .error _ => False
| .ok m1 => readBytes m1 ⟨1, 0⟩ 4 2 =
.ok [.uninit, .uninit, .uninit, .uninit]) :=
rfl
end GuardMutation