Skip to content

Commit

Permalink
Emit profiling.bin in workspace directory (#1539)
Browse files Browse the repository at this point in the history
* Move profiling.bin into workspace directory

* Move profiling data dumping to plugin

Reliant on manticore.kill, so requires explicit call to finalize (should be fine from the CLI)

* Remove extraneous os.path
  • Loading branch information
artemdinaburg authored and Eric Hennenfent committed Nov 7, 2019
1 parent eb44222 commit 561c3ee
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions manticore/core/manticore.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def newFunction(self, *args, **kw):
"terminate_state",
"kill_state",
"execute_instruction",
"terminate_execution",
}

def __init__(self, initial_state, workspace_url=None, policy="random", **kwargs):
Expand Down Expand Up @@ -849,8 +850,10 @@ def kill(self):
Workers must terminate
RUNNING, STANDBY -> KILLED
"""
self._publish("will_terminate_execution", self._output)
self._killed.value = True
self._lock.notify_all()
self._publish("did_terminate_execution", self._output)

def terminate(self):
logger.warning("manticore.terminate is deprecated (Use manticore.kill)")
Expand Down
4 changes: 4 additions & 0 deletions manticore/core/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ def did_terminate_worker_callback(self, id):
with self.manticore.locked_context("_profiling_stats", dict) as profiling_stats:
profiling_stats[id] = self.data.profile.stats.items()

def did_terminate_execution_callback(self, output):
with output.save_stream("profiling.bin", binary=True) as f:
self.save_profiling_data(f)

def get_profiling_data(self):
class PstatsFormatted:
def __init__(self, d):
Expand Down
4 changes: 0 additions & 4 deletions manticore/ethereum/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ def ethereum_main(args, logger):
else:
m.kill()

if consts.profile:
with open("profiling.bin", "wb") as f:
profiler.save_profiling_data(f)

for detector in list(m.detectors):
m.unregister_detector(detector)

Expand Down
17 changes: 14 additions & 3 deletions tests/native/test_manticore.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
import os
import logging
import filecmp

from manticore.native import Manticore
from manticore.utils.log import get_verbosity, set_verbosity
Expand All @@ -18,15 +19,23 @@ def setUp(self):

def test_profiling_data(self):
p = Profiler()
self.m.verbosity(0)
set_verbosity(0)
self.m.register_plugin(p)
self.m.run()
self.m.finalize()
profile_path = os.path.join(self.m.workspace, "profiling.bin")
with open(profile_path, "wb") as f:
p.save_profiling_data(f)
self.assertTrue(os.path.exists(profile_path))
self.assertTrue(os.path.getsize(profile_path) > 0)

profile_path_2 = os.path.join(self.m.workspace, "profiling_2.bin")
with open(profile_path_2, "wb") as f:
p.save_profiling_data(f)

self.assertTrue(os.path.exists(profile_path_2))
self.assertTrue(os.path.getsize(profile_path_2) > 0)

self.assertTrue(filecmp.cmp(profile_path, profile_path_2))

def test_add_hook(self):
def tmp(state):
pass
Expand Down Expand Up @@ -118,3 +127,5 @@ def test_logging(self):
set_verbosity(1)
self.assertEqual(get_verbosity("manticore.native.cpu.abstractcpu"), logging.WARNING)
self.assertEqual(get_verbosity("manticore.ethereum.abi"), logging.INFO)

set_verbosity(0)

0 comments on commit 561c3ee

Please sign in to comment.