Merge branch 'dev' into dev-types

pull/1388/head
Josselin Feist 2 years ago
commit bad83348b0
  1. 2
      .github/workflows/features.yml
  2. 8
      Dockerfile
  3. 6
      slither/core/expressions/literal.py
  4. 2
      slither/core/solidity_types/array_type.py
  5. 3
      slither/slithir/variables/constant.py
  6. 2
      slither/solc_parsing/declarations/modifier.py
  7. 9
      slither/utils/arithmetic.py
  8. 23
      slither/utils/integer_conversion.py
  9. 14
      slither/visitors/expression/constants_folding.py
  10. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.0-compact.zip
  11. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.0-legacy.zip
  12. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.1-compact.zip
  13. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.1-legacy.zip
  14. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.2-compact.zip
  15. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.2-legacy.zip
  16. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.3-compact.zip
  17. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.3-legacy.zip
  18. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.4-compact.zip
  19. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.4-legacy.zip
  20. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.5-compact.zip
  21. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.5-legacy.zip
  22. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.6-compact.zip
  23. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.7.6-legacy.zip
  24. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.0-compact.zip
  25. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.1-compact.zip
  26. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.10-compact.zip
  27. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.11-compact.zip
  28. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.12-compact.zip
  29. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.13-compact.zip
  30. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.14-compact.zip
  31. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.15-compact.zip
  32. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.2-compact.zip
  33. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.3-compact.zip
  34. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.4-compact.zip
  35. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.5-compact.zip
  36. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.6-compact.zip
  37. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.7-compact.zip
  38. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.8-compact.zip
  39. BIN
      tests/ast-parsing/compile/modifier-0.7.0.sol-0.8.9-compact.zip
  40. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.0-compact.zip
  41. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.0-legacy.zip
  42. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.1-compact.zip
  43. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.1-legacy.zip
  44. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.10-compact.zip
  45. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.10-legacy.zip
  46. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.11-compact.zip
  47. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.11-legacy.zip
  48. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.12-compact.zip
  49. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.12-legacy.zip
  50. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.13-compact.zip
  51. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.13-legacy.zip
  52. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.14-compact.zip
  53. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.14-legacy.zip
  54. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.15-compact.zip
  55. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.15-legacy.zip
  56. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.16-compact.zip
  57. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.16-legacy.zip
  58. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.17-compact.zip
  59. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.17-legacy.zip
  60. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.18-compact.zip
  61. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.18-legacy.zip
  62. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.19-compact.zip
  63. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.19-legacy.zip
  64. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.2-compact.zip
  65. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.2-legacy.zip
  66. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.20-compact.zip
  67. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.20-legacy.zip
  68. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.21-compact.zip
  69. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.21-legacy.zip
  70. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.22-compact.zip
  71. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.22-legacy.zip
  72. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.23-compact.zip
  73. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.23-legacy.zip
  74. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.24-compact.zip
  75. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.24-legacy.zip
  76. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.25-compact.zip
  77. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.25-legacy.zip
  78. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.26-compact.zip
  79. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.26-legacy.zip
  80. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.3-compact.zip
  81. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.3-legacy.zip
  82. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.4-compact.zip
  83. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.4-legacy.zip
  84. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.5-compact.zip
  85. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.5-legacy.zip
  86. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.6-compact.zip
  87. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.6-legacy.zip
  88. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.7-compact.zip
  89. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.7-legacy.zip
  90. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.8-compact.zip
  91. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.8-legacy.zip
  92. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.9-compact.zip
  93. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.4.9-legacy.zip
  94. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.5.0-compact.zip
  95. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.5.0-legacy.zip
  96. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.5.1-compact.zip
  97. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.5.1-legacy.zip
  98. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.5.10-compact.zip
  99. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.5.10-legacy.zip
  100. BIN
      tests/ast-parsing/compile/modifier-all.sol-0.5.11-compact.zip
  101. Some files were not shown because too many files have changed in this diff Show More

@ -45,7 +45,7 @@ jobs:
- name: Test with pytest - name: Test with pytest
run: | run: |
pytest tests/test_features.py pytest tests/test_features.py
pytest tests/test_constant_folding_unary.py pytest tests/test_constant_folding.py
pytest tests/slithir/test_ternary_expressions.py pytest tests/slithir/test_ternary_expressions.py
pytest tests/test_functions_ids.py pytest tests/test_functions_ids.py
pytest tests/test_function.py pytest tests/test_function.py

@ -26,6 +26,14 @@ RUN export DEBIAN_FRONTEND=noninteractive \
&& apt-get install -y --no-install-recommends python3-pip \ && apt-get install -y --no-install-recommends python3-pip \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
# improve compatibility with amd64 solc in non-amd64 environments (e.g. Docker Desktop on M1 Mac)
ENV QEMU_LD_PREFIX=/usr/x86_64-linux-gnu
RUN if [ ! "$(uname -m)" = "x86_64" ]; then \
export DEBIAN_FRONTEND=noninteractive \
&& apt-get update \
&& apt-get install -y --no-install-recommends libc6-amd64-cross \
&& rm -rf /var/lib/apt/lists/*; fi
RUN useradd -m slither RUN useradd -m slither
USER slither USER slither

@ -1,7 +1,9 @@
from typing import Optional, Union, TYPE_CHECKING from typing import Optional, Union, TYPE_CHECKING
from slither.core.expressions.expression import Expression from slither.core.expressions.expression import Expression
from slither.core.solidity_types.elementary_type import Fixed, Int, Ufixed, Uint
from slither.utils.arithmetic import convert_subdenomination from slither.utils.arithmetic import convert_subdenomination
from slither.utils.integer_conversion import convert_string_to_int
if TYPE_CHECKING: if TYPE_CHECKING:
from slither.core.solidity_types.type import Type from slither.core.solidity_types.type import Type
@ -29,6 +31,10 @@ class Literal(Expression):
def __str__(self): def __str__(self):
if self.subdenomination: if self.subdenomination:
return str(convert_subdenomination(self._value, self.subdenomination)) return str(convert_subdenomination(self._value, self.subdenomination))
if self.type in Int + Uint + Fixed + Ufixed + ["address"]:
return str(convert_string_to_int(self._value))
# be sure to handle any character # be sure to handle any character
return str(self._value) return str(self._value)

@ -53,7 +53,7 @@ class ArrayType(Type):
def storage_size(self) -> Tuple[int, bool]: def storage_size(self) -> Tuple[int, bool]:
if self._length_value: if self._length_value:
elem_size, _ = self._type.storage_size elem_size, _ = self._type.storage_size
return elem_size * int(self._length_value.value), True return elem_size * int(str(self._length_value)), True
return 32, True return 32, True
def __str__(self): def __str__(self):

@ -1,4 +1,3 @@
from decimal import Decimal
from functools import total_ordering from functools import total_ordering
from slither.core.solidity_types.elementary_type import ElementaryType, Int, Uint from slither.core.solidity_types.elementary_type import ElementaryType, Int, Uint
@ -33,7 +32,7 @@ class Constant(SlithIRVariable):
else: else:
if val.isdigit(): if val.isdigit():
self._type = ElementaryType("uint256") self._type = ElementaryType("uint256")
self._val = int(Decimal(val)) self._val = convert_string_to_int(val)
else: else:
self._type = ElementaryType("string") self._type = ElementaryType("string")
self._val = val self._val = val

@ -62,7 +62,7 @@ class ModifierSolc(FunctionSolc):
self._content_was_analyzed = True self._content_was_analyzed = True
if self.is_compact_ast: if self.is_compact_ast:
body = self._functionNotParsed["body"] body = self._functionNotParsed.get("body", None)
if body and body[self.get_key()] == "Block": if body and body[self.get_key()] == "Block":
self._function.is_implemented = True self._function.is_implemented = True

@ -1,17 +1,12 @@
from decimal import Decimal
from slither.exceptions import SlitherException from slither.exceptions import SlitherException
from slither.utils.integer_conversion import convert_string_to_fraction
# pylint: disable=too-many-branches # pylint: disable=too-many-branches
def convert_subdenomination( def convert_subdenomination(
value: str, sub: str value: str, sub: str
) -> int: # pylint: disable=too-many-return-statements ) -> int: # pylint: disable=too-many-return-statements
# to allow 0.1 ether conversion decimal_value = convert_string_to_fraction(value)
if value[0:2] == "0x":
decimal_value = Decimal(int(value, 16))
else:
decimal_value = Decimal(value)
if sub == "wei": if sub == "wei":
return int(decimal_value) return int(decimal_value)
if sub == "gwei": if sub == "gwei":

@ -1,28 +1,35 @@
from decimal import Decimal from fractions import Fraction
from typing import Union from typing import Union
from slither.exceptions import SlitherError from slither.exceptions import SlitherError
def convert_string_to_int(val: Union[str, int]) -> int: def convert_string_to_fraction(val: Union[str, int]) -> Fraction:
if isinstance(val, int): if isinstance(val, int):
return val return Fraction(val)
if val.startswith(("0x", "0X")): if val.startswith(("0x", "0X")):
return int(val, 16) return Fraction(int(val, 16))
# Fractions do not support underscore separators (on Python <3.11)
val = val.replace("_", "")
if "e" in val or "E" in val: if "e" in val or "E" in val:
base, expo = val.split("e") if "e" in val else val.split("E") base, expo = val.split("e") if "e" in val else val.split("E")
base, expo = Decimal(base), int(expo) base, expo = Fraction(base), int(expo)
# The resulting number must be < 2**256-1, otherwise solc # The resulting number must be < 2**256-1, otherwise solc
# Would not be able to compile it # Would not be able to compile it
# 10**77 is the largest exponent that fits # 10**77 is the largest exponent that fits
# See https://github.com/ethereum/solidity/blob/9e61f92bd4d19b430cb8cb26f1c7cf79f1dff380/libsolidity/ast/Types.cpp#L1281-L1290 # See https://github.com/ethereum/solidity/blob/9e61f92bd4d19b430cb8cb26f1c7cf79f1dff380/libsolidity/ast/Types.cpp#L1281-L1290
if expo > 77: if expo > 77:
if base != Decimal(0): if base != Fraction(0):
raise SlitherError( raise SlitherError(
f"{base}e{expo} is too large to fit in any Solidity integer size" f"{base}e{expo} is too large to fit in any Solidity integer size"
) )
return 0 return 0
return int(Decimal(base) * Decimal(10**expo)) return Fraction(base) * Fraction(10**expo)
return Fraction(val)
return int(Decimal(val))
def convert_string_to_int(val: Union[str, int]) -> int:
return int(convert_string_to_fraction(val))

@ -1,5 +1,5 @@
from slither.core.expressions import BinaryOperationType, Literal, UnaryOperationType from slither.core.expressions import BinaryOperationType, Literal, UnaryOperationType
from slither.utils.integer_conversion import convert_string_to_int from slither.utils.integer_conversion import convert_string_to_fraction, convert_string_to_int
from slither.visitors.expression.expression import ExpressionVisitor from slither.visitors.expression.expression import ExpressionVisitor
@ -72,15 +72,15 @@ class ConstantFolding(ExpressionVisitor):
cf = ConstantFolding(expr, self._type) cf = ConstantFolding(expr, self._type)
expr = cf.result() expr = cf.result()
assert isinstance(expr, Literal) assert isinstance(expr, Literal)
set_val(expression, int(expr.value)) set_val(expression, -convert_string_to_fraction(expr.value))
else: else:
raise NotConstant raise NotConstant
def _post_literal(self, expression): def _post_literal(self, expression):
if expression.value.isdigit(): try:
set_val(expression, int(expression.value)) set_val(expression, convert_string_to_fraction(expression.value))
else: except ValueError as e:
raise NotConstant raise NotConstant from e
def _post_assignement_operation(self, expression): def _post_assignement_operation(self, expression):
raise NotConstant raise NotConstant
@ -115,7 +115,7 @@ class ConstantFolding(ExpressionVisitor):
cf = ConstantFolding(expression.expressions[0], self._type) cf = ConstantFolding(expression.expressions[0], self._type)
expr = cf.result() expr = cf.result()
assert isinstance(expr, Literal) assert isinstance(expr, Literal)
set_val(expression, int(expr.value)) set_val(expression, convert_string_to_fraction(expr.value))
return return
raise NotConstant raise NotConstant

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save