Exemple #1
0
    def _recursive_alpha_beta(self, depth, alpha, beta, end):
        if depth == 0 or self.final or depth <= self.depth:
            return self.best_value, self.final
        f = self.identities[self.hashes_stack[-1]][1]
        if not self.sorted_ident_moves:
            moves_dic = {}
            for move in self._get_moves():
                self.put(move)
                if self.identities_stack[-1] not in moves_dic:
                    moves_dic[
                        self.identities_stack[-1]] = self.trans[f]["func"](
                            *self.moves_stack[-1]), self.best_value
                self.undo()
            self.sorted_ident_moves = sc.SortedKeyList(
                moves_dic.values(), key=lambda x: -self.turn * x[1])
            self._save_cache()
        best_value, final = -self.turn * float("inf"), True
        new_moves = []
        for id_move, old_value in self.sorted_ident_moves:
            move = self.trans[self.ident_trans[f + "-1"]]["func"](*id_move)
            self.put(move)
            value, final_son = self._recursive_alpha_beta(
                depth - 1, alpha, beta, end)
            self.undo()
            if not final_son:
                final = False
            new_moves.append((id_move, value))
            if value * self.turn > best_value * self.turn:
                best_value = value
                if self.turn == 1:
                    alpha = max(best_value, alpha)
                else:
                    beta = min(best_value, beta)
                if alpha >= beta:
                    break
            if time.perf_counter() >= end:
                break

        if time.perf_counter() < end:
            self.final, self.depth, self.best_value = final, depth, best_value
            if alpha < beta:
                self.sorted_ident_moves = sc.SortedKeyList(
                    new_moves, key=lambda x: -self.turn * x[1])
            else:
                for old, new in zip(self.sorted_ident_moves, new_moves):
                    self.sorted_ident_moves.remove(old)
                    self.sorted_ident_moves.add(new)

            self._save_cache()

        return self.best_value, self.final
    def __init__(self, values, keyfunc, index=None):
        if index:
            assert len(values) == len(index)
        if index is None:
            index = range(len(values))
        sorted_key_list = [(val, ix) for ix, val in zip(index, values)]
        self.index = set(index)
        self.keyfunc = keyfunc

        try:
            self.sorted_key_list_2 = sortedcontainers.SortedKeyList(
                sorted_key_list, key=lambda t: keyfunc(t[0])
            )
        except TypeError as e:
            raise SortedKeyListException() from e
Exemple #3
0
 async def _fetch_dnddata(self):
     self._dnddata = self._dnddata = sortedcontainers.SortedKeyList(
         [], key=lambda i: i["name"].lower())
     async with aiohttp.ClientSession() as session:
         async with session.get(
                 "https://5e.tools/data/items.json") as response:
             j = await response.json()
             for item in j["item"]:
                 self._dnddata.add(item)
         async with session.get(
                 "https://5e.tools/data/fluff-items.json") as response:
             j = await response.json()
             for item in j["itemFluff"]:
                 self._dnddata.add(item)
         async with session.get(
                 "https://5e.tools/data/items-base.json") as response:
             j = await response.json()
             for item in j["baseitem"]:
                 self._dnddata.add(item)
     self._test_all()
Exemple #4
0
 async def _fetch_dnddata(self):
     self._dnddata = self._dnddata = sortedcontainers.SortedKeyList([], key=lambda i: i["name"].lower())
     async with aiohttp.ClientSession() as session:
         for url in [
             "https://5e.tools/data/spells/spells-ai.json",
             "https://5e.tools/data/spells/spells-ggr.json",
             "https://5e.tools/data/spells/spells-phb.json",
             "https://5e.tools/data/spells/spells-scag.json",
             "https://5e.tools/data/spells/spells-xge.json",
             "https://5e.tools/data/spells/spells-ua-frw.json",
             "https://5e.tools/data/spells/spells-stream.json",
             "https://5e.tools/data/spells/spells-llk.json",
             "https://5e.tools/data/spells/spells-ua-saw.json",
             "https://5e.tools/data/spells/spells-ua-mm.json",
             "https://5e.tools/data/spells/spells-ua-ss.json",
             "https://5e.tools/data/spells/spells-ua-tobm.json",
             "https://5e.tools/data/spells/spells-ua-ar.json",
         ]:
             async with session.get(url) as response:
                 j = await response.json()
                 for spell in j["spell"]:
                     self._dnddata.add(spell)
     self._test_all()
Exemple #5
0
    def __init__(self,
                 binary,
                 binary_stream,
                 loader=None,
                 is_main_bin=False,
                 entry_point=None,
                 arch=None,
                 base_addr=None,
                 force_rebase=False,
                 has_memory=True,
                 **kwargs):
        """
        :param binary:          The path to the binary to load
        :param binary_stream:   The open stream to this binary. The reference to this will be held until you call close.
        :param is_main_bin:     Whether this binary should be loaded as the main executable
        """
        self.binary = binary
        self._binary_stream = binary_stream
        if self.binary is not None:
            self.binary_basename = os.path.basename(self.binary)
        elif hasattr(self._binary_stream, "name"):
            self.binary_basename = os.path.basename(self._binary_stream.name)
        else:
            self.binary_basename = str(self._binary_stream)

        for k in list(kwargs.keys()):
            if k == 'custom_entry_point':
                entry_point = kwargs.pop(k)
            elif k == 'custom_arch':
                arch = kwargs.pop(k)
            elif k == 'custom_base_addr':
                base_addr = kwargs.pop(k)
            else:
                continue
            l.critical(
                "Deprecation warning: the %s parameter has been renamed to %s",
                k, k[7:])

        if kwargs != {}:
            l.warning("Unused kwargs for loading binary %s: %s", self.binary,
                      ', '.join(kwargs.keys()))

        self.is_main_bin = is_main_bin
        self.has_memory = has_memory
        self.loader = loader
        self._entry = 0
        self._segments = Regions()  # List of segments
        self._sections = Regions()  # List of sections
        self.sections_map = {}  # Mapping from section name to section
        self.symbols = sortedcontainers.SortedKeyList(
            key=self._get_symbol_relative_addr)
        self.imports = {}
        self.resolved_imports = []
        self.relocs = []
        self.irelatives = [
        ]  # list of tuples (resolver, destination), dest w/o rebase
        self.jmprel = {}
        self.arch = None
        self.os = None  # Let other stuff override this
        self.compiler = None, None  # compiler name, version
        self._symbol_cache = {}
        # a list of directories to search for libraries specified by the object
        self.extra_load_path = []
        # attributes to enable SimProcedure guessing
        self.guess_simprocs = False
        self.guess_simprocs_hint = None

        # checksums
        self.md5 = None
        self.sha256 = None

        self.mapped_base_symbolic = 0
        # These are set by cle, and should not be overriden manually
        self.mapped_base = self.linked_base = 0  # not to be set manually - used by CLE

        self.deps = []  # Needed shared objects (libraries dependencies)
        self.child_objects = []  # any objects loaded directly out of this
        self.parent_object = None
        self.linking = None  # Dynamic or static linking
        self.pic = force_rebase
        self.execstack = False

        # tls info set by backend to communicate with thread manager
        self.tls_used = False
        self.tls_block_size = None
        self.tls_data_size = None
        self.tls_data_start = None
        # tls info set by thread manager
        self.tls_module_id = None
        #self.tls_block_offset = None  # this is an ELF-only attribute

        # exception handling
        # they should be rebased when .rebase() is called
        self.exception_handlings = []  # type: List[ExceptionHandling]

        # Hints
        # they should be rebased when .rebase() is called
        self.function_hints = []  # type: List[FunctionHint]

        # Custom options
        self._custom_entry_point = entry_point
        self._custom_base_addr = base_addr
        self.provides = os.path.basename(
            self.binary) if self.binary is not None else None

        self.memory = None  # type: Clemory

        # should be set inside `cle.Loader.add_object`
        self._is_mapped = False
        # cached max_addr
        self._max_addr = None
        # cached last section
        self._last_section = None
        # cached last segment
        self._last_segment = None

        if arch is None:
            self.arch = None
        elif isinstance(arch, str):
            self.set_arch(archinfo.arch_from_id(arch))
        elif isinstance(arch, archinfo.Arch):
            self.set_arch(arch)
        elif isinstance(arch, type) and issubclass(arch, archinfo.Arch):
            self.set_arch(arch())
        else:
            raise CLEError("Bad parameter: arch=%s" % arch)

        self._checksum()
Exemple #6
0
 def __setstate__(self, state):
     state['symbols'] = sortedcontainers.SortedKeyList(
         state['symbols'], key=self._get_symbol_relative_addr)
     self.__dict__.update(state)
     for sym in self.symbols:
         sym.owner = self
Exemple #7
0
    def __init__(self,
                 binary,
                 loader=None,
                 is_main_bin=False,
                 filename=None,
                 entry_point=None,
                 arch=None,
                 base_addr=None,
                 force_rebase=False,
                 has_memory=True,
                 **kwargs):
        """
        :param binary:          The path to the binary to load
        :param is_main_bin:     Whether this binary should be loaded as the main executable
        """
        if hasattr(binary, 'seek') and hasattr(binary, 'read'):
            self.binary = filename
            self.binary_stream = binary
        else:
            self.binary = binary
            try:
                self.binary_stream = open(binary, 'rb')
            except IOError:
                self.binary_stream = None

        for k in list(kwargs.keys()):
            if k == 'custom_entry_point':
                entry_point = kwargs.pop(k)
            elif k == 'custom_arch':
                arch = kwargs.pop(k)
            elif k == 'custom_base_addr':
                base_addr = kwargs.pop(k)
            else:
                continue
            l.critical(
                "Deprecation warning: the %s parameter has been renamed to %s",
                k, k[7:])

        if kwargs != {}:
            l.warning("Unused kwargs for loading binary %s: %s", self.binary,
                      ', '.join(kwargs.keys()))

        self.is_main_bin = is_main_bin
        self.has_memory = has_memory
        self.loader = loader
        self._entry = None
        self._segments = Regions()  # List of segments
        self._sections = Regions()  # List of sections
        self.sections_map = {}  # Mapping from section name to section
        self.symbols = sortedcontainers.SortedKeyList(
            key=self._get_symbol_relative_addr)
        self.imports = {}
        self.resolved_imports = []
        self.relocs = []
        self.irelatives = [
        ]  # list of tuples (resolver, destination), dest w/o rebase
        self.jmprel = {}
        self.arch = None
        self.os = None  # Let other stuff override this
        self.engine_preset = None
        self._symbol_cache = {}
        # a list of directories to search for libraries specified by the object
        self.extra_load_path = []
        # attributes to enable SimProcedure guessing
        self.guess_simprocs = False
        self.guess_simprocs_hint = None

        self.mapped_base_symbolic = 0
        # These are set by cle, and should not be overriden manually
        self.mapped_base = self.linked_base = 0  # not to be set manually - used by CLE

        self.deps = []  # Needed shared objects (libraries dependencies)
        self.linking = None  # Dynamic or static linking
        self.pic = force_rebase
        self.execstack = False

        # Custom options
        self._custom_entry_point = entry_point
        self._custom_base_addr = base_addr
        self.provides = os.path.basename(
            self.binary) if self.binary is not None else None

        self.memory = None

        # should be set inside `cle.Loader.add_object`
        self._is_mapped = False
        # cached max_addr
        self._max_addr = None

        if arch is None:
            self.arch = None
        elif isinstance(arch, str):
            self.set_arch(archinfo.arch_from_id(arch))
        elif isinstance(arch, archinfo.Arch):
            self.set_arch(arch)
        elif isinstance(arch, type) and issubclass(arch, archinfo.Arch):
            self.set_arch(arch())
        else:
            raise CLEError("Bad parameter: arch=%s" % arch)