Skip to content

Commit

Permalink
Merge branch 'main' into memcpy
Browse files Browse the repository at this point in the history
  • Loading branch information
shigoel authored Sep 25, 2024
2 parents 5f2f7ac + 26b5da0 commit 62c4719
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 250 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/copyright-header.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Check for copyright header

on: [pull_request]

jobs:
check-lean-files:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Verify .lean files start with a copyright header.
run: |
FILES=$(find . -type d \( -path "./.lake" \) -prune -o -type f -name "*.lean" -exec perl -ne 'BEGIN { $/ = undef; } print "$ARGV\n" if !m{\A/-\nCopyright}; exit;' {} \;)
if [ -n "$FILES" ]; then
echo "Found .lean files which do not have a copyright header:"
echo "$FILES"
exit 1
else
echo "All copyright headers present."
fi
24 changes: 6 additions & 18 deletions Arm/Attr.lean
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/-
Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author(s): Shilpi Goel
-/

import Lean

-- A minimal theory, safe for all LNSym proofs
Expand All @@ -9,21 +15,3 @@ register_simp_attr state_simp_rules
register_simp_attr bitvec_rules
-- Rules for memory lemmas
register_simp_attr memory_rules

/-
syntax "state_simp" : tactic
macro_rules
| `(tactic| state_simp) => `(tactic| simp only [state_simp_rules])
syntax "state_simp?" : tactic
macro_rules
| `(tactic| state_simp?) => `(tactic| simp? only [state_simp_rules])
syntax "state_simp_all" : tactic
macro_rules
| `(tactic| state_simp_all) => `(tactic| simp_all only [state_simp_rules])
syntax "state_simp_all?" : tactic
macro_rules
| `(tactic| state_simp_all?) => `(tactic| simp_all? only [state_simp_rules])
-/
6 changes: 6 additions & 0 deletions Arm/FromMathlib.lean
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/-
Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Released under Apache 2.0 license as described in the file LICENSE.
-/


-- This file has definitions temporarily lifted from Mathlib.
-- We will move them into Lean shortly.

Expand Down
6 changes: 6 additions & 0 deletions Arm/MinTheory.lean
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/-
Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author(s): Shilpi Goel
-/

import Arm.Attr

-- These lemmas are from lean/Init/SimpLemmas.lean.
Expand Down
5 changes: 5 additions & 0 deletions Proofs/SHA512/SHA512.lean
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/-
Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author(s): Shilpi Goel
-/
import Proofs.SHA512.SHA512_block_armv8_rules
import Proofs.SHA512.SHA512Sym
13 changes: 11 additions & 2 deletions Tactics/Common.lean
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ def findLocalDeclOfType? (expectedType : Expr) : MetaM (Option LocalDecl) := do
-- the local context, so we can safely pass it to `get!`

def findLocalDeclOfTypeOrError (expectedType : Expr) : MetaM LocalDecl := do
let some name ← findLocalDeclOfType? expectedType
let some decl ← findLocalDeclOfType? expectedType
| throwError "Failed to find a local hypothesis of type {expectedType}"
return name
return decl

/-- `findProgramHyp` searches the local context for an hypothesis of type
`state.program = ?concreteProgram`,
Expand Down Expand Up @@ -269,3 +269,12 @@ def traceHeartbeats (cls : Name) (header : Option String := none) :
let percent ← heartbeatsPercent
trace cls fun _ =>
m!"{header}used {heartbeats} heartbeats ({percent}% of maximum)"

/-! ## `withMainContext'` -/

variable {m} [Monad m] [MonadLiftT TacticM m] [MonadControlT MetaM m] in
/-- Execute `x` using the main goal local context and instances.
Unlike the standard `withMainContext`, `x` may live in a generic monad `m`. -/
def withMainContext' (x : m α) : m α := do
(← getMainGoal).withContext x
61 changes: 59 additions & 2 deletions Tactics/Reflect/AxEffects.lean
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,37 @@ structure AxEffects where

namespace AxEffects

/-! ## Monad getters -/

section Monad
variable {m} [Monad m] [MonadReaderOf AxEffects m]

def getCurrentState : m Expr := do return (← read).currentState
def getInitialState : m Expr := do return (← read).initialState
def getNonEffectProof : m Expr := do return (← read).nonEffectProof
def getMemoryEffect : m Expr := do return (← read).memoryEffect
def getMemoryEffectProof : m Expr := do return (← read).memoryEffectProof
def getProgramProof : m Expr := do return (← read).programProof

def getStackAlignmentProof? : m (Option Expr) := do
return (← read).stackAlignmentProof?

variable [MonadLiftT MetaM m] in
/-- Retrieve the user-facing name of the current state, assuming that
the current state is a free variable in the ambient local context -/
def getCurrentStateName : m Name := do
let state ← getCurrentState
@id (MetaM _) <| do
let state ← instantiateMVars state
let Expr.fvar id := state.consumeMData
| throwError "error: expected a free variable, found:\n {state} WHHOPS"
let lctx ← getLCtx
let some decl := lctx.find? id
| throwError "error: unknown fvar: {state}"
return decl.userName

end Monad

/-! ## Initial Reflected State -/

/-- An initial `AxEffects` state which has no writes.
Expand Down Expand Up @@ -185,7 +216,7 @@ partial def mkAppNonEffect (eff : AxEffects) (field : Expr) : MetaM Expr := do
return nonEffectProof

/-- Get the value for a field, if one is stored in `eff.fields`,
or assemble an instantiation of the non-effects proof -/
or assemble an instantiation of the non-effects proof otherwise -/
def getField (eff : AxEffects) (fld : StateField) : MetaM FieldEffect :=
let msg := m!"getField {fld}"
withTraceNode `Tactic.sym (fun _ => pure msg) <| do
Expand All @@ -200,6 +231,11 @@ def getField (eff : AxEffects) (fld : StateField) : MetaM FieldEffect :=
let proof ← eff.mkAppNonEffect (toExpr fld)
pure { value, proof }

variable {m} [Monad m] [MonadReaderOf AxEffects m] [MonadLiftT MetaM m] in
@[inherit_doc getField]
def getFieldM (field : StateField) : m FieldEffect := do
(← read).getField field

/-! ## Update a Reflected State -/

/-- Execute `write_mem <n> <addr> <val>` against the state stored in `eff`
Expand Down Expand Up @@ -359,6 +395,24 @@ private def assertIsDefEq (e expected : Expr) : MetaM Unit := do
if !(←isDefEq e expected) then
throwError "expected:\n {expected}\nbut found:\n {e}"

/-- Given an expression `e : ArmState`,
which is a sequence of `w`/`write_mem`s to `eff.currentState`,
return an `AxEffects` where `e` is the new `currentState`. -/
partial def updateWithExpr (eff : AxEffects) (e : Expr) : MetaM AxEffects := do
let msg := m!"Updating effects with writes from: {e}"
withTraceNode `Tactic.sym (fun _ => pure msg) <| do match_expr e with
| write_mem_bytes n addr val e =>
let eff ← eff.updateWithExpr e
eff.update_write_mem n addr val

| w field value e =>
let eff ← eff.updateWithExpr e
eff.update_w field value

| _ =>
assertIsDefEq e eff.currentState
return eff

/-- Given an expression `e : ArmState`,
which is a sequence of `w`/`write_mem`s to the some state `s`,
return an `AxEffects` where `s` is the intial state, and `e` is `currentState`.
Expand Down Expand Up @@ -466,7 +520,10 @@ def withField (eff : AxEffects) (eq : Expr) : MetaM AxEffects := do
trace[Tactic.sym] "current field effect: {fieldEff}"

if field ∉ eff.fields then
let proof ← mkEqTrans fieldEff.proof eq
let proof ← if eff.currentState == eff.initialState then
pure eq
else
mkEqTrans fieldEff.proof eq
let fields := eff.fields.insert field { value, proof }
return { eff with fields }
else
Expand Down
Loading

0 comments on commit 62c4719

Please sign in to comment.