Skip to content

Commit d86fb18

Browse files
committed
minor
1 parent 1f44266 commit d86fb18

File tree

7 files changed

+42
-35
lines changed

7 files changed

+42
-35
lines changed

program-analysis/slither/api.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Slither has an API that allows you to explore basic attributes of contracts and their functions.
44

55
On a high level there are 6 layers:
6+
67
- `Slither` - main slither object
78
- `SlitherCompilationUnit` - group of files used by one call to solc
89
- `Contract` - contract level
@@ -29,28 +30,29 @@ slither = Slither('0x..') # assuming the code is verified on etherscan
2930
```
3031

3132
Use `etherscan_api_key` to provide an [Etherscan API KEY](https://docs.etherscan.io/getting-started/viewing-api-usage-statistics)
33+
3234
```python
33-
slither = Slither('0x..', etherscan_api_key='..')
35+
slither = Slither('0x..', etherscan_api_key='..')
3436
```
3537

3638
You can retrieve the list of compilation units with:
37-
- `sl.compilation_units # array of SlitherCompilationUnit`
3839

40+
- `sl.compilation_units # array of SlitherCompilationUnit`
3941

4042
## SlitherCompilationUnit object
43+
4144
- ~ group of files used by one call to solc
4245
- Most targets have 1 compilation, but not always true
4346
- Partial compilation for optimization
4447
- Multiple solc version used
4548
- Etc..
4649
- Why compilation unit matters?
47-
- Some APIs might be not intuitive
50+
- Some APIs might be not intuitive
4851
- Ex: looking for a contract based on the name?
4952
- Can have multiple contracts
5053
- For hacking you can (probably) use the first compilation unit
5154
- `compilation_unit = sl.compilation_units[0]`
5255

53-
5456
A [`SlitherCompilationUnit`](https://github.com/crytic/slither/blob/master/slither/core/compilation_unit.py) has:
5557

5658
- `contracts (list(Contract))`: A list of contracts
@@ -59,6 +61,7 @@ A [`SlitherCompilationUnit`](https://github.com/crytic/slither/blob/master/slith
5961
- `[structures | enums | events | variables | functions]_top_level`: Top level object
6062

6163
Example
64+
6265
```python
6366
from slither import Slither
6467
sl = Slither("0xdac17f958d2ee523a2206206994597c13d831ec7")
@@ -70,6 +73,7 @@ print([str(c) for c in compilation_unit.contracts])
7073
# Print the most derived contracts from the USDT address
7174
print([str(c) for c in compilation_unit.contracts_derived])
7275
```
76+
7377
```bash
7478
% python test.py
7579
['SafeMath', 'Ownable', 'ERC20Basic', 'ERC20', 'BasicToken', 'StandardToken', 'Pausable', 'BlackList', 'UpgradedStandardToken', 'TetherToken']
@@ -78,6 +82,7 @@ print([str(c) for c in compilation_unit.contracts_derived])
7882
```
7983

8084
## Contract Object
85+
8186
A [`Contract`](https://github.com/crytic/slither/blob/master/slither/core/declarations/contract.py) object has:
8287

8388
- `name: str`: The name of the contract
@@ -93,10 +98,11 @@ A [`Contract`](https://github.com/crytic/slither/blob/master/slither/core/declar
9398
- `state_variables_ordered: List[StateVariable]`: all variable ordered by declaration
9499

95100
Example
101+
96102
```python
97103
from slither import Slither
98104
sl = Slither("0xdac17f958d2ee523a2206206994597c13d831ec7")
99-
compilation_unit = sl.compilation_units[0]
105+
compilation_unit = sl.compilation_units[0]
100106

101107
# Print all the state variables of the USDT token
102108
contract = compilation_unit.get_contract_from_name("TetherToken")[0]
@@ -117,8 +123,8 @@ A [`Function`](https://github.com/crytic/slither/blob/master/slither/core/declar
117123
- `nodes: list[Node]`: A list of nodes composing the CFG of the function/modifier
118124
- `entry_point: Node`: The entry point of the CFG
119125
- `[state |local]_variable_[read |write]: list[StateVariable]`: A list of local/state variables read/write
120-
- All can be prefixed by “all_” for recursive lookup
121-
- Ex: `all_state_variable_read`: return all the state variables read in internal calls
126+
- All can be prefixed by “all\_” for recursive lookup
127+
- Ex: `all_state_variable_read`: return all the state variables read in internal calls
122128
- `slithir_operations: List[Operation]`: list of IR operations
123129

124130
```python
@@ -129,7 +135,7 @@ contract = compilation_unit.get_contract_from_name("TetherToken")[0]
129135

130136
transfer = contract.get_function_from_signature("transfer(address,uint256)")
131137

132-
# Print all the state variables read by the transfer function
138+
# Print all the state variables read by the transfer function
133139
print([str(v) for v in transfer.state_variables_read])
134140
# Print all the state variables read by the transfer function and its internal calls
135141
print([str(v) for v in transfer.all_state_variables_read])
@@ -142,12 +148,15 @@ print([str(v) for v in transfer.all_state_variables_read])
142148
```
143149

144150
## Node object
151+
145152
[Node](https://github.com/crytic/slither/blob/master/slither/core/cfg/node.py)
146153

147154
To explore the nodes:
155+
148156
- If order does not matter
149157
- `for node in function.nodes`
150158
- If order matters, walk through the nodes
159+
151160
```python
152161
def visit_node(node: Node, visited: List[Node]):
153162

@@ -159,21 +168,22 @@ def visit_node(node: Node, visited: List[Node]):
159168
for son in node.sons:
160169
visit_node(son, visited)
161170
```
162-
- If need to iterate more than once (advanced usages)
163-
- Bound the iteration X times
164-
- Create a fix-point - abstract interpretation style analysis
165171

172+
- If need to iterate more than once (advanced usages)
173+
- Bound the iteration X times
174+
- Create a fix-point - abstract interpretation style analysis
166175

167176
## SlithIR
177+
168178
- [slither/slithir](https://github.com/crytic/slither/tree/master/slither/slithir)
169179
- Every IR operation has its own methods
170180
- Check if an operation is of a type:
171181
- `isinstance(ir, TYPE)`
172182
- Ex: `isinstance(ir, Call)`
173183
- Check if the operation is an addition
174-
- `isinstance(ir, Binary) & ir.type == BinaryType.ADDITION`
184+
- `isinstance(ir, Binary) & ir.type == BinaryType.ADDITION`
175185
- Check if the operation is a call to MyContract
176-
- `isinstance(ir, HighLevelCall) & ir.destination == MyContract`
186+
- `isinstance(ir, HighLevelCall) & ir.destination == MyContract`
177187

178188
```python
179189
from slither import Slither

program-analysis/slither/examples/coin.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: AGPL-3.0
2-
pragma solidity ^0.5.0;
2+
pragma solidity ^0.8.0;
33

44
contract Coin {
55
address owner = msg.sender;

program-analysis/slither/exercise1.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
The goal is to create a script that performs a feature that was not present in previous version of Solidity: function overriding protection.
44

5-
[exercises/exercise1/coin.sol](exercises/exercise1/coin.sol) contains a function that must never be overridden:
5+
[exercises/exercise1/coin.sol](https://github.com/crytic/building-secure-contracts/tree/master/program-analysis/slither/exercises/exercise1/coin.sol) contains a function that must never be overridden:
66

77
```solidity
88
_mint(address dst, uint256 val)
99
```
1010

1111
Use Slither to ensure that no contract inheriting Coin overrides this function.
1212

13-
Use `solc-select install 0.5.0 && solc-select use 0.5.0` to switch to solc 0.5.0
13+
Use `solc-select install 0.5.0 && solc-select use 0.5.0` to switch to solc 0.5.0
1414

1515
## Proposed Algorithm
1616

@@ -30,4 +30,4 @@ Get the Coin contract
3030

3131
## Solution
3232

33-
See [exercises/exercise1/solution.py](exercises/exercise1/solution.py).
33+
See [exercises/exercise1/solution.py](https://github.com/crytic/building-secure-contracts/tree/master/program-analysis/slither/exercises/exercise1/solution.py).

program-analysis/slither/exercise2.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Exercise 2: Access Control
22

3-
The [exercises/exercise2/coin.sol](exercises/exercise2/coin.sol) file contains an access control implementation with the `onlyOwner` modifier. A common mistake is forgetting to add the modifier to a crucial function. In this exercise, we will use Slither to implement a conservative access control approach.
3+
The [exercises/exercise2/coin.sol](https://github.com/crytic/building-secure-contracts/tree/master/program-analysis/slither/exercises/exercise2/coin.sol) file contains an access control implementation with the `onlyOwner` modifier. A common mistake is forgetting to add the modifier to a crucial function. In this exercise, we will use Slither to implement a conservative access control approach.
44

55
Our goal is to create a script that ensures all public and external functions call `onlyOwner`, except for the functions on the whitelist.
66

@@ -18,4 +18,4 @@ Explore all the functions
1818

1919
## Solution
2020

21-
Refer to [exercises/exercise2/solution.py](exercises/exercise2/solution.py) for the solution.
21+
Refer to [exercises/exercise2/solution.py](https://github.com/crytic/building-secure-contracts/tree/master/program-analysis/slither/exercises/exercise2/solution.py) for the solution.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# Exercise 3: Find function that use a given variable in a condition
22

3-
The [exercises/exercise3/find.sol](exercises/exercise3/find.sol) file contains a contract that use `my_variable` variable in multiple locations.
3+
The [exercises/exercise3/find.sol](https://github.com/crytic/building-secure-contracts/tree/master/program-analysis/slither/exercises/exercise3/find.sol) file contains a contract that use `my_variable` variable in multiple locations.
44

55
Our goal is to create a script that list all the functions that use `my_variable` in a conditional or require statement.
66

77
## Proposed Approach
88

99
Explore all the helpers provided by [`Function`](https://github.com/crytic/slither/blob/master/slither/core/declarations/function.py) object to find an easy way to reach the goal
1010

11-
1211
## Solution
1312

14-
Refer to [exercises/exercise3/solution.py](exercises/exercise3/solution.py) for the solution.
13+
Refer to [exercises/exercise3/solution.py](https://github.com/crytic/building-secure-contracts/tree/master/program-analysis/slither/exercises/exercise3/solution.py) for the solution.
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
contract Find{
2-
1+
contract Find {
32
uint my_variable;
43

5-
function condition() public{
6-
if(my_variable==0){
7-
8-
}
4+
function condition() public {
5+
if (my_variable == 0) {}
96
}
107

11-
function call_require() public{
12-
require(my_variable==0);
8+
function call_require() public {
9+
require(my_variable == 0);
1310
}
14-
15-
function read_and_write() public{
11+
12+
function read_and_write() public {
1613
my_variable = my_variable + 1;
1714
}
18-
19-
}
15+
}

program-analysis/slither/scripts/gh_action_test.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ test_exercise(){
5555

5656
cd program-analysis/slither
5757
pip install slither-analyzer
58+
solc-select install 0.8.20
59+
solc-select use 0.8.20
5860

5961
test_examples
62+
6063
solc-select install 0.5.11
6164
solc-select use 0.5.11
6265
test_exercise 1
6366

64-
solc-select install 0.8.20
6567
solc-select use 0.8.20
6668
test_exercise 2
6769

0 commit comments

Comments
 (0)