Repository navigation
Fix soundness bugs found by the coherence audit - #25
Open
evertonfraga wants to merge 15 commits into
Open
evertonfraga wants to merge 15 commits into
evertonfraga wants to merge 15 commits into
Conversation
An invariant is assumed on entry to every external function and checked on exit, so it must hold between transactions, whoever calls next. `block.*` and `tx.*` were already rejected in invariants for that reason, but `msg.sender` and `msg.value` were accepted, and an invariant over them was established for one caller and assumed for the next. With `invariant owner == msg.sender`, `require(msg.sender != owner)` could never pass, so every property after it was proven: the false `ensures x == 7` of a function that sets `x = 1` passed, even under --strict. `invariant balances[msg.sender] <= totalSupply` was proven for a contract that sets another account's balance above the supply. Both are now rejected like `block.timestamp`. test/examples/invariant_msg_sender.sol and invariant_msg_value.sol pin the rejections; invariant_block_value.sol pins the existing rejection of block values, which no fixture covered.
`require(cond, msg)` kept only `cond`. Solidity evaluates every argument
before the check, even when `cond` holds ("require arguments are evaluated
unconditionally"), so in `require(true, f())` or `require(c, E(f()))` the
call `f()` runs on the passing path. Dropping it proved false properties:
`ensures notes == old(notes)` passed for a function whose require message
increments `notes`, while the true `notes == old(notes) + 1` was refuted.
The message's effects are now evaluated before the assume; for a custom
error `E(args)`, the effects of its arguments (an error value itself has
none). As for any call, an effect that changes a variable another
argument reads is rejected, since solc's argument order is unspecified.
test/examples/require_message_effects.sol pairs each true property with a
false one, for string and custom-error messages.
`forall v : T :: P` and `exists v : T :: P` bound `v` over all integers, whatever `T` was. So false existentials were proven: `exists i : uint8 :: i == k` for every uint256 `k`, `exists v : uint256 :: v + 1 == 0`, and true universals over a type were refuted (`forall v : uint8 :: v <= 255`). The body now carries the type's range: `forall v :: inRange(v) ==> P` and `exists v :: inRange(v) && P`, for every type with a range (uintN, intN, address, bytesN). Those false existentials are now refuted, the true universals proven. test/examples/quantifier_types.sol pairs true and false properties for both quantifiers over uint8, int8, uint256 and address.
A variable bound at an integer type now carries the range of its type (`forall v : T :: P` parses to `forall v :: inRange(v) ==> P`). The round-trip property serialized that guard as part of the body, so re-parsing added a second one and REQ-PBT-001 failed on its first sample. The canonical serializer now recognizes the guard, recovers the type name from its bounds and emits only the written body. The generator draws quantified variables from more types (`uint8`, `int8`, `int256`, `bytes32`, `string` as well as `bool`, `address` and `uint256`), covering guarded, unguarded and boolean binders.
An uninterpreted bitwise result (`&`, `|`, `^`, a shift by a variable, and their `op=` forms) lay in [0, 2^256) whatever the operands' type, so a signed result was never negative: `(a & a) >= 0` was proven for `int8 a == -1`, as was `(a >> n) >= 0`. The result is now wrapped into the operation's type, which maps [0, 2^256) onto every value of it. `~x` is exact: `-x - 1` for a signed `x`, `2^N - 1 - x` for an N-bit unsigned one (it was uninterpreted). test/examples/bitwise_types.sol pairs each behaviour with a true and a false property; before this change five of its false properties were proven and four of its true ones failed.
solc evaluates constant expressions exactly, with rational intermediate values, and records the result in the expression's type (`int_const 7`). The translator evaluated them step by step in integers instead, so `(7 / 2) * 2` was 6 and `assert(x == 6)` was proven. A literal, unary or binary expression of `int_const` type now translates to that value. This also makes fractional units and exponents work (`2.5 ether`, `1e18`), which were rejected as invalid number literals.
…wrap `type(int8).min / -1` is `type(int8).min` in `unchecked` code, but the quotient stayed unbounded (128), so `c > 0` was proven. A signed `/` and `/=` now wrap within their type in `unchecked`, like `+`, `-` and `*`. A power of named constants (`B ** 2`, `B` a `uint8` constant) is folded to a constant of their type; in `unchecked` it now wraps within that type too (`B ** 2` is 0 for `B == 16`, not 256). The division keeps its own source location under the wrap, so its `divisor is non-zero` check stays located.
A specification's `/` and `%` were Euclidean (`-7 % 2 == 1`), while the code's signed `/` and `%` truncate (`-7 % 2 == -1`). So `ensures a % 2 == 0 || a % 2 == 1` was proven for every int256 `a`, and `ensures r == a / 2` failed for `r = a / 2`. Specs now use the truncating operations; for non-negative operands they agree with the Euclidean ones.
integers.md and LIMITATIONS.md now say that a constant expression has solc's exact value (fractional units and exponents included), that `~x` is exact and other bitwise results range over their type, that a signed `/` and a power of constants wrap in `unchecked`, and that `/` and `%` round toward zero in specifications too.
A mapping or array `storage` parameter refers to the caller's storage: `_credit(balances, k)` with `m[k] += 1` writes `balances[k]`. Parameters are modelled as local copies, so the write was lost, and `ensures balances[k] == old(balances[k])` was proven for a function that increments it. Such parameters are now rejected, as local mapping and array storage references already were. Struct `storage` parameters are unchanged.
Assembly `return` and `stop`, and `selfdestruct`, end the whole transaction with the state written so far. They were modelled as a return from the function they are in, which is right in an entry point but not in a function another one calls: the caller continued, so after `x = 1; _maybeStop(v); x = 2;` the postcondition `x == 2` was proven, though `_maybeStop(7)` ends the call with `x == 1`. A halt now translates to a labelled block, and a call of a function whose body contains one is rejected. A halt in the entry point itself is still modelled as before.
Solidity copies a struct into storage, and from storage or calldata into memory; only memory-to-memory assignments share it. Structs are heap objects in the model, and every assignment shared the object, so after `b = a; b.value = 5` the postcondition `a.value == 5` was proven, as was `a.value == 9` after `Info memory m = a; m.value = 9`. Where solc's types say the struct changes data location (an assignment, a declaration, an argument of an internal call, a returned value), the translation now allocates a new object and copies the fields into it, nested structs included. A returned tuple holding such a struct is rejected. A memory struct passed to an internal function is shared with it, and the callee's heap frame does not cover it: its fields are now unknown after the call, where they were assumed unchanged (`Info memory m = a; _bump(m); assert(m.value == a.value)` was proven).
…rage
`Info storage d = data;` declared one local per field of `data`, set
when `d` was declared, so a read `d.value` returned that value even after
`data` changed: after `data = Info({value: 5, …})` or a call that writes
`data`, `ensures r == 1` was proven for `r = d.value`.
A storage reference no longer declares locals: a read `d.f` reads the
storage `d` refers to when it runs (`self#data#f`), when `d` was taken
from a path of state-variable fields. For any other reference (one
returned by a call) each read is an unknown value. Writes through a
reference stay rejected.
CI runs Ubuntu 24.04's z3 4.8.12, which left four obligations of the new fixtures unknown that z3 4.15 decides: - bitwise_types.sol: `(a & b) <= 254` times out; the false companion of `(a & b) <= 255` is now `(a & b) >= 1` (refuted by 0 & 0). - quantifier_types.sol: 4.8.12 decides an existential only when its body simplifies to false. The refuted existentials are now of that form (`exists i : uint8 :: i > 255`, `exists v : uint256 :: v < 0`), and the true one with a witness (`exists i : uint8 :: i == k` for k <= 255) is dropped. The golden suite passes under CI=true with both versions.
This was referenced Oct 23, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the soundness findings of the coherence audit that affect
main: each made a false property verify. Stacked on #23 (the new fixtures use its per-obligation goldens); review after it.One commit per fix. Each commit adds a fixture under
test/examples/that pairs true and false properties, with// Passes/// Refuted/// Not provencomments checked by the golden suite. Before each fix, the fixture's false properties were proven (✅), or the file was accepted where it is now rejected.Translator
fix(translator): reject msg.sender and msg.value in invariantsblock.*already was. An invariant holds between transactions; one over the caller was established for one caller and assumed for the next.invariant_msg_sender.sol,invariant_msg_value.sol,invariant_block_value.solfix(translator): keep the effects of require's message argumentsrequire(c, f())andrequire(c, E(f()))evaluate the message's effects before assumingc.require_message_effects.solfix(translator): a bitwise result has the type of the operation&,|,^, shift by a variable (andop=) is wrapped into its type, so a signed result can be negative.~xis exact.bitwise_types.solfix(translator): a constant expression has the value solc computedint_const Ntranslates toN:(7 / 2) * 2is 7, not 6. Also accepts2.5 etherand1e18, which failed as invalid literals.constant_expressions.solfix(translator): in unchecked, signed division and constant powers wraptype(int8).min / -1andB ** 2(B a named constant) wrap within their type inunchecked. The division keeps its own source location under the wrap.unchecked_wrapping.solfix(translator): reject mapping and array storage parametersstorage_param_mapping.sol,storage_param_array.solfix(translator): reject calls of functions that may end the transactionreturn/stop,selfdestruct) translates to a labelled block. A call of a function containing one is rejected: the caller continued after it. A halt in the entry point itself is modelled as before.halt_in_helper.sol,selfdestruct_in_helper.solfix(translator): copy structs where Solidity copies themstruct_copies.solfix(translator): a read through a storage reference reads current storageInfo storage d = datano longer snapshotsdata's fields into locals.d.freadsdata's storage when the read runs. A reference returned by a call reads unknown values.storage_reference_reads.solSpecification language
fix(spec): a quantified variable ranges over its declared typeforall v : T :: Pbecomesforall v :: inRange(v) ==> P, andexistsbecomesinRange(v) && P, for uintN, intN, address and bytesN.quantifier_types.solfix(spec): / and % round toward zero, as in Solidity-7 % 2 == 1.spec_division.soltest(spec): round-trip the range guard of quantified variablesSpecParserTest.leanDocs
docs/guide/src/integers.md,limitations.md,supported_features.md,contract_invariants.mdandLIMITATIONS.mddescribe the new behaviour.CHANGELOG.mdhas an entry for these fixes.Testing
CI=true bash test/run_tests.sh: 68 passed, 0 failed, exit-code contract 8/8 (15 new fixtures), with z3 4.15.4 and with z3 4.8.12 (the version CI's Ubuntu image ships). No existing golden changed. Obligations that the two z3 versions decide differently are not pinned (see the last commit).lake test,python3 -m pytest Tools/Solidity/test(15 passed),lake run pbt-determinism, and the docs site build all pass.main@ 87df510) to confirm that its false properties were proven there.Not covered
heap frame (only this contract is modified). This is audit finding A1-11, a false alarm that predates this PR; the fixtures avoid such writes.