From 997a2b6512f31e33cd164ab627b26ccc89b1b09c Mon Sep 17 00:00:00 2001 From: Dan Robinson Date: Sat, 14 Nov 2026 02:08:04 -0800 Subject: [PATCH] Validate storage bytes headers before deletion --- SolidCore.lean | 1 + SolidCore/Solidity/Interpreter.lean | 27 ++++---- .../Witness/DirtyStorageBytesDelete.lean | 63 +++++++++++++++++++ 3 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 SolidCore/Witness/DirtyStorageBytesDelete.lean diff --git a/SolidCore.lean b/SolidCore.lean index b192303d..b4a9b24e 100644 --- a/SolidCore.lean +++ b/SolidCore.lean @@ -157,3 +157,4 @@ import SolidCore.Witness.TupleNestedPush import SolidCore.Witness.WideSignedStorageCopy import SolidCore.Witness.EnumMappingKeyCleanup import SolidCore.Witness.EnumMappingKeyContract +import SolidCore.Witness.DirtyStorageBytesDelete diff --git a/SolidCore/Solidity/Interpreter.lean b/SolidCore/Solidity/Interpreter.lean index 7cbefcf2..cbef8eb8 100644 --- a/SolidCore/Solidity/Interpreter.lean +++ b/SolidCore/Solidity/Interpreter.lean @@ -3502,6 +3502,12 @@ def State.storeStorageBytesAt (state : State) (slot : Word) State.clearStorageBytesLongSlots written slot newLongChunks (oldLongChunks - newLongChunks) +/-- Solidity validates the old bytes header before deleting the value. -/ +def State.deleteStorageBytesAt (state : State) (slot : Word) : + Except RevertData State := do + let _ ← storageBytesHeader? (state.loadSlot slot) + pure (state.storeStorageBytesAt slot []) + def State.storageBytesLengthAt (state : State) (slot : Word) : Except RevertData Nat := do let header ← storageBytesHeader? (state.loadSlot slot) @@ -4102,9 +4108,9 @@ def State.clearStorageLayoutAt (state : State) (slot : Word) : | StorageLayout.struct layouts => State.clearStructLayoutSlots state slot { slot := 0, offset := 0 } layouts | StorageLayout.bytes => - Except.ok (State.storeStorageBytesAt state slot []) + State.deleteStorageBytesAt state slot | StorageLayout.string => - Except.ok (State.storeStorageBytesAt state slot []) + State.deleteStorageBytesAt state slot | StorageLayout.dynamicArray _ => Except.ok (state.storeSlot slot 0) | StorageLayout.mapping _ _ => @@ -4217,9 +4223,9 @@ def State.clearStorageLayoutAtFuel : (state, { slot := 0, offset := 0 }) Except.ok state | StorageLayout.bytes => - Except.ok (State.storeStorageBytesAt state slot []) + State.deleteStorageBytesAt state slot | StorageLayout.string => - Except.ok (State.storeStorageBytesAt state slot []) + State.deleteStorageBytesAt state slot | StorageLayout.dynamicArray elementLayout => do let length := SolidCore.Solidity.Shared.norm (state.loadSlot slot) let state ← @@ -4519,16 +4525,9 @@ def Runtime.deleteStorageField (context : Context) State.clearStorageLayoutAtDeep runtime.state field.slot (StorageLayout.dynamicArray elementLayout) Except.ok { runtime with state } - | some StorageLayout.bytes => - Except.ok - { runtime with - state := - State.storeStorageBytesAt runtime.state field.slot [] } - | some StorageLayout.string => - Except.ok - { runtime with - state := - State.storeStorageBytesAt runtime.state field.slot [] } + | some StorageLayout.bytes | some StorageLayout.string => do + let state ← runtime.state.deleteStorageBytesAt field.slot + pure { runtime with state } | some (StorageLayout.struct layouts) => do let state ← State.clearStorageLayoutAtDeep runtime.state field.slot diff --git a/SolidCore/Witness/DirtyStorageBytesDelete.lean b/SolidCore/Witness/DirtyStorageBytesDelete.lean new file mode 100644 index 00000000..3ffce0d1 --- /dev/null +++ b/SolidCore/Witness/DirtyStorageBytesDelete.lean @@ -0,0 +1,63 @@ +import SolidCore.Solidity.Checked +import SolidCore.Witness.Checked +namespace SolidCore +namespace Solidity +namespace SolcAstImport +namespace DirtyStorageBytesDelete + +def importedSourceName : String := "spec-hunt/87aeea6b.sol" + +def importedContractDecl0 : ContractDecl := +{ kind := ContractKind.contract + name := "D" + abstract := false + bases := [] + items := [(ContractItem.stateVar + { name := "b", + ty := Ty.bytes, + visibility := some Visibility.internal_, + mutability := VarMutability.mutable, + override? := none, + init := none }), (ContractItem.function + { kind := FunctionKind.function, + name := some "run", + visibility := some Visibility.external_, + mutability := StateMutability.nonpayable, + params := [], + returns := [{ name := none, ty := Ty.uint 256, location := none }], + virtual := false, + override? := none, + modifiers := [], + body := some (Stmt.block [Stmt.expr (Expr.unary UnaryOp.delete (Expr.ident "b")), Stmt.returnValues (some (Expr.literal (Literal.number "1")))]) })] } + +def importedContract : ContractDecl := + importedContractDecl0 + +def importedContracts : List ContractDecl := + [importedContractDecl0] + +def importedSourceUnit : SourceUnit := + { items := [SourceItem.pragma "solidity" "0.8.35", SourceItem.contract importedContractDecl0] } + +def importedContractAccepted : Bool := + TypeCheck.Result.isOk (TypeCheck.TypecheckedInput.checkedSourceUnit importedSourceUnit) + +end DirtyStorageBytesDelete +end SolcAstImport +end Solidity +end SolidCore + +namespace SolidCore.Solidity.Witness.DirtyStorageBytesDelete +open SolidCore.Solidity.Source +open SolidCore.Solidity.TypeCheck +abbrev D := SolidCore.Solidity.SolcAstImport.DirtyStorageBytesDelete.importedContract +private def isOkTrue : Except TypeError Bool -> Bool + | .ok true => true + | _ => false +#guard isOkTrue (Examples.checkedOwnCallPanicMatches 4096 D "run" (State.empty.storeSlot 0 3) [] 34) +#guard isOkTrue (Examples.checkedOwnCallPanicMatches 4096 D "run" (State.empty.storeSlot 0 64) [] 34) +#guard isOkTrue (Examples.checkedOwnCallWordMatches 4096 D "run" (State.empty.storeSlot 0 2) [] 1) +#guard isOkTrue (do + let state ← Examples.checkedOwnCallState 4096 D "run" (State.empty.storeSlot 0 2) [] + pure (wordEq (state.loadSlot 0) 0)) +end SolidCore.Solidity.Witness.DirtyStorageBytesDelete