Source code for pyvex.enums

from typing import Any

import cffi

from .native import ffi, pvc
from .utils import stable_hash


[docs] class VEXObject: """ The base class for Vex types. """ __slots__: list[str] = [] def __eq__(self, other): if not isinstance(other, type(self)): return False # compare values in slots for slot in self.__slots__: if getattr(self, slot) != getattr(other, slot): return False return True def __hash__(self): values = [getattr(self, slot) for slot in self.__slots__] for i, lst_val in enumerate(values): if isinstance(lst_val, list): values[i] = tuple(lst_val) return stable_hash(tuple([type(self)] + values))
def _cstr(c_char_p) -> str: """Decode a C string; ffi.string() is typed as returning bytes or str.""" value = ffi.string(c_char_p) return value.decode() if isinstance(value, bytes) else value
[docs] class IRCallee(VEXObject): """ Describes a helper function to call. """ __slots__ = ["regparms", "name", "mcx_mask"]
[docs] def __init__(self, regparms, name, mcx_mask): VEXObject.__init__(self) self.regparms = regparms self.name = name self.mcx_mask = mcx_mask
def __str__(self): return str(self.name) @staticmethod def _from_c(c_callee): return IRCallee( c_callee.regparms, _cstr(c_callee.name), # NO. #int(ffi.cast("unsigned long long", c_callee.addr)), c_callee.mcx_mask, ) @staticmethod def _to_c(callee): # pylint: disable=unused-argument raise TypeError( "This doesn't work! Please invent a way to get the correct address for the named function from pyvex_c." )
# c_callee = pvc.mkIRCallee(callee.regparms, # callee.name.encode(), # ffi.cast("void *", callee.addr)) # c_callee.mcx_mask = callee.mcx_mask # return c_callee
[docs] class IRRegArray(VEXObject): """ A section of the guest state that we want te be able to index at run time, so as to be able to describe indexed or rotating register files on the guest. :ivar int base: The offset into the state that this array starts :ivar str elemTy: The types of the elements in this array, as VEX enum strings :ivar int nElems: The number of elements in this array """ __slots__ = ["base", "elemTy", "nElems"]
[docs] def __init__(self, base, elemTy, nElems): VEXObject.__init__(self) self.base = base self.elemTy = elemTy self.nElems = nElems
def __str__(self): return "%s:%sx%d" % (self.base, self.elemTy[4:], self.nElems) @staticmethod def _from_c(c_arr): return IRRegArray(c_arr.base, get_enum_from_int(c_arr.elemTy, "IRType"), c_arr.nElems) @staticmethod def _to_c(arr): return pvc.mkIRRegArray(arr.base, get_int_from_enum(arr.elemTy), arr.nElems)
ints_to_enums: dict[int, str] = {} enums_to_ints: dict[str, int] = {} irop_enums_to_ints: dict[str, int] = {} will_be_overwritten = ["Ircr_GT", "Ircr_LT", "IRICB_vbit", "IRICB_iropt", "Iop_FIRST_EVEX"] # Per-enum-type int -> name tables, built from cffi's own view of each C enum. # Unlike the flat ints_to_enums map, these cannot suffer cross-enum integer # collisions (e.g. Ircr_GT vs Irrm_NEAREST, both 0), so lookups made at a site # that knows which C enum a value belongs to should use these. The "IROp" # table additionally folds in the members of the separate IROp_EVEX enum # (AVX-512 ops), which continue IROp's integer space. type_ints_to_enums: dict[str, dict[int, str]] = {} # Enum member names that are range markers rather than real members; they # alias real members' integer values and lose any name conflict. _SENTINEL_NAMES = frozenset(["Iop_FIRST_EVEX", "Iop_LAST_NOT_EVEX", "Iop_LAST"]) def _build_type_tables(): typedef_names, _, _ = ffi.list_types() enum_types = [] for name in typedef_names: try: t = ffi.typeof(name) except (cffi.CDefError, cffi.FFIError): continue if t.kind == "enum": enum_types.append((name, t)) for name, t in enum_types: table = type_ints_to_enums.setdefault(name, {}) for member, value in sorted(t.relements.items()): if value in table and member in _SENTINEL_NAMES: continue if value in table and table[value] not in _SENTINEL_NAMES: continue # first (alphabetically) real name wins table[value] = member # IROp_EVEX continues the IROp integer space; expose one op namespace. if "IROp_EVEX" in type_ints_to_enums: evex = type_ints_to_enums.pop("IROp_EVEX") irop = type_ints_to_enums["IROp"] for value, member in evex.items(): if value not in irop: irop[value] = member
[docs] def get_enum_from_int(i, enum_type=None): """ Translate a VEX enum value to its name. :param enum_type: The name of the C enum type ``i`` belongs to (e.g. ``"IROp"``, ``"IRType"``). When given, the lookup uses that enum's own table and cannot be confused by another enum reusing the same integer. When omitted, the legacy flat table is used. """ if enum_type is not None: return type_ints_to_enums[enum_type][i] return ints_to_enums[i]
[docs] def get_int_from_enum(e): return enums_to_ints[e]
_add_enum_counter = 0 def _add_enum(s, i=None): # TODO get rid of this global _add_enum_counter # pylint: disable=global-statement if i is None: while _add_enum_counter in ints_to_enums: _add_enum_counter += 1 i = _add_enum_counter _add_enum_counter += 1 # Update for the next iteration if i in ints_to_enums: prev = ints_to_enums[i] is_op, prev_is_op = s.startswith("Iop_"), prev.startswith("Iop_") if prev not in will_be_overwritten: if s in _SENTINEL_NAMES or prev in _SENTINEL_NAMES: pass # range markers may alias real members; ignore elif is_op != prev_is_op: # The EVEX ops continue IROp's integer space past 0x1900 and so # overlap IRExprTag. Ops are always looked up through the # per-type tables, so the flat table keeps the tag name. pass else: raise ValueError("Enum with intkey %d already present" % i) if s in _SENTINEL_NAMES or (is_op and not prev_is_op): # Keep the already-registered name in the flat table. enums_to_ints[s] = i if is_op: irop_enums_to_ints[s] = i return if prev_is_op and not is_op: enums_to_ints[s] = i ints_to_enums[i] = s return enums_to_ints[s] = i ints_to_enums[i] = s if s.startswith("Iop_"): irop_enums_to_ints[s] = i for attr in dir(pvc): if attr[0] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and hasattr(pvc, attr) and isinstance(getattr(pvc, attr), int): _add_enum(attr, getattr(pvc, attr)) _build_type_tables()
[docs] def vex_endness_from_string(endness_str): return getattr(pvc, endness_str)
[docs] def default_vex_archinfo() -> dict[str, Any]: return { "hwcaps": 0, "endness": vex_endness_from_string("VexEndnessLE"), "ppc_icache_line_szB": 0, "ppc_dcbz_szB": 0, "ppc_dcbzl_szB": 0, "arm64_dMinLine_lg2_szB": 0, "arm64_iMinLine_lg2_szB": 0, "hwcache_info": { "num_levels": 0, "num_caches": 0, "caches": None, "icaches_maintain_coherence": True, }, "x86_cr0": 0xFFFFFFFF, }