# Path: sibyl/commons.py
# def init_logger(name):
#     logger = logging.getLogger(name)
# 
#     console_handler = logging.StreamHandler()
#     log_format = "%(levelname)-5s: %(message)s"
#     console_handler.setFormatter(logging.Formatter(log_format))
#     logger.addHandler(console_handler)
# 
#     logger.setLevel(logging.ERROR)
#     return logger
# 
# class TimeoutException(Exception):
#     """Exception to be called on timeouts"""
#     pass
# 
# END_ADDR = 0x1337babe
# 
# Path: sibyl/engine/qemu.py
# class QEMUEngine(Engine):
#     """Engine based on QEMU, using unicorn as a wrapper"""
# 
#     def __init__(self, machine):
#         if unicorn is None:
#             raise ImportError("QEMU engine unavailable: 'unicorn' import error")
# 
#         self.jitter = UcWrapJitter(machine)
#         super(QEMUEngine, self).__init__(machine)
# 
# 
#     def run(self, address, timeout_seconds):
#         try:
#             self.jitter.run(address, timeout_seconds)
#         except UnexpectedStopException as _:
#             return False
#         except Exception as error:
#             self.logger.exception(error)
#             return False
# 
#         return True
# 
#     def prepare_run(self):
#         # XXX HACK: Avoid a slow down of unicorn (apparently to too many
#         # threads...)
#         self.jitter.renew()
# 
#     def restore_snapshot(self, memory=True):
#         # Restore VM
#         if memory:
#             self.jitter.vm.restore_mem_state(self.vm_mem)
# 
#         # Restore registers
#         self.jitter.cpu.set_gpreg(self.vm_regs)
# 
# Path: sibyl/engine/miasm.py
# class MiasmEngine(Engine):
#     """Engine based on Miasm"""
# 
#     def __init__(self, machine, jit_engine):
#         jitter = machine.jitter(jit_engine)
#         jitter.set_breakpoint(END_ADDR, MiasmEngine._code_sentinelle)
#         self.jitter = jitter
# 
#         # Signal handling
#         #
#         # Due to Python signal handling implementation, signals aren't handled
#         # nor passed to Jitted code in case of registration with signal API
#         if jit_engine == "python":
#             signal.signal(signal.SIGALRM, MiasmEngine._timeout)
#         elif jit_engine in ["llvm", "tcc", "gcc"]:
#             self.jitter.vm.set_alarm()
#         else:
#             raise ValueError("Unknown engine: %s" % jit_engine)
# 
#         super(MiasmEngine, self).__init__(machine)
# 
# 
#     @staticmethod
#     def _code_sentinelle(jitter):
#         jitter.run = False
#         jitter.pc = 0
#         return True
# 
#     @staticmethod
#     def _timeout(signum, frame):
#         raise TimeoutException()
# 
#     def run(self, address, timeout_seconds):
#         self.jitter.init_run(address)
# 
#         try:
#             signal.alarm(timeout_seconds)
#             self.jitter.continue_run()
#         except (AssertionError, RuntimeError, ValueError,
#                 KeyError, IndexError, TimeoutException) as _:
#             return False
#         except Exception as error:
#             self.logger.exception(error)
#             return False
#         finally:
#             signal.alarm(0)
# 
#         return True
# 
#     def restore_snapshot(self, memory=True):
#         # Restore memory
#         if memory:
#             self.jitter.vm.reset_memory_page_pool()
#             self.jitter.vm.reset_code_bloc_pool()
#             for addr, metadata in self.vm_mem.iteritems():
#                 self.jitter.vm.add_memory_page(addr,
#                                                metadata["access"],
#                                                metadata["data"])
# 
#         # Restore registers
#         self.jitter.cpu.init_regs()
#         self.jitter.cpu.set_gpreg(self.vm_regs)
# 
#         # Reset intern elements
#         self.jitter.vm.set_exception(0)
#         self.jitter.cpu.set_exception(0)
#         self.jitter.bs._atomic_mode = False
# 
# Path: sibyl/config.py
# class Config(object):
#     def __init__(self, default_config, files):
#     def expandpath(path):
#     def parse_files(self, files):
#     def dump(self):
#     def jit_engine(self):
#     def available_tests(self):
#     def miasm_engine(self):
#     def pin_root(self):
#     def pin_tracer(self):
#     def prune_strategy(self):
#     def prune_keep(self):
#     def prune_keep_max(self):
#     def stubs(self):
#     def idaq64_path(self):
#     def ghidra_headless_path(self):
#     def ghidra_export_function(self):


import time
import signal
import logging
from miasm2.analysis.binary import Container, ContainerPE, ContainerELF
from sibyl.commons import init_logger, TimeoutException, END_ADDR
from sibyl.engine import QEMUEngine, MiasmEngine
from sibyl.config import config
            from miasm2.jitter.loader.pe import preload_pe, libimp_pe
            from miasm2.jitter.loader.elf import preload_elf, libimp_elf
        if isinstance(self.ctr, ContainerPE):
            libs = libimp_pe()
            preload_pe(self.jitter.vm, self.ctr.executable, libs)

        elif isinstance(self.ctr, ContainerELF):
            libs = libimp_elf()
            preload_elf(self.jitter.vm, self.ctr.executable, libs)

        else:
            return

        # Add associated breakpoints
        self.jitter.add_lib_handler(libs, context)

    def initialize_tests(self, tests_cls):
        tests = []
        for testcls in tests_cls:
            tests.append(testcls(self.jitter, self.abi))
        self.tests = tests

    def load_vm(self, filename, map_addr):
        self.ctr = Container.from_stream(open(filename), vm=self.jitter.vm,
                                         addr=map_addr)
        self.jitter.cpu.init_regs()
        self.jitter.init_stack()

    def init_engine(self, engine_name):
        if engine_name == "qemu":
            self.engine = QEMUEngine(self.machine)
        else:

# Path: sibyl/commons.py
# def init_logger(name):
#     logger = logging.getLogger(name)
# 
#     console_handler = logging.StreamHandler()
#     log_format = "%(levelname)-5s: %(message)s"
#     console_handler.setFormatter(logging.Formatter(log_format))
#     logger.addHandler(console_handler)
# 
#     logger.setLevel(logging.ERROR)
#     return logger
# 
# class TimeoutException(Exception):
#     """Exception to be called on timeouts"""
#     pass
# 
# END_ADDR = 0x1337babe
# 
# Path: sibyl/engine/qemu.py
# class QEMUEngine(Engine):
#     """Engine based on QEMU, using unicorn as a wrapper"""
# 
#     def __init__(self, machine):
#         if unicorn is None:
#             raise ImportError("QEMU engine unavailable: 'unicorn' import error")
# 
#         self.jitter = UcWrapJitter(machine)
#         super(QEMUEngine, self).__init__(machine)
# 
# 
#     def run(self, address, timeout_seconds):
#         try:
#             self.jitter.run(address, timeout_seconds)
#         except UnexpectedStopException as _:
#             return False
#         except Exception as error:
#             self.logger.exception(error)
#             return False
# 
#         return True
# 
#     def prepare_run(self):
#         # XXX HACK: Avoid a slow down of unicorn (apparently to too many
#         # threads...)
#         self.jitter.renew()
# 
#     def restore_snapshot(self, memory=True):
#         # Restore VM
#         if memory:
#             self.jitter.vm.restore_mem_state(self.vm_mem)
# 
#         # Restore registers
#         self.jitter.cpu.set_gpreg(self.vm_regs)
# 
# Path: sibyl/engine/miasm.py
# class MiasmEngine(Engine):
#     """Engine based on Miasm"""
# 
#     def __init__(self, machine, jit_engine):
#         jitter = machine.jitter(jit_engine)
#         jitter.set_breakpoint(END_ADDR, MiasmEngine._code_sentinelle)
#         self.jitter = jitter
# 
#         # Signal handling
#         #
#         # Due to Python signal handling implementation, signals aren't handled
#         # nor passed to Jitted code in case of registration with signal API
#         if jit_engine == "python":
#             signal.signal(signal.SIGALRM, MiasmEngine._timeout)
#         elif jit_engine in ["llvm", "tcc", "gcc"]:
#             self.jitter.vm.set_alarm()
#         else:
#             raise ValueError("Unknown engine: %s" % jit_engine)
# 
#         super(MiasmEngine, self).__init__(machine)
# 
# 
#     @staticmethod
#     def _code_sentinelle(jitter):
#         jitter.run = False
#         jitter.pc = 0
#         return True
# 
#     @staticmethod
#     def _timeout(signum, frame):
#         raise TimeoutException()
# 
#     def run(self, address, timeout_seconds):
#         self.jitter.init_run(address)
# 
#         try:
#             signal.alarm(timeout_seconds)
#             self.jitter.continue_run()
#         except (AssertionError, RuntimeError, ValueError,
#                 KeyError, IndexError, TimeoutException) as _:
#             return False
#         except Exception as error:
#             self.logger.exception(error)
#             return False
#         finally:
#             signal.alarm(0)
# 
#         return True
# 
#     def restore_snapshot(self, memory=True):
#         # Restore memory
#         if memory:
#             self.jitter.vm.reset_memory_page_pool()
#             self.jitter.vm.reset_code_bloc_pool()
#             for addr, metadata in self.vm_mem.iteritems():
#                 self.jitter.vm.add_memory_page(addr,
#                                                metadata["access"],
#                                                metadata["data"])
# 
#         # Restore registers
#         self.jitter.cpu.init_regs()
#         self.jitter.cpu.set_gpreg(self.vm_regs)
# 
#         # Reset intern elements
#         self.jitter.vm.set_exception(0)
#         self.jitter.cpu.set_exception(0)
#         self.jitter.bs._atomic_mode = False
# 
# Path: sibyl/config.py
# class Config(object):
#     def __init__(self, default_config, files):
#     def expandpath(path):
#     def parse_files(self, files):
#     def dump(self):
#     def jit_engine(self):
#     def available_tests(self):
#     def miasm_engine(self):
#     def pin_root(self):
#     def pin_tracer(self):
#     def prune_strategy(self):
#     def prune_keep(self):
#     def prune_keep_max(self):
#     def stubs(self):
#     def idaq64_path(self):
#     def ghidra_headless_path(self):
#     def ghidra_export_function(self):


# Path: sibyl/testlauncher.py
import time
import signal
import logging
from miasm2.analysis.binary import Container, ContainerPE, ContainerELF
from sibyl.commons import init_logger, TimeoutException, END_ADDR
from sibyl.engine import QEMUEngine, MiasmEngine
from sibyl.config import config
            from miasm2.jitter.loader.pe import preload_pe, libimp_pe
            from miasm2.jitter.loader.elf import preload_elf, libimp_elf

        if isinstance(self.ctr, ContainerPE):
            libs = libimp_pe()
            preload_pe(self.jitter.vm, self.ctr.executable, libs)

        elif isinstance(self.ctr, ContainerELF):
            libs = libimp_elf()
            preload_elf(self.jitter.vm, self.ctr.executable, libs)

        else:
            return

        # Add associated breakpoints
        self.jitter.add_lib_handler(libs, context)

    def initialize_tests(self, tests_cls):
        tests = []
        for testcls in tests_cls:
            tests.append(testcls(self.jitter, self.abi))
        self.tests = tests

    def load_vm(self, filename, map_addr):
        self.ctr = Container.from_stream(open(filename), vm=self.jitter.vm,
                                         addr=map_addr)
        self.jitter.cpu.init_regs()
        self.jitter.init_stack()

    def init_engine(self, engine_name):
        if engine_name == "qemu":
            self.engine = QEMUEngine(self.machine)
        else:

            self.engine = MiasmEngine(self.machine, engine_name)