Display func name in CFG, add example

pull/88/head
Bernhard Mueller 7 years ago
parent c4984cce08
commit e2eee11218
  1. 43
      mythril/analysis/callgraph.py
  2. 33
      solidity_examples/calls.sol

@ -1,4 +1,5 @@
from z3 import Z3Exception, simplify from z3 import Z3Exception, simplify
from laser.ethereum.svm import NodeFlags
import re import re
@ -117,7 +118,7 @@ colors = [
"{border: '#9e42b3', background: '#842899', highlight: {border: '#9e42b3', background: '#933da6'}}", "{border: '#9e42b3', background: '#842899', highlight: {border: '#9e42b3', background: '#933da6'}}",
"{border: '#b82323', background: '#991d1d', highlight: {border: '#b82323', background: '#a61f1f'}}", "{border: '#b82323', background: '#991d1d', highlight: {border: '#b82323', background: '#a61f1f'}}",
"{border: '#4753bf', background: '#3b46a1', highlight: {border: '#4753bf', background: '#424db3'}}", "{border: '#4753bf', background: '#3b46a1', highlight: {border: '#4753bf', background: '#424db3'}}",
] ]
def serialize(statespace, color_map): def serialize(statespace, color_map):
@ -127,47 +128,51 @@ def serialize(statespace, color_map):
for node_key in statespace.nodes: for node_key in statespace.nodes:
code = statespace.nodes[node_key].get_cfg_dict()['code'] node = statespace.nodes[node_key]
code = re.sub("([0-9a-f]{8})[0-9a-f]+", lambda m: m.group(1) + "(...)", code) code = node.get_cfg_dict()['code']
code = re.sub("([0-9a-f]{8})[0-9a-f]+", lambda m: m.group(1) + "(...)", code)
if NodeFlags.FUNC_ENTRY in node.flags:
code = re.sub("JUMPDEST", "%d %s" % (node.start_addr, node.function_name), code)
code_split = code.split("\\n") code_split = code.split("\\n")
truncated_code = code if (len(code_split) < 7) else "\\n".join(code_split[:6]) + "\\n(click to expand +)" truncated_code = code if (len(code_split) < 7) else "\\n".join(code_split[:6]) + "\\n(click to expand +)"
color = color_map[statespace.nodes[node_key].get_cfg_dict()['contract_name']] color = color_map[node.get_cfg_dict()['contract_name']]
nodes.append("{id: '" + str(node_key) + "', color: " + color + ", size: 150, 'label': '" + truncated_code + "', 'fullLabel': '" + code + "', 'truncLabel': '" + truncated_code + "', 'isExpanded': false}") nodes.append("{id: '" + str(node_key) + "', color: " + color + ", size: 150, 'label': '" + truncated_code + "', 'fullLabel': '" + code + "', 'truncLabel': '" + truncated_code + "', 'isExpanded': false}")
for edge in statespace.edges: for edge in statespace.edges:
if (edge.condition is None): if (edge.condition is None):
label = "" label = ""
else: else:
try: try:
label = str(simplify(edge.condition)).replace("\n", "") label = str(simplify(edge.condition)).replace("\n", "")
except Z3Exception: except Z3Exception:
label = str(edge.condition).replace("\n", "") label = str(edge.condition).replace("\n", "")
label = re.sub("([^_])([\d]{2}\d+)", lambda m: m.group(1) + hex(int(m.group(2))), label)
code = re.sub("([0-9a-f]{8})[0-9a-f]+", lambda m: m.group(1) + "(...)", code)
edges.append("{from: '" + str(edge.as_dict()['from']) + "', to: '" + str(edge.as_dict()['to']) + "', 'arrows': 'to', 'label': '" + label + "', 'smooth': {'type': 'cubicBezier'}}") label = re.sub("([^_])([\d]{2}\d+)", lambda m: m.group(1) + hex(int(m.group(2))), label)
code = re.sub("([0-9a-f]{8})[0-9a-f]+", lambda m: m.group(1) + "(...)", code)
return "var nodes = [\n" + ",\n".join(nodes) + "\n];\nvar edges = [\n" + ",\n".join(edges) + "\n];" edges.append("{from: '" + str(edge.as_dict()['from']) + "', to: '" + str(edge.as_dict()['to']) + "', 'arrows': 'to', 'label': '" + label + "', 'smooth': {'type': 'cubicBezier'}}")
return "var nodes = [\n" + ",\n".join(nodes) + "\n];\nvar edges = [\n" + ",\n".join(edges) + "\n];"
def generate_graph(statespace, physics = False): def generate_graph(statespace, physics=False):
i = 0 i = 0
color_map = {} color_map = {}
for k in statespace.accounts: for k in statespace.accounts:
color_map[statespace.accounts[k].contract_name] = colors[i] color_map[statespace.accounts[k].contract_name] = colors[i]
i += 1 i += 1
html = graph_html.replace("[JS]", serialize(statespace, color_map)) html = graph_html.replace("[JS]", serialize(statespace, color_map))
html = html.replace("[ENABLE_PHYSICS]", str(physics).lower()) html = html.replace("[ENABLE_PHYSICS]", str(physics).lower())

@ -0,0 +1,33 @@
pragma solidity ^0.4.17;
contract Callee {
function theFunction() payable {
}
}
contract Caller {
address public fixed_address;
address public stored_address;
function Caller(address addr) {
fixed_address = addr;
}
function thisisfine() public {
Callee(fixed_address).theFunction();
}
function calluseraddress(address addr) {
addr.call();
}
function callstoredaddress() {
stored_address.call();
}
function setstoredaddress(address addr) {
stored_address = addr;
}
}
Loading…
Cancel
Save