blob: 972b2c3ac784c57c79abd94f400cf9189ea7a787 [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.Bytes
/-!
# Negative Refutations for `Trusted.MirSemantics.Layout` Guards
Exercises every alignment and admission guard in
`Trusted/MirSemantics/Layout.lean`:
- `layoutOfTy`: unadmitted fieldless enums and unsupported types return `none`
and fail closed with `.unsupported` in `loadIndirect`.
- `intLayout`, `layoutOfTy .char`, and `overflowPairLayout`: natural alignment
requirements cause misaligned indirect loads at byte offset `1` to fault with
`.undefined`.
-/
namespace GuardMutation
open Trusted.MirAst Trusted.MirSemantics
/-- Live mutable 32-byte, 16-byte-aligned (`alignLog2 = 4`) zero-initialized
memory containing allocation id `1`, so offset `1` stays strictly in bounds for
every 16-byte or smaller type while violating any alignment > 1. -/
noncomputable def layoutRefuteMem : MemoryState :=
{ nextAllocId := 2
allocations := [{
allocId := 1
alignLog2 := 4
mutability := .mut
bytes := List.replicate 32 (.init 0 none)
live := true
}] }
/-- Unadmitted enum with duplicate discriminants `[10, 10]`. -/
noncomputable def layoutDupDiscrEnum : EnumTy := {
id := 99
discrWidth := .i8
discrSigned := false
tagRepr := .scalar .i8 false
discriminants := [10, 10]
}
/-- Guard `layout_unadmitted_enum_none`: an unadmitted enum has no layout and
fails closed with `.unsupported` on `loadIndirect`. -/
theorem refute_layout_unadmitted_enum_none :
layoutOfTy (.enum layoutDupDiscrEnum) = none ∧
loadIndirect layoutRefuteMem (.enum layoutDupDiscrEnum) ⟨1, 0⟩ =
.error .unsupported :=
⟨rfl, rfl⟩
/-- Guard `layout_unsupported_ty_none`: unmodeled types (`.ref`, non-overflow
tuples) have no layout and fail closed with `.unsupported`. -/
theorem refute_layout_unsupported_ty_none :
layoutOfTy (.ref .shared (.int .i32 false)) = none ∧
layoutOfTy (.tuple [.int .i32 false, .int .i32 false]) = none :=
⟨rfl, rfl⟩
/-- Guard `layout_int_i16_align`: misaligned `u16` load at offset `1` faults
with `.undefined`. -/
theorem refute_layout_int_i16_align :
loadIndirect layoutRefuteMem (.int .i16 false) ⟨1, 1⟩ =
.error .undefined :=
rfl
/-- Guard `layout_int_i32_align`: misaligned `u32` load at offset `1` faults
with `.undefined`. -/
theorem refute_layout_int_i32_align :
loadIndirect layoutRefuteMem (.int .i32 false) ⟨1, 1⟩ =
.error .undefined :=
rfl
/-- Guard `layout_int_i64_isize_align`: misaligned `u64` and `isize` loads at
offset `1` fault with `.undefined`. -/
theorem refute_layout_int_i64_isize_align :
loadIndirect layoutRefuteMem (.int .i64 false) ⟨1, 1⟩ =
.error .undefined ∧
loadIndirect layoutRefuteMem (.int .isize true) ⟨1, 1⟩ =
.error .undefined :=
⟨rfl, rfl⟩
/-- Guard `layout_int_i128_align`: misaligned `u128` load at offset `1` faults
with `.undefined`. -/
theorem refute_layout_int_i128_align :
loadIndirect layoutRefuteMem (.int .i128 false) ⟨1, 1⟩ =
.error .undefined :=
rfl
/-- Guard `layout_char_align`: misaligned `char` load at offset `1` faults with
`.undefined`. -/
theorem refute_layout_char_align :
loadIndirect layoutRefuteMem .char ⟨1, 1⟩ = .error .undefined :=
rfl
/-- Guard `layout_overflow_pair_align`: misaligned `(u32, bool)` load at offset
`1` faults with `.undefined`. -/
theorem refute_layout_overflow_pair_align :
loadIndirect layoutRefuteMem (.tuple [.int .i32 false, .bool]) ⟨1, 1⟩ =
.error .undefined :=
rfl
/-- Guard `layout_v2_struct_overlap`: a struct layout whose non-zero-sized
fields overlap has no Profile v2 layout. -/
theorem refute_layout_v2_struct_overlap :
layoutOfTyV2 (.struct_ ⟨1, 2, 0, [0, 0]⟩ [.int .i8 false, .int .i8 false]) =
none :=
rfl
/-- Guard `layout_v2_struct_misaligned`: a struct layout placing a `u16` field
at an odd offset has no Profile v2 layout. -/
theorem refute_layout_v2_struct_misaligned :
layoutOfTyV2 (.struct_ ⟨1, 4, 1, [1]⟩ [.int .i16 false]) = none :=
rfl
/-- Guard `layout_v2_rec_self_none`: an inline `.recSelf` (which
`recTyWfV2` rejects) has no layout. -/
theorem refute_layout_v2_rec_self_none :
layoutOfTyV2 (.recSelf 7) = none :=
rfl
/-- Guard `layout_v2_rec_self_field_none`: a struct with an inline `.recSelf`
field has no layout either. -/
theorem refute_layout_v2_rec_self_field_none :
layoutOfTyV2
(.struct_ { id := 7, size := 8, alignLog2 := 3, fieldOffsets := [0] }
[.recSelf 7]) = none :=
rfl
/-! ## Profile v2 `Option` struct fields and payload enum layouts -/
/-- `struct S { x: Option<u64> }`: 16 bytes, align 8. -/
noncomputable def layOptIntS : MirTy :=
.struct_ ⟨61, 16, 3, [0]⟩ [.option (.int .i64 false)]
/-- `struct T { a: Option<bool>, b: Option<()> }`: 2 bytes, align 1. -/
noncomputable def layOptBoolS : MirTy :=
.struct_ ⟨62, 2, 0, [0, 1]⟩ [.option .bool, .option .unit]
/-- `enum E { A(u32), B(u16), C(u32) }` with a direct `u16` tag at offset 0. -/
noncomputable def layETy : MirTy :=
.adtEnum ⟨1, 8, 2, .isize, true, [0, 1, 2], 0, .i16, false, 0, 2, .direct⟩
[.struct_ ⟨11, 8, 2, [4]⟩ [.int .i32 false],
.struct_ ⟨12, 8, 2, [2]⟩ [.int .i16 false],
.struct_ ⟨13, 8, 2, [4]⟩ [.int .i32 false]]
/-- `Result<u32, E>` with `rustc`'s layout: `Err` is untagged, and `Ok` is the
niche value `0xffff` of `E`'s tag, whose valid range becomes `0xffff..=2`. -/
noncomputable def layRTy : MirTy :=
.adtEnum ⟨2, 8, 2, .isize, true, [0, 1], 0, .i16, false, 65535, 2,
.niche 1 0 0 65535⟩
[.struct_ ⟨21, 8, 2, [4]⟩ [.int .i32 false],
.struct_ ⟨22, 8, 2, [0]⟩ [layETy]]
/-- Guards `layout_v2_option_int_field` and `layout_v2_option_bool_field`: a
struct field of type `Option<u64>`, `Option<bool>`, or `Option<()>` has the
`Option`'s own layout, so the struct has a layout and its values round-trip
through memory. -/
theorem refute_layout_v2_option_plain_field :
layoutOfTyV2 layOptIntS = some ⟨16, 3⟩ ∧
layoutOfTyV2 layOptBoolS = some ⟨2, 0⟩ ∧
((encodeValV2 profileV2TargetEndian layOptIntS
(.tuple [.optionVal (some (.scalar ⟨.i64, false, BitVec.ofNat 128 7⟩))])).bind
(decodeValV2 profileV2TargetEndian layOptIntS)) =
.ok (some (.tuple [.optionVal (some (.scalar ⟨.i64, false, BitVec.ofNat 128 7⟩))])) :=
⟨rfl, rfl, rfl⟩
/-- Guard `layout_adt_tag_overlap`: a tagged variant's field may not overlap
the tag (variant `B`'s `u16` moved onto the tag), while the untagged
variant's field holds the niche. -/
theorem refute_layout_adt_tag_overlap :
layoutOfTyV2 (.adtEnum ⟨1, 8, 2, .isize, true, [0, 1, 2], 0, .i16, false,
0, 2, .direct⟩
[.struct_ ⟨11, 8, 2, [4]⟩ [.int .i32 false],
.struct_ ⟨12, 8, 2, [0]⟩ [.int .i16 false],
.struct_ ⟨13, 8, 2, [4]⟩ [.int .i32 false]]) = none ∧
layoutOfTyV2 layRTy = some ⟨8, 2⟩ :=
⟨rfl, rfl⟩
/-- Guard `layout_adt_direct_roundtrip`: a discriminant that does not survive
truncation to the tag (`256` in a `u8` tag) is refused. -/
theorem refute_layout_adt_direct_roundtrip :
layoutOfTyV2 (.adtEnum ⟨1, 2, 1, .i16, false, [0, 256], 0, .i8, false,
0, 255, .direct⟩
[.struct_ ⟨11, 2, 1, []⟩ [], .struct_ ⟨12, 2, 1, []⟩ []]) = none :=
rfl
/-- Guard `layout_adt_niche_discriminants`: a niche enum's discriminants must
be its variant indices. -/
theorem refute_layout_adt_niche_discriminants :
layoutOfTyV2 (.adtEnum ⟨3, 2, 1, .isize, true, [0, 7], 0, .i16, false, 0,
5, .niche 1 0 0 5⟩
[.struct_ ⟨31, 2, 1, []⟩ [], .struct_ ⟨32, 2, 1, [0]⟩ [.int .i16 false]]) =
none :=
rfl
/-- Guard `layout_adt_tag_in_bounds`: the tag must lie inside the enum. -/
theorem refute_layout_adt_tag_in_bounds :
layoutOfTyV2 (.adtEnum ⟨1, 8, 2, .isize, true, [0, 1], 8, .i8, false,
0, 1, .direct⟩
[.struct_ ⟨11, 8, 2, []⟩ [], .struct_ ⟨12, 8, 2, []⟩ []]) = none :=
rfl
/-- Guard `layout_adt_single_other_uninhabited`: an untagged enum's other
variants must be uninhabited, so one whose `Ok` holds a `u8` has no
layout. -/
theorem refute_layout_adt_single_other_uninhabited :
layoutOfTyV2
(.adtEnum ⟨12, 2, 0, .isize, true, [0, 1], 0, .i8, false, 1, 1, .single⟩
[.struct_ ⟨121, 2, 0, [0]⟩ [.int .i8 false],
.struct_ ⟨122, 2, 0, [0]⟩
[.adtEnum ⟨11, 2, 0, .isize, true, [0, 1], 0, .i8, false, 0, 1,
.direct⟩
[.struct_ ⟨111, 2, 0, []⟩ [],
.struct_ ⟨112, 2, 0, [1]⟩ [.int .i8 false]]]]) = none :=
rfl
/-- Guard `layout_adt_single_head_index`: the inhabited variant an untagged
enum names must be one of its variants. -/
theorem refute_layout_adt_single_head_index :
layoutOfTyV2
(.adtEnum ⟨12, 0, 0, .isize, true, [0, 1], 0, .i8, false, 2, 2, .single⟩
[.struct_ ⟨121, 0, 0, [0]⟩ [.never],
.struct_ ⟨122, 0, 0, [0]⟩ [.never]]) = none :=
rfl
/-- Guard `layout_v2_struct_align_max`: a struct layout with `alignLog2 > 29`
(exceeding `rustc_abi::Align::MAX = 2^29`, including `alignLog2 = 64` which
would wrap `danglingAligned` to address `0`) has no Profile v2 layout, while
`alignLog2 = 29` remains admitted. -/
theorem refute_layout_v2_struct_align_max :
layoutOfTyV2 (.struct_ ⟨70, 0, 30, []⟩ []) = none ∧
layoutOfTyV2 (.struct_ ⟨70, Nat.pow 2 30, 30, []⟩ []) = none ∧
layoutOfTyV2 (.struct_ ⟨70, 0, 64, []⟩ []) = none ∧
layoutOfTyV2 (.struct_ ⟨70, 0, 29, []⟩ []) = some ⟨0, 29⟩ :=
⟨rfl, rfl, rfl, rfl⟩
/-- Guard `layout_adt_align_max`: a payload enum layout with `alignLog2 > 29`
(including `alignLog2 = 30` and `64`) has no Profile v2 layout, while
`alignLog2 = 29` remains admitted. -/
theorem refute_layout_adt_align_max :
layoutOfTyV2
(.adtEnum ⟨71, 0, 30, .isize, true, [0], 0, .i8, false, 0, 0, .single⟩
[.struct_ ⟨711, 0, 30, []⟩ []]) = none ∧
layoutOfTyV2
(.adtEnum ⟨71, 0, 64, .isize, true, [0], 0, .i8, false, 0, 0, .single⟩
[.struct_ ⟨711, 0, 64, []⟩ []]) = none ∧
layoutOfTyV2
(.adtEnum ⟨71, 0, 29, .isize, true, [0], 0, .i8, false, 0, 0, .single⟩
[.struct_ ⟨711, 0, 29, []⟩ []]) = some ⟨0, 29⟩ :=
⟨rfl, rfl, rfl⟩
end GuardMutation