def __init__(self, configuration, create_emulator, create_target): self._configuration = configuration self._plugins = [] self._terminating = Event() self._call_proxy = EmulatorTargetCallProxy() self._emulator = None self._target = None self._listeners = [] self._events = Queue() #Setup logging to console # logging.basicConfig(level = logging.DEBUG, format = CONSOLE_LOG_FORMAT) # console_log_handler = logging.StreamHandler() # console_log_handler.setLevel(logging.DEBUG) # console_log_handler.setFormatter(logging.Formatter(CONSOLE_LOG_FORMAT)) # logging.getLogger("").addHandler(console_log_handler) # logging.getLogger("").setLevel(logging.DEBUG) #Check if output directory exists, and create it if not output_directory = configuration["output_directory"] if os.path.exists( output_directory) and not os.path.isdir(output_directory): log.error("Output destination exists, but is not a directory") sys.exit(1) if not os.path.exists(output_directory): log.info("Output directory did not exist, trying to create it") mkdir_p(output_directory) if os.listdir(output_directory): log.warn("Output directory is not empty, will overwrite files") #Now the output directory should exist, divert a logging stream there file_log_handler = logging.FileHandler(filename=os.path.join( output_directory, "avatar.log"), mode='w') file_log_handler.setLevel(logging.DEBUG) file_log_handler.setFormatter(logging.Formatter(FILE_LOG_FORMAT)) logging.getLogger("").addHandler(file_log_handler) # console_log_handler.setLevel(logging.INFO) self._event_thread = Thread(target=self._process_events) self._event_thread.start() create_emulator(self) create_target(self)
def __init__(self, configuration, create_emulator, create_target): self._configuration = configuration self._plugins = [] self._terminating = Event() self._call_proxy = EmulatorTargetCallProxy() self._emulator = None self._target = None self._listeners = [] self._events = Queue() #Setup logging to console # logging.basicConfig(level = logging.DEBUG, format = CONSOLE_LOG_FORMAT) console_log_handler = logging.StreamHandler() console_log_handler.setLevel(logging.DEBUG) console_log_handler.setFormatter(logging.Formatter(CONSOLE_LOG_FORMAT)) logging.getLogger("").addHandler(console_log_handler) logging.getLogger("").setLevel(logging.DEBUG) #Check if output directory exists, and create it if not output_directory = configuration["output_directory"] if os.path.exists(output_directory) and not os.path.isdir(output_directory): log.error("Output destination exists, but is not a directory") sys.exit(1) if not os.path.exists(output_directory): log.info("Output directory did not exist, trying to create it") mkdir_p(output_directory) if os.listdir(output_directory): log.warn("Output directory is not empty, will overwrite files") #Now the output directory should exist, divert a logging stream there file_log_handler = logging.FileHandler(filename = os.path.join(output_directory, "avatar.log"), mode = 'w') file_log_handler.setLevel(logging.DEBUG) file_log_handler.setFormatter(logging.Formatter(FILE_LOG_FORMAT)) logging.getLogger("").addHandler(file_log_handler) console_log_handler.setLevel(logging.INFO) self._event_thread = Thread(target = self._process_events) self._event_thread.start() create_emulator(self) create_target(self)
def __init__(self, user_settings=None, options=[]): assert(System.instance is None), \ "Only one instance of System is allowed, use getInstance()" assert(isinstance(user_settings, dict)), \ "User_settings parameter for System class constructor must be a dict" assert(isinstance(options, list)), \ "Options parameter for System class constructor must be a list" for key in options: if "--debug" in options : self._debug = True if "--trace" in options : self._trace = True self.is_initialized = False System.instance = self self._emulator = None self._target = None try: self._configuration = ConfigurationFactory.createParser(user_settings) self._target = TargetsFactory.create(self._configuration) self._emulator = EmulatorsFactory.create(self._configuration) Log.activeLog(self._configuration.getOutputDirectory()) except (ValueError, AssertionError, OSError, NotImplementedError) as e: log.critical("Fail create : %s \r\n" % e) sys.exit(0) AvatarSignal.handle() self._events = EventsDispatcher() self._call_proxy = EmulatorTargetCallProxy() self._started = True
class System(object): """ Entry point of Avatar Framework Setup the entire process according to user configuration Singleton Design Pattern to enforce only one instance of System per process """ instance = None @staticmethod def getInstance(): if System.instance is None : System.instance = System() return System.instance def is_debug(self): return self._debug def is_trace(self): return self._trace def __init__(self, user_settings=None, options=[]): assert(System.instance is None), \ "Only one instance of System is allowed, use getInstance()" assert(isinstance(user_settings, dict)), \ "User_settings parameter for System class constructor must be a dict" assert(isinstance(options, list)), \ "Options parameter for System class constructor must be a list" for key in options: if "--debug" in options : self._debug = True if "--trace" in options : self._trace = True self.is_initialized = False System.instance = self self._emulator = None self._target = None try: self._configuration = ConfigurationFactory.createParser(user_settings) self._target = TargetsFactory.create(self._configuration) self._emulator = EmulatorsFactory.create(self._configuration) Log.activeLog(self._configuration.getOutputDirectory()) except (ValueError, AssertionError, OSError, NotImplementedError) as e: log.critical("Fail create : %s \r\n" % e) sys.exit(0) AvatarSignal.handle() self._events = EventsDispatcher() self._call_proxy = EmulatorTargetCallProxy() self._started = True def _init(self): assert(self._emulator), "The emulator configuration stage may not have been successfully achieved" assert(self._target), "The target configuration stage may not have been successfully achieved" try : self._emulator.init() log.info("\r\nAnalyzer initialized : %s\r\n" % str(self._emulator)) self._target.init() log.info("\r\nTarget initialized : %s\r\n" % str(self._target)) self.is_initialized = True except ConnectionRefusedError as e: log.critical("Fail to init : \r\n"+str(e)+"\r\n") self.stop() def start(self): assert(self._emulator), "Emulator is not initialized" assert(self._target), "Target is not initialized" assert(self._events) if self.is_initialized == False : self._init() try: self._events.start() log.info("Event dispatcher started") self._emulator.set_read_request_handler(self._call_proxy.handle_emulator_read_request) self._emulator.set_write_request_handler(self._call_proxy.handle_emulator_write_request) self._emulator.set_set_cpu_state_request_handler(self._call_proxy.handle_emulator_set_cpu_state_request) self._emulator.set_get_cpu_state_request_handler(self._call_proxy.handle_emulator_get_cpu_state_request) self._emulator.set_continue_request_handler(self._call_proxy.handle_emulator_continue_request) self._emulator.set_get_checksum_request_handler(self._call_proxy.handle_emulator_get_checksum_request) self._call_proxy.set_target(self._target) log.info("Hook installed") self._target.start() log.info("Target started") self._emulator.start() log.info("Emulator started") self._started = True except (FileNotFoundError, ConnectionRefusedError) as e : log.critical("Fail to start : \r\n"+e+"\r\n") self.stop() nn = NameNormalizer() info = {"Version" : "2.0", "compatibility": "1/2", "Testing Support" : "S2E/Klee", "Debugger" : "SuperspeeedJTag/OpenOCD/GDB"} log.info("\r\nAvatar : The Dynamic Analysis Framework for Embedded Systems V2.0 : \r\n %s \r\n" % format_table([(nn.normalize_name(n), info[n]) for n in info])) def stop(self): self._events.stop() self._emulator.stop() self._target.stop() self._started = False time.sleep(5) sys.exit(0) """ Proxy API """ def add_monitor(self, monitor): self._call_proxy.add_monitor(monitor) """ EventsDispatcher API """ def register_event_listener(self, listener): self._events.append(listener) def unregister_event_listener(self, listener): self._events.remove(listener) def post_event(self, evt): self._events.put(evt) def get_emulator(self): return self._emulator def get_target(self): return self._target
class System(): def __init__(self, configuration, create_emulator, create_target): self._configuration = configuration self._plugins = [] self._terminating = Event() self._call_proxy = EmulatorTargetCallProxy() self._emulator = None self._target = None self._listeners = [] self._events = Queue() #Setup logging to console # logging.basicConfig(level = logging.DEBUG, format = CONSOLE_LOG_FORMAT) # console_log_handler = logging.StreamHandler() # console_log_handler.setLevel(logging.DEBUG) # console_log_handler.setFormatter(logging.Formatter(CONSOLE_LOG_FORMAT)) # logging.getLogger("").addHandler(console_log_handler) # logging.getLogger("").setLevel(logging.DEBUG) #Check if output directory exists, and create it if not output_directory = configuration["output_directory"] if os.path.exists( output_directory) and not os.path.isdir(output_directory): log.error("Output destination exists, but is not a directory") sys.exit(1) if not os.path.exists(output_directory): log.info("Output directory did not exist, trying to create it") mkdir_p(output_directory) if os.listdir(output_directory): log.warn("Output directory is not empty, will overwrite files") #Now the output directory should exist, divert a logging stream there file_log_handler = logging.FileHandler(filename=os.path.join( output_directory, "avatar.log"), mode='w') file_log_handler.setLevel(logging.DEBUG) file_log_handler.setFormatter(logging.Formatter(FILE_LOG_FORMAT)) logging.getLogger("").addHandler(file_log_handler) # console_log_handler.setLevel(logging.INFO) self._event_thread = Thread(target=self._process_events) self._event_thread.start() create_emulator(self) create_target(self) def get_configuration(self): return self._configuration def init(self): self._emulator.init() self._target.init() def start(self): assert (self._emulator) #Start emulator hook needs to be set! assert (self._target) #Start target hook needs to be set! self._emulator.set_read_request_handler( self._call_proxy.handle_emulator_read_request) self._emulator.set_write_request_handler( self._call_proxy.handle_emulator_write_request) self._emulator.set_set_cpu_state_request_handler( self._call_proxy.handle_emulator_set_cpu_state_request) self._emulator.set_get_cpu_state_request_handler( self._call_proxy.handle_emulator_get_cpu_state_request) self._emulator.set_continue_request_handler( self._call_proxy.handle_emulator_continue_request) self._emulator.set_get_checksum_request_handler( self._call_proxy.handle_emulator_get_checksum_request) self._call_proxy.set_target(self._target) self._target.start() self._emulator.start() def stop(self): if not self._terminating.is_set(): self._terminating.set() self._emulator.stop() self._target.stop() self._call_proxy.stop_monitors() def set_emulator(self, emulator): """This method is supposed to be called by the emulator init function when it sets the actual emulator object. The _emulator variable should not be set directly, as some decoration might happen.""" self._emulator = emulator def set_target(self, target): """This method is supposed to be called by the target init function when it sets the actual target object. The _target variable should not be set directly, as some decoration might happen.""" self._target = target def get_emulator(self): return self._emulator def get_target(self): return self._target def add_monitor(self, monitor): self._call_proxy.add_monitor(monitor) def post_event(self, evt): if not "properties" in evt: evt["properties"] = {} self._events.put(evt) def register_event_listener(self, listener): self._listeners.append(listener) def unregister_event_listener(self, listener): self._listeners.remove(listener) def _process_events(self): while not self._terminating.is_set(): try: evt = self._events.get(1) log.debug("Processing event: '%s'", str(evt)) for listener in self._listeners: try: listener(evt) except Exception: log.exception("Exception while handling event") except Empty: pass except Exception as ex: log.exception( "Some more serious exception handled while processing events. Investigate." ) raise ex
class System(): def __init__(self, configuration, create_emulator, create_target): self._configuration = configuration self._plugins = [] self._terminating = Event() self._call_proxy = EmulatorTargetCallProxy() self._emulator = None self._target = None self._listeners = [] self._events = Queue() #Setup logging to console # logging.basicConfig(level = logging.DEBUG, format = CONSOLE_LOG_FORMAT) console_log_handler = logging.StreamHandler() console_log_handler.setLevel(logging.DEBUG) console_log_handler.setFormatter(logging.Formatter(CONSOLE_LOG_FORMAT)) logging.getLogger("").addHandler(console_log_handler) logging.getLogger("").setLevel(logging.DEBUG) #Check if output directory exists, and create it if not output_directory = configuration["output_directory"] if os.path.exists(output_directory) and not os.path.isdir(output_directory): log.error("Output destination exists, but is not a directory") sys.exit(1) if not os.path.exists(output_directory): log.info("Output directory did not exist, trying to create it") mkdir_p(output_directory) if os.listdir(output_directory): log.warn("Output directory is not empty, will overwrite files") #Now the output directory should exist, divert a logging stream there file_log_handler = logging.FileHandler(filename = os.path.join(output_directory, "avatar.log"), mode = 'w') file_log_handler.setLevel(logging.DEBUG) file_log_handler.setFormatter(logging.Formatter(FILE_LOG_FORMAT)) logging.getLogger("").addHandler(file_log_handler) console_log_handler.setLevel(logging.INFO) self._event_thread = Thread(target = self._process_events) self._event_thread.start() create_emulator(self) create_target(self) def get_configuration(self): return self._configuration def init(self): self._emulator.init() self._target.init() def start(self): assert(self._emulator) #Start emulator hook needs to be set! assert(self._target) #Start target hook needs to be set! self._emulator.set_read_request_handler(self._call_proxy.handle_emulator_read_request) self._emulator.set_write_request_handler(self._call_proxy.handle_emulator_write_request) self._call_proxy.set_target(self._target) self._target.start() self._emulator.start() def stop(self): if not self._terminating.is_set(): self._terminating.set() self._emulator.stop() self._target.stop() self._call_proxy.stop_monitors() def set_emulator(self, emulator): """This method is supposed to be called by the emulator init function when it sets the actual emulator object. The _emulator variable should not be set directly, as some decoration might happen.""" self._emulator = emulator def set_target(self, target): """This method is supposed to be called by the target init function when it sets the actual target object. The _target variable should not be set directly, as some decoration might happen.""" self._target = target def get_emulator(self): return self._emulator def get_target(self): return self._target def add_monitor(self, monitor): self._call_proxy.add_monitor(monitor) def post_event(self, evt): if not "properties" in evt: evt["properties"] = {} self._events.put(evt) def register_event_listener(self, listener): self._listeners.append(listener) def unregister_event_listener(self, listener): self._listeners.remove(listener) def _process_events(self): while not self._terminating.is_set(): try: evt = self._events.get(1) log.debug("Processing event: '%s'", str(evt)) for listener in self._listeners: try: listener(evt) except Exception: log.exception("Exception while handling event") except Empty: pass except Exception as ex: log.exception("Some more serious exception handled while processing events. Investigate.") raise ex