Property Checking with Scribble and Mythril
Intro
In this tutorial we will learn how to:
start using scribble
add a custom property to an ERC20 token contract
look for violations of our custom property with mythril
Without further ado...
Dependencies
Make sure that you have the following installed:
python3 and pip3
nodejs version 12.x and npm.
Setup
To get setup you first need to install scribble:
Next install mythril:
Now we are ready to begin!
Background
Before we begin, we should clarify what we mean by 'custom properties'. Custom properties in general can be any predicate you have in mind about your code. For example, a custom property can be:
Only the current owner of the contract may change the
owner
state variableMy contract will allow trades only after the
initialize()
function was calledThe number of minted tokens only increases
etc..
In order to be able to reason about these properties formally however, we need to translate their English statements into a formal language that automated tools understand. We will show how to do this using the Scribble spec language. Note that the language choice imposes certain limitations on what can be expressed. We are actively working on adding more expressive power to Scribble.
Our example
For this tutorial we will consider a simple ERC20 token, and annotate it with a custom property expressed in the Scribble language. Take the code below and save it as Token.sol
in a directory of your choice:
Token.sol
Custom Properties
Lets consider the transfer
function above.
There are several properties that could be specified over the transfer function:
If the transfer function succeeds then the recipient had sufficient balance at the start
If the transfer succeeds then the sender will have `_value` subtracted from it’s balance
If the transfer succeeds then the receiver will have `_value` added to it’s balance
If the transfer succeeds then the sum of the balances between the sender and receiver remains he same
For this example we’ll go with property #4;
💡 Try to write annotations for the other two properties after you’ve finished this getting started guide!
Formalizing the property
First things first, we need to dissect what the property actually entails. There are two main parts: if the transfer succeeds
and the sum of the sender and receiver balances is the same before and after the transaction
.
if the transfer succeeds
This specifies when you expect something to hold. In the scribble specification language we use if_succeeds
function annotations to check properties after successful termination of a function. Check out the specification docs for detailed information on the if_succeeds
keyword.
the sum of the sender and receiver balances is the same before and after the transaction
This is the boolean expression that we expect to hold. Note that this property introduces a statement that relates the initial and final state of the contract. That is, we want to know that the sum of the balances (_balances[sender] + _balances[receiver]
) doesn’t change relative to the state before the transaction. To allow you to express such properties the old()
is used. Check out the specification docs for more information on old
.
Using this keyword we can formulate the following expression:
Now we can combine the two to get to our specification:
Adding the annotation
if_succeeds
annotations are function specifications, which in the Scribble specification language are placed above the function definitions using a docstring comment.
🔍 Analysis
To see if the property holds, you can run mythril from the command line as follows:
The -t
option tells mythril to explore only 1 transaction deep, and --execution-timeout
specifies a timeout of 60 sec as the name implies. This should be sufficient, however if for some reason you don't see the expected failures, try a 2 minute timeout.
In about a minute mythril should return with two violations - an assertion violation and a user assertion violation:
And voila! We have found a counterexample to our property! Note that the first failure directly incudes the user-readable label from the assertion, which makes it easy to identify which property was violated if you have multiple:
If you were to decode the transaction data:
You would find that the recipient of the transfer is 0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe
which is the address of the CREATOR
account in mythril. That however is also the same account that sent the transaction:
So the failure occurs when the sender and receiver of transfer
are the same. We will leave it up to the reader to look at the code and figure out what goes wrong in this case.
Last updated