Fix/natives (#1302)

* Fix precompiles

* Fix black

* Deal with TypeError
pull/1305/head
Nikhil Parasaram 5 years ago committed by GitHub
parent c322fd142e
commit dab4a8f69a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      mythril/laser/ethereum/call.py
  2. 6
      mythril/laser/ethereum/instruction_data.py
  3. 11
      mythril/laser/ethereum/natives.py

@ -10,7 +10,7 @@ import mythril.laser.ethereum.util as util
from mythril.laser.ethereum import natives
from mythril.laser.ethereum.instruction_data import calculate_native_gas
from mythril.laser.ethereum.state.account import Account
from mythril.laser.ethereum.natives import PRECOMPILE_COUNT
from mythril.laser.ethereum.natives import PRECOMPILE_COUNT, PRECOMPILE_FUNCTIONS
from mythril.laser.ethereum.state.calldata import (
BaseCalldata,
SymbolicCalldata,
@ -249,11 +249,10 @@ def native_call(
log.debug("CALL with symbolic start or offset not supported")
return [global_state]
contract_list = ["ecrecover", "sha256", "ripemd160", "identity"]
call_address_int = int(callee_address, 16)
native_gas_min, native_gas_max = calculate_native_gas(
global_state.mstate.calculate_extension_size(mem_out_start, mem_out_sz),
contract_list[call_address_int - 1],
PRECOMPILE_FUNCTIONS[call_address_int - 1].__name__,
)
global_state.mstate.min_gas_used += native_gas_min
global_state.mstate.max_gas_used += native_gas_max
@ -263,7 +262,11 @@ def native_call(
except natives.NativeContractException:
for i in range(mem_out_sz):
global_state.mstate.memory[mem_out_start + i] = global_state.new_bitvec(
contract_list[call_address_int - 1] + "(" + str(call_data) + ")", 8
PRECOMPILE_FUNCTIONS[call_address_int - 1].__name__
+ "("
+ str(call_data)
+ ")",
8,
)
return [global_state]

@ -199,7 +199,7 @@ def calculate_native_gas(size: int, contract: str):
:param contract:
:return:
"""
gas_value = None
gas_value = 0
word_num = ceil32(size) // 32
if contract == "ecrecover":
gas_value = opcodes.GECRECOVER
@ -210,7 +210,9 @@ def calculate_native_gas(size: int, contract: str):
elif contract == "identity":
gas_value = opcodes.GIDENTITYBASE + word_num * opcodes.GIDENTITYWORD
else:
raise ValueError("Unknown contract type {}".format(contract))
# TODO: Add gas for other precompiles, computation should be shifted to natives.py
# as some data isn't available here
pass
return gas_value, gas_value

@ -213,9 +213,10 @@ def native_contracts(address: int, data: BaseCalldata) -> List[int]:
:return:
"""
if isinstance(data, ConcreteCalldata):
concrete_data = data.concrete(None)
else:
if not isinstance(data, ConcreteCalldata):
raise NativeContractException()
return PRECOMPILE_FUNCTIONS[address - 1](concrete_data)
concrete_data = data.concrete(None)
try:
return PRECOMPILE_FUNCTIONS[address - 1](concrete_data)
except TypeError:
raise NativeContractException

Loading…
Cancel
Save