Skip to content

Commit 26598f4

Browse files
authored
Merge pull request #447 from Certora/yoav/safecasting-builtin
docs for safeCasting and uncheckedOverflow builtin rules
2 parents 3b95e09 + 4154db7 commit 26598f4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/cvl/builtin.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ built_in_rule_name ::=
2626
| "sanity"
2727
| "deepSanity"
2828
| "viewReentrancy"
29+
| "safeCasting"
30+
| "uncheckedOverflow"
2931
```
3032

3133
(built-in-msg-value-in-loop)=
@@ -210,3 +212,39 @@ This ensures that the external call cannot observe `currentContract` in any stat
210212
called from `currentContract`.
211213

212214

215+
(built-in-safe-casting)=
216+
Safe Casting — `safeCasting`
217+
--------------------------------------------------
218+
219+
The `safeCasting` built-in rule is there to catch cases where an explicit cast in solidity is actually out of bounds.
220+
For example, if an `uint` is cast to a `uint64`, the rule will fail if there is some external function in the contract, that can be called
221+
in such a way, that the value being cast is larger than `2^64 - 1`. Casting between signed and unsigned values are also checked. So if an `uint` is cast to an `int`, and the cast value is larger than `2^255 - 1`, the rule will fail.
222+
223+
This rule can be enabled by including
224+
```cvl
225+
use builtin rule safeCasting;
226+
```
227+
in a spec file. In addition, the line `"safe_casting_builtin" : true` must be added to the conf file.
228+
229+
230+
(built-in-unchecked-overflow)=
231+
Unchecked Overflow — `uncheckedOverflow`
232+
--------------------------------------------------
233+
234+
The `uncheckedOverflow` built-in looks for cases where any of the operations `+, -, *` appearing within an `unchecked` solidity block can actually overflow.
235+
236+
For example:
237+
```
238+
function basicMul(uint128 x, uint128 y) public pure returns (uint) {
239+
unchecked {
240+
return x * y;
241+
}
242+
}
243+
```
244+
This will fail the builtin rule, because the multiplication is a `uint128` multiplication (even if it's later considered as a `uint`).
245+
246+
This rule can be enabled by including
247+
```cvl
248+
use builtin rule uncheckedOverflow;
249+
```
250+
In addition, the line `"unchecked_overflow_builtin" : true` must be added to the conf file.

0 commit comments

Comments
 (0)