Skip to content

Commit

Permalink
Merge pull request #780 from nbanmp/logging
Browse files Browse the repository at this point in the history
Switch to named loggers
  • Loading branch information
norhh authored Dec 12, 2018
2 parents 02dc627 + 312a2c8 commit 02a8ae9
Show file tree
Hide file tree
Showing 31 changed files with 188 additions and 143 deletions.
4 changes: 3 additions & 1 deletion mythril/analysis/modules/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
from typing import List

log = logging.getLogger(__name__)


class DetectionModule:
def __init__(
Expand All @@ -16,7 +18,7 @@ def __init__(
self.hooks = hooks
self.description = description
if entrypoint not in ("post", "callback"):
logging.error(
log.error(
"Invalid entrypoint in module %s, must be one of {post, callback}",
self.name,
)
Expand Down
1 change: 0 additions & 1 deletion mythril/analysis/modules/delegatecall.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from mythril.analysis.ops import get_variable, VarType
from mythril.analysis.report import Issue
from mythril.analysis.modules.base import DetectionModule
import logging


class DelegateCallModule(DetectionModule):
Expand Down
14 changes: 7 additions & 7 deletions mythril/analysis/modules/dependence_on_predictable_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from mythril.exceptions import UnsatError
import logging

log = logging.getLogger(__name__)


class PredictableDependenceModule(DetectionModule):
def __init__(self):
Expand All @@ -24,16 +26,14 @@ def __init__(self):

def execute(self, statespace):

logging.debug("Executing module: DEPENDENCE_ON_PREDICTABLE_VARS")
log.debug("Executing module: DEPENDENCE_ON_PREDICTABLE_VARS")

issues = []

for call in statespace.calls:

if "callvalue" in str(call.value):
logging.debug(
"[DEPENDENCE_ON_PREDICTABLE_VARS] Skipping refund function"
)
log.debug("[DEPENDENCE_ON_PREDICTABLE_VARS] Skipping refund function")
continue

# We're only interested in calls that send Ether
Expand Down Expand Up @@ -171,16 +171,16 @@ def execute(self, statespace):
def solve(self, call):
try:
model = solver.get_model(call.node.constraints)
logging.debug("[DEPENDENCE_ON_PREDICTABLE_VARS] MODEL: " + str(model))
log.debug("[DEPENDENCE_ON_PREDICTABLE_VARS] MODEL: " + str(model))
pretty_model = solver.pretty_print_model(model)

logging.debug(
log.debug(
"[DEPENDENCE_ON_PREDICTABLE_VARS] main model: \n%s" % pretty_model
)
return True

except UnsatError:
logging.debug("[DEPENDENCE_ON_PREDICTABLE_VARS] no model found")
log.debug("[DEPENDENCE_ON_PREDICTABLE_VARS] no model found")
return False


Expand Down
5 changes: 3 additions & 2 deletions mythril/analysis/modules/deprecated_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mythril.laser.ethereum.state.global_state import GlobalState
import logging

log = logging.getLogger(__name__)

DESCRIPTION = """
Check for usage of deprecated opcodes
Expand All @@ -15,7 +16,7 @@ def _analyze_state(state):
instruction = state.get_current_instruction()

if instruction["opcode"] == "ORIGIN":
logging.debug("ORIGIN in function " + node.function_name)
log.debug("ORIGIN in function " + node.function_name)
title = "Use of tx.origin"
description = (
"The function `{}` retrieves the transaction origin (tx.origin) using the ORIGIN opcode. "
Expand All @@ -27,7 +28,7 @@ def _analyze_state(state):
swc_id = DEPRICATED_FUNCTIONS_USAGE

elif instruction["opcode"] == "CALLCODE":
logging.debug("CALLCODE in function " + node.function_name)
log.debug("CALLCODE in function " + node.function_name)
title = "Use of callcode"
description = (
"The function `{}` uses callcode. Callcode does not persist sender or value over the call. "
Expand Down
4 changes: 3 additions & 1 deletion mythril/analysis/modules/ether_thief.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import logging
from copy import copy

log = logging.getLogger(__name__)

DESCRIPTION = """
Search for cases where Ether can be withdrawn to a user-specified address.
Expand Down Expand Up @@ -68,7 +70,7 @@ def _analyze_state(state):
gas_used=(state.mstate.min_gas_used, state.mstate.max_gas_used),
)
except UnsatError:
logging.debug("[ETHER_THIEF] no model found")
log.debug("[ETHER_THIEF] no model found")
return []

return [issue]
Expand Down
7 changes: 5 additions & 2 deletions mythril/analysis/modules/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import logging


log = logging.getLogger(__name__)


class ReachableExceptionsModule(DetectionModule):
def __init__(self):
super().__init__(
Expand All @@ -17,7 +20,7 @@ def __init__(self):

def execute(self, statespace):

logging.debug("Executing module: EXCEPTIONS")
log.debug("Executing module: EXCEPTIONS")

issues = []

Expand Down Expand Up @@ -65,7 +68,7 @@ def execute(self, statespace):
)

except UnsatError:
logging.debug("[EXCEPTIONS] no model found")
log.debug("[EXCEPTIONS] no model found")

return issues

Expand Down
6 changes: 4 additions & 2 deletions mythril/analysis/modules/external_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from mythril.exceptions import UnsatError
import logging

log = logging.getLogger(__name__)

DESCRIPTION = """
Search for low level calls (e.g. call.value()) that forward all gas to the callee.
Expand Down Expand Up @@ -58,7 +60,7 @@ def _analyze_state(state):

except UnsatError:

logging.debug(
log.debug(
"[EXTERNAL_CALLS] Callee address cannot be modified. Reporting informational issue."
)

Expand All @@ -82,7 +84,7 @@ def _analyze_state(state):
)

except UnsatError:
logging.debug("[EXTERNAL_CALLS] No model found.")
log.debug("[EXTERNAL_CALLS] No model found.")
return []

return [issue]
Expand Down
13 changes: 8 additions & 5 deletions mythril/analysis/modules/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import logging


log = logging.getLogger(__name__)


class IntegerOverflowUnderflowModule(DetectionModule):
def __init__(self):
super().__init__(
Expand All @@ -30,7 +33,7 @@ def execute(self, statespace):
:param statespace: Statespace to analyse
:return: Found issues
"""
logging.debug("Executing module: INTEGER")
log.debug("Executing module: INTEGER")

issues = []

Expand Down Expand Up @@ -89,7 +92,7 @@ def _check_integer_overflow(self, statespace, state, node):
model = self._try_constraints(node.constraints, [constraint])

if model is None:
logging.debug("[INTEGER_OVERFLOW] no model found")
log.debug("[INTEGER_OVERFLOW] no model found")
return issues

# Build issue
Expand Down Expand Up @@ -170,7 +173,7 @@ def _check_integer_underflow(self, statespace, state, node):
if type(op0) == int and type(op1) == int:
return []

logging.debug(
log.debug(
"[INTEGER_UNDERFLOW] Checking SUB {0}, {1} at address {2}".format(
str(op0), str(op1), str(instruction["address"])
)
Expand Down Expand Up @@ -215,7 +218,7 @@ def _check_integer_underflow(self, statespace, state, node):
issues.append(issue)

except UnsatError:
logging.debug("[INTEGER_UNDERFLOW] no model found")
log.debug("[INTEGER_UNDERFLOW] no model found")
return issues

def _check_usage(self, state, taint_result):
Expand Down Expand Up @@ -266,7 +269,7 @@ def _search_children(
if constraint is None:
constraint = []

logging.debug("SEARCHING NODE for usage of an overflowed variable %d", node.uid)
log.debug("SEARCHING NODE for usage of an overflowed variable %d", node.uid)

if taint_result is None:
state = node.states[index]
Expand Down
8 changes: 5 additions & 3 deletions mythril/analysis/modules/suicide.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
from mythril.laser.ethereum.state.global_state import GlobalState
import logging

log = logging.getLogger(__name__)

DESCRIPTION = """
Check if the contact can be 'accidentally' killed by anyone.
For kill-able contracts, also check whether it is possible to direct the contract balance to the attacker.
"""


def _analyze_state(state):
logging.info("Suicide module: Analyzing suicide instruction")
log.info("Suicide module: Analyzing suicide instruction")
node = state.node
instruction = state.get_current_instruction()
to = state.mstate.stack[-1]

logging.debug("[SUICIDE] SUICIDE in function " + node.function_name)
log.debug("[SUICIDE] SUICIDE in function " + node.function_name)

try:
try:
Expand Down Expand Up @@ -52,7 +54,7 @@ def _analyze_state(state):
)
return [issue]
except UnsatError:
logging.info("[UNCHECKED_SUICIDE] no model found")
log.info("[UNCHECKED_SUICIDE] no model found")

return []

Expand Down
5 changes: 4 additions & 1 deletion mythril/analysis/modules/transaction_order_dependence.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from mythril.exceptions import UnsatError


log = logging.getLogger(__name__)


class TxOrderDependenceModule(DetectionModule):
def __init__(self):
super().__init__(
Expand All @@ -26,7 +29,7 @@ def __init__(self):

def execute(self, statespace):
""" Executes the analysis module"""
logging.debug("Executing module: TOD")
log.debug("Executing module: TOD")

issues = []

Expand Down
4 changes: 3 additions & 1 deletion mythril/analysis/modules/unchecked_retval.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import logging
import re

log = logging.getLogger(__name__)


class UncheckedRetvalModule(DetectionModule):
def __init__(self):
Expand All @@ -26,7 +28,7 @@ def __init__(self):

def execute(self, statespace):

logging.debug("Executing module: UNCHECKED_RETVAL")
log.debug("Executing module: UNCHECKED_RETVAL")

issues = []

Expand Down
4 changes: 3 additions & 1 deletion mythril/analysis/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import _pysha3 as sha3
import hashlib

log = logging.getLogger(__name__)


class Issue:
def __init__(
Expand Down Expand Up @@ -39,7 +41,7 @@ def __init__(
keccak.update(bytes.fromhex(bytecode))
self.bytecode_hash = "0x" + keccak.hexdigest()
except ValueError:
logging.debug(
log.debug(
"Unable to change the bytecode to bytes. Bytecode: {}".format(bytecode)
)
self.bytecode_hash = ""
Expand Down
11 changes: 6 additions & 5 deletions mythril/analysis/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import importlib.util
import logging

log = logging.getLogger(__name__)

OPCODE_LIST = [c[0] for _, c in opcodes.items()]

Expand All @@ -27,7 +28,7 @@ def get_detection_module_hooks(modules):
for actual_hook in to_register:
hook_dict[actual_hook].append(module.detector.execute)
else:
logging.error(
log.error(
"Encountered invalid hook opcode %s in module %s",
op_code,
module.detector.name,
Expand All @@ -54,24 +55,24 @@ def get_detection_modules(entrypoint, include_modules=()):
if module.__name__ != "base" and module.detector.entrypoint == entrypoint:
_modules.append(module)

logging.info("Found %s detection modules", len(_modules))
log.info("Found %s detection modules", len(_modules))
return _modules


def fire_lasers(statespace, module_names=()):
logging.info("Starting analysis")
log.info("Starting analysis")

issues = []
for module in get_detection_modules(
entrypoint="post", include_modules=module_names
):
logging.info("Executing " + module.detector.name)
log.info("Executing " + module.detector.name)
issues += module.detector.execute(statespace)

for module in get_detection_modules(
entrypoint="callback", include_modules=module_names
):
logging.debug("Retrieving results for " + module.detector.name)
log.debug("Retrieving results for " + module.detector.name)
issues += module.detector.issues

reset_callback_modules()
Expand Down
4 changes: 3 additions & 1 deletion mythril/analysis/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
)
import logging

log = logging.getLogger(__name__)


def get_model(constraints, minimize=(), maximize=()):
s = Optimize()
Expand All @@ -27,7 +29,7 @@ def get_model(constraints, minimize=(), maximize=()):
if result == sat:
return s.model()
elif result == unknown:
logging.debug("Timeout encountered while solving expression using z3")
log.debug("Timeout encountered while solving expression using z3")
raise UnsatError


Expand Down
1 change: 0 additions & 1 deletion mythril/disassembler/disassembly.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from mythril.ethereum import util
from mythril.disassembler import asm
from mythril.support.signatures import SignatureDB
import logging


class Disassembly(object):
Expand Down
Loading

0 comments on commit 02a8ae9

Please sign in to comment.