From b32d59ecf471fac9800be12fde37042dfc4b6963 Mon Sep 17 00:00:00 2001 From: Eric Kilmer Date: Fri, 30 Sep 2022 09:52:02 -0400 Subject: [PATCH] [Server] Use logger for messages --- server/manticore_server/evm_utils.py | 7 +++++- server/manticore_server/introspect_plugin.py | 5 +++- server/manticore_server/manticore_server.py | 26 ++++++++++++-------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/server/manticore_server/evm_utils.py b/server/manticore_server/evm_utils.py index 49fe065b..28ab06d6 100644 --- a/server/manticore_server/evm_utils.py +++ b/server/manticore_server/evm_utils.py @@ -1,4 +1,5 @@ import argparse +import logging import shlex from typing import List, Type @@ -29,6 +30,8 @@ from manticore.ethereum.plugins import ( ) from manticore.utils import config +logger = logging.getLogger(__name__) + def get_detectors_classes() -> List[Type[Detector]]: return [ @@ -189,6 +192,8 @@ def setup_detectors_flags( m.register_plugin(filter_nohuman_constants) if m.plugins: - print(f'Registered plugins: {", ".join(d.name for d in m.plugins.values())}') + logger.info( + f'Registered plugins: {", ".join(d.name for d in m.plugins.values())}' + ) return args diff --git a/server/manticore_server/introspect_plugin.py b/server/manticore_server/introspect_plugin.py index bd55de5b..74ae23f1 100644 --- a/server/manticore_server/introspect_plugin.py +++ b/server/manticore_server/introspect_plugin.py @@ -1,9 +1,12 @@ +import logging from datetime import datetime from manticore.core.plugin import IntrospectionAPIPlugin, StateDescriptor from manticore.core.state import StateBase from manticore.utils.enums import StateLists +logger = logging.getLogger(__name__) + class ManticoreServerIntrospectionPlugin(IntrospectionAPIPlugin): NAME = "ManticoreServerIntrospectionPlugin" @@ -39,4 +42,4 @@ class ManticoreServerIntrospectionPlugin(IntrospectionAPIPlugin): context[state.id].last_intermittent_update = datetime.now() def did_terminate_worker_callback(self, worker_id: int): - print(f"worker exits (id: {worker_id})") + logger.debug(f"worker exits (id: {worker_id})") diff --git a/server/manticore_server/manticore_server.py b/server/manticore_server/manticore_server.py index 9b015c29..8fd9b67b 100644 --- a/server/manticore_server/manticore_server.py +++ b/server/manticore_server/manticore_server.py @@ -37,6 +37,8 @@ from .ManticoreServer_pb2_grpc import ( from .native_plugin import UnicornEmulatePlugin from .native_utils import parse_native_arguments +logger = logging.getLogger(__name__) + class ManticoreWrapper: def __init__( @@ -107,7 +109,7 @@ class ManticoreServicer(ManticoreServerServicer): manticore_logger.addHandler(custom_log_handler) def log_callback(self, msg: str): - print(msg, end="") + logger.debug(msg) thread_name, msg_content = msg.split(" ", 1) if thread_name in self.manticore_instances: @@ -134,7 +136,7 @@ class ManticoreServicer(ManticoreServerServicer): try: parsed = parse_native_arguments(native_arguments.additional_mcore_args) except Exception as e: - print(e) + logger.debug(e) context.set_code(grpc.StatusCode.INVALID_ARGUMENT) context.set_details("Additional arguments could not be parsed!") return ManticoreInstance() @@ -163,7 +165,7 @@ class ManticoreServicer(ManticoreServerServicer): introspection_plugin_type=ManticoreServerIntrospectionPlugin, ) except Exception as e: - print(e) + logger.warning(e) context.set_code(grpc.StatusCode.INVALID_ARGUMENT) context.set_details("Basic arguments are invalid!") return ManticoreInstance() @@ -186,7 +188,7 @@ class ManticoreServicer(ManticoreServerServicer): def find_f(state: StateBase): bufs = state.solve_one_n_batched(state.input_symbols) for symbol, buf in zip(state.input_symbols, bufs): - print(f"{symbol.name}: {buf!r}\n") + logger.info(f"{symbol.name}: {buf!r}\n") with m.locked_context() as context: m.kill() state.abandon() @@ -205,7 +207,7 @@ class ManticoreServicer(ManticoreServerServicer): exec(hook.func_text, {"m": m}) except Exception as e: - print(e) + logger.warning(e) context.set_code(grpc.StatusCode.INVALID_ARGUMENT) context.set_details("Hooks set are invalid!") return ManticoreInstance() @@ -233,7 +235,7 @@ class ManticoreServicer(ManticoreServerServicer): manticore_wrapper.start() except Exception as e: - print(e) + logger.warning(e) context.set_code(grpc.StatusCode.INTERNAL) context.set_details( "Manticore failed to start or crashed during execution!" @@ -317,7 +319,7 @@ class ManticoreServicer(ManticoreServerServicer): manticore_wrapper.start() except Exception as e: - print(e) + logger.warning(e) context.set_code(grpc.StatusCode.INTERNAL) context.set_details( "Manticore failed to start or crashed during execution!" @@ -460,7 +462,7 @@ class ManticoreServicer(ManticoreServerServicer): warning_message = "WARNING: Not all Manticore processes were shut down successfully before timeout. There may be extra processes running even after the server has stopped." context.set_code(grpc.StatusCode.INTERNAL) context.set_details(warning_message) - print(warning_message) + logger.warning(warning_message) self.stop_event.set() return StopServerResponse() @@ -536,14 +538,18 @@ class ManticoreServicer(ManticoreServerServicer): def main(): + logger.setLevel(logging.DEBUG) + port = "[::]:50010" + logger.info(f"server starting at '{port}' ...") stop_event = Event() server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) add_ManticoreServerServicer_to_server(ManticoreServicer(stop_event), server) - server.add_insecure_port("[::]:50010") + server.add_insecure_port(port) server.start() + logger.info(f"server started.") stop_event.wait() server.stop(None) - print("shutdown gracefully!") + logger.info("shutdown gracefully!") if __name__ == "__main__":