more cleanup

pull/2099/head
alpharush 1 year ago
parent 239369c853
commit e8fa8b85fa
  1. 4
      slither/core/compilation_unit.py
  2. 8
      slither/core/declarations/function.py
  3. 4
      slither/core/declarations/solidity_variables.py
  4. 1
      slither/core/expressions/identifier.py
  5. 2
      slither/detectors/abstract_detector.py
  6. 1
      slither/visitors/slithir/expression_to_slithir.py
  7. 13
      tests/e2e/vyper_parsing/snapshots/ast_parsing__vyper_cfgir_chain_test__0.txt
  8. 72
      tests/e2e/vyper_parsing/snapshots/ast_parsing__vyper_cfgir_if_compute__0.txt
  9. 9
      tests/e2e/vyper_parsing/snapshots/ast_parsing__vyper_cfgir_literal_slitherConstructorConstantVariables__0.txt
  10. 9
      tests/e2e/vyper_parsing/snapshots/ast_parsing__vyper_cfgir_tricky_slitherConstructorConstantVariables__0.txt
  11. 57
      tests/e2e/vyper_parsing/snapshots/ast_parsing__vyper_cfgir_tuple_bar__0.txt
  12. 12
      tests/e2e/vyper_parsing/snapshots/ast_parsing__vyper_cfgir_tuple_foo__0.txt
  13. 37
      tests/e2e/vyper_parsing/test_data/if.vy

@ -97,6 +97,10 @@ class SlitherCompilationUnit(Context):
# region Compiler
###################################################################################
###################################################################################
@property
def language(self) -> Language:
return self._language
@property
def is_vyper(self) -> bool:
return self._language == Language.VYPER

@ -107,7 +107,6 @@ def _filter_state_variables_written(expressions: List["Expression"]):
return ret
# TODO replace
class FunctionLanguage(Enum):
Solidity = 0
Yul = 1
@ -220,8 +219,9 @@ class Function(SourceMapping, metaclass=ABCMeta): # pylint: disable=too-many-pu
self.compilation_unit: "SlitherCompilationUnit" = compilation_unit
# Assume we are analyzing Solidity by default
self.function_language: FunctionLanguage = FunctionLanguage.Solidity
self.function_language: FunctionLanguage = (
FunctionLanguage.Solidity if compilation_unit.is_solidity else FunctionLanguage.Vyper
)
self._id: Optional[str] = None
@ -1527,7 +1527,6 @@ class Function(SourceMapping, metaclass=ABCMeta): # pylint: disable=too-many-pu
def _analyze_read_write(self) -> None:
"""Compute variables read/written/..."""
write_var = [x.variables_written_as_expression for x in self.nodes]
print(write_var)
write_var = [x for x in write_var if x]
write_var = [item for sublist in write_var for item in sublist]
write_var = list(set(write_var))
@ -1763,7 +1762,6 @@ class Function(SourceMapping, metaclass=ABCMeta): # pylint: disable=too-many-pu
node.irs_ssa = [ir for ir in node.irs_ssa if not self._unchange_phi(ir)]
def generate_slithir_and_analyze(self) -> None:
print("generate_slithir_and_analyze")
for node in self.nodes:
node.slithir_generation()

@ -17,6 +17,7 @@ SOLIDITY_VARIABLES = {
"block": "",
"super": "",
"chain": "",
"ZERO_ADDRESS": "address",
}
SOLIDITY_VARIABLES_COMPOSED = {
@ -89,8 +90,9 @@ SOLIDITY_FUNCTIONS: Dict[str, List[str]] = {
"codehash(address)": ["bytes32"],
# Vyper
"create_from_blueprint()": [],
"create_minimal_proxy_to()": [],
"empty()": [],
"convert()": [], # TODO make type conversion
"convert()": [],
"len()": ["uint256"],
"method_id()": [],
"unsafe_sub()": [],

@ -26,7 +26,6 @@ class Identifier(Expression):
],
) -> None:
super().__init__()
assert value
# pylint: disable=import-outside-toplevel
from slither.core.declarations import Contract, SolidityVariable, SolidityFunction
from slither.solc_parsing.yul.evm_functions import YulBuiltin

@ -182,7 +182,7 @@ class AbstractDetector(metaclass=abc.ABCMeta):
and self.compilation_unit.solc_version in self.VULNERABLE_SOLC_VERSIONS
)
if self.LANGUAGE:
return self.compilation_unit._language.value == self.LANGUAGE
return self.compilation_unit.language.value == self.LANGUAGE
return True
@abc.abstractmethod

@ -482,7 +482,6 @@ class ExpressionToSlithIR(ExpressionVisitor):
def _post_literal(self, expression: Literal) -> None:
expression_type = expression.type
assert isinstance(expression_type, ElementaryType)
print(expression.value)
cst = Constant(expression.value, expression_type, expression.subdenomination)
set_val(expression, cst)

@ -1,13 +0,0 @@
digraph{
0[label="Node Type: ENTRY_POINT 0
"];
0->1;
1[label="Node Type: NEW VARIABLE 1
EXPRESSION:
x = bytes32(chain.id)
IRs:
TMP_0 = CONVERT chain.id to bytes32
x(bytes32) := TMP_0(bytes32)"];
}

@ -5,34 +5,34 @@ digraph{
1[label="Node Type: NEW VARIABLE 1
EXPRESSION:
p_new = p
a = p
IRs:
p_new(uint256) := p(uint256)"];
a(uint256) := p(uint256)"];
1->2;
2[label="Node Type: NEW VARIABLE 2
EXPRESSION:
dt = 1
b = 1
IRs:
dt(uint256) := 1(uint256)"];
b(uint256) := 1(uint256)"];
2->3;
3[label="Node Type: NEW VARIABLE 3
EXPRESSION:
ratio = 0
c = 0
IRs:
ratio(uint256) := 0(uint256)"];
c(uint256) := 0(uint256)"];
3->4;
4[label="Node Type: IF 4
EXPRESSION:
dt > 0
b > 0
IRs:
TMP_0(bool) = dt > 0
TMP_0(bool) = b > 0
CONDITION TMP_0"];
4->6[label="True"];
4->5[label="False"];
@ -41,26 +41,26 @@ CONDITION TMP_0"];
6[label="Node Type: NEW VARIABLE 6
EXPRESSION:
old_p_o = 1
old_a = 1
IRs:
old_p_o(uint256) := 1(uint256)"];
old_a(uint256) := 1(uint256)"];
6->7;
7[label="Node Type: NEW VARIABLE 7
EXPRESSION:
old_ratio = 2
old_c = 2
IRs:
old_ratio(uint256) := 2(uint256)"];
old_c(uint256) := 2(uint256)"];
7->8;
8[label="Node Type: IF 8
EXPRESSION:
p > old_p_o
p > old_a
IRs:
TMP_1(bool) = p > old_p_o
TMP_1(bool) = p > old_a
CONDITION TMP_1"];
8->10[label="True"];
8->15[label="False"];
@ -70,23 +70,23 @@ CONDITION TMP_1"];
10[label="Node Type: EXPRESSION 10
EXPRESSION:
ratio = unsafe_div()(old_p_o * 10 ** 18,p)
c = unsafe_div()(old_a * 10 ** 18,p)
IRs:
TMP_2(uint256) = 10 (c)** 18
TMP_3(uint256) = old_p_o (c)* TMP_2
TMP_3(uint256) = old_a (c)* TMP_2
TMP_4(None) = SOLIDITY_CALL unsafe_div()(TMP_3,p)
ratio(uint256) := TMP_4(None)"];
c(uint256) := TMP_4(None)"];
10->11;
11[label="Node Type: IF 11
EXPRESSION:
ratio < 10 ** 36 / 1
c < 10 ** 36 / 1
IRs:
TMP_5(uint256) = 10 (c)** 36
TMP_6(uint256) = TMP_5 (c)/ 1
TMP_7(bool) = ratio < TMP_6
TMP_7(bool) = c < TMP_6
CONDITION TMP_7"];
11->13[label="True"];
11->12[label="False"];
@ -96,44 +96,44 @@ CONDITION TMP_7"];
13[label="Node Type: EXPRESSION 13
EXPRESSION:
p_new = unsafe_div()(old_p_o * 1,10 ** 18)
a = unsafe_div()(old_a * 1,10 ** 18)
IRs:
TMP_8(uint256) = old_p_o (c)* 1
TMP_8(uint256) = old_a (c)* 1
TMP_9(uint256) = 10 (c)** 18
TMP_10(None) = SOLIDITY_CALL unsafe_div()(TMP_8,TMP_9)
p_new(uint256) := TMP_10(None)"];
a(uint256) := TMP_10(None)"];
13->14;
14[label="Node Type: EXPRESSION 14
EXPRESSION:
ratio = 10 ** 36 / 1
c = 10 ** 36 / 1
IRs:
TMP_11(uint256) = 10 (c)** 36
TMP_12(uint256) = TMP_11 (c)/ 1
ratio(uint256) := TMP_12(uint256)"];
c(uint256) := TMP_12(uint256)"];
14->12;
15[label="Node Type: EXPRESSION 15
EXPRESSION:
ratio = unsafe_div()(p * 10 ** 18,old_p_o)
c = unsafe_div()(p * 10 ** 18,old_a)
IRs:
TMP_13(uint256) = 10 (c)** 18
TMP_14(uint256) = p (c)* TMP_13
TMP_15(None) = SOLIDITY_CALL unsafe_div()(TMP_14,old_p_o)
ratio(uint256) := TMP_15(None)"];
TMP_15(None) = SOLIDITY_CALL unsafe_div()(TMP_14,old_a)
c(uint256) := TMP_15(None)"];
15->16;
16[label="Node Type: IF 16
EXPRESSION:
ratio < 10 ** 36 / 1
c < 10 ** 36 / 1
IRs:
TMP_16(uint256) = 10 (c)** 36
TMP_17(uint256) = TMP_16 (c)/ 1
TMP_18(bool) = ratio < TMP_17
TMP_18(bool) = c < TMP_17
CONDITION TMP_18"];
16->18[label="True"];
16->17[label="False"];
@ -143,30 +143,30 @@ CONDITION TMP_18"];
18[label="Node Type: EXPRESSION 18
EXPRESSION:
p_new = unsafe_div()(old_p_o * 10 ** 18,1)
a = unsafe_div()(old_a * 10 ** 18,1)
IRs:
TMP_19(uint256) = 10 (c)** 18
TMP_20(uint256) = old_p_o (c)* TMP_19
TMP_20(uint256) = old_a (c)* TMP_19
TMP_21(None) = SOLIDITY_CALL unsafe_div()(TMP_20,1)
p_new(uint256) := TMP_21(None)"];
a(uint256) := TMP_21(None)"];
18->19;
19[label="Node Type: EXPRESSION 19
EXPRESSION:
ratio = 10 ** 36 / 1
c = 10 ** 36 / 1
IRs:
TMP_22(uint256) = 10 (c)** 36
TMP_23(uint256) = TMP_22 (c)/ 1
ratio(uint256) := TMP_23(uint256)"];
c(uint256) := TMP_23(uint256)"];
19->17;
20[label="Node Type: EXPRESSION 20
EXPRESSION:
ratio = 1
c = 1
IRs:
ratio(uint256) := 1(uint256)"];
c(uint256) := 1(uint256)"];
20->5;
}

@ -1,9 +0,0 @@
digraph{
0[label="Node Type: OTHER_ENTRYPOINT 0
EXPRESSION:
MAX_BANDS = 10
IRs:
MAX_BANDS(uint256) := 10(uint256)"];
}

@ -1,9 +0,0 @@
digraph{
0[label="Node Type: OTHER_ENTRYPOINT 0
EXPRESSION:
MAX_TICKS_UINT = 50
IRs:
MAX_TICKS_UINT(uint256) := 50(uint256)"];
}

@ -1,57 +0,0 @@
digraph{
0[label="Node Type: ENTRY_POINT 0
"];
0->1;
1[label="Node Type: NEW VARIABLE 1
EXPRESSION:
a = 0
IRs:
a(int128) := 0(uint256)"];
1->2;
2[label="Node Type: NEW VARIABLE 2
EXPRESSION:
b = 0
IRs:
b(int128) := 0(uint256)"];
2->3;
3[label="Node Type: EXPRESSION 3
EXPRESSION:
(a,b) = self.foo()
IRs:
TUPLE_0(int128,int128) = INTERNAL_CALL, tuple.foo()()
a(int128)= UNPACK TUPLE_0 index: 0
b(int128)= UNPACK TUPLE_0 index: 1 "];
3->4;
4[label="Node Type: NEW VARIABLE 4
EXPRESSION:
x = 0x0000000000000000000000000000000000000000
IRs:
x(address) := 0(address)"];
4->5;
5[label="Node Type: NEW VARIABLE 5
EXPRESSION:
c = 0
IRs:
c(uint256) := 0(uint256)"];
5->6;
6[label="Node Type: EXPRESSION 6
EXPRESSION:
(a,c) = Test(x).foo()
IRs:
TMP_0 = CONVERT x to Test
TUPLE_1(int128,uint256) = HIGH_LEVEL_CALL, dest:TMP_0(Test), function:foo, arguments:[]
a(int128)= UNPACK TUPLE_1 index: 0
c(uint256)= UNPACK TUPLE_1 index: 1 "];
}

@ -1,12 +0,0 @@
digraph{
0[label="Node Type: ENTRY_POINT 0
"];
0->1;
1[label="Node Type: RETURN 1
EXPRESSION:
(2,3)
IRs:
RETURN 2,3"];
}

@ -1,25 +1,22 @@
@external
@view
def limit_p_o(p: uint256):
p_new: uint256 = p
dt: uint256 = 1
ratio: uint256 = 0
def compute(p: uint256):
a: uint256 = p
b: uint256 = 1
c: uint256 = 0
if dt > 0:
old_p_o: uint256 = 1
old_ratio: uint256 = 2
# ratio = p_o_min / p_o_max
if p > old_p_o:
ratio = unsafe_div(old_p_o * 10**18, p)
if ratio < 10**36 / 1:
p_new = unsafe_div(old_p_o * 1, 10**18)
ratio = 10**36 / 1
if b > 0:
old_a: uint256 = 1
old_c: uint256 = 2
if p > old_a:
c = unsafe_div(old_a * 10**18, p)
if c < 10**36 / 1:
a = unsafe_div(old_a * 1, 10**18)
c = 10**36 / 1
else:
ratio = unsafe_div(p * 10**18, old_p_o)
if ratio < 10**36 / 1:
p_new = unsafe_div(old_p_o * 10**18, 1)
ratio = 10**36 / 1
c = unsafe_div(p * 10**18, old_a)
if c < 10**36 / 1:
a = unsafe_div(old_a * 10**18, 1)
c = 10**36 / 1
# ratio is guaranteed to be less than 1e18
# Also guaranteed to be limited, therefore can have all ops unsafe
ratio = 1
c = 1

Loading…
Cancel
Save