Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/pyvex/enums.py: 88%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

77 statements  

1from typing import Any, Dict, List 

2 

3from .native import ffi, pvc 

4from .utils import stable_hash 

5 

6 

7class VEXObject: 

8 """ 

9 The base class for Vex types. 

10 """ 

11 

12 __slots__: List[str] = [] 

13 

14 def __eq__(self, other): 

15 if not isinstance(other, type(self)): 

16 return False 

17 # compare values in slots 

18 for slot in self.__slots__: 

19 if getattr(self, slot) != getattr(other, slot): 

20 return False 

21 return True 

22 

23 def __hash__(self): 

24 values = [getattr(self, slot) for slot in self.__slots__] 

25 for i, lst_val in enumerate(values): 

26 if isinstance(lst_val, list): 

27 values[i] = tuple(lst_val) 

28 return stable_hash(tuple([type(self)] + values)) 

29 

30 

31class IRCallee(VEXObject): 

32 """ 

33 Describes a helper function to call. 

34 """ 

35 

36 __slots__ = ["regparms", "name", "mcx_mask"] 

37 

38 def __init__(self, regparms, name, mcx_mask): 

39 VEXObject.__init__(self) 

40 self.regparms = regparms 

41 self.name = name 

42 self.mcx_mask = mcx_mask 

43 

44 def __str__(self): 

45 return str(self.name) 

46 

47 @staticmethod 

48 def _from_c(c_callee): 

49 return IRCallee( 

50 c_callee.regparms, 

51 ffi.string(c_callee.name).decode(), 

52 # NO. #int(ffi.cast("unsigned long long", c_callee.addr)), 

53 c_callee.mcx_mask, 

54 ) 

55 

56 @staticmethod 

57 def _to_c(callee): # pylint: disable=unused-argument 

58 raise TypeError( 

59 "This doesn't work! Please invent a way to get the correct address for the named function from pyvex_c." 

60 ) 

61 # c_callee = pvc.mkIRCallee(callee.regparms, 

62 # callee.name.encode(), 

63 # ffi.cast("void *", callee.addr)) 

64 # c_callee.mcx_mask = callee.mcx_mask 

65 # return c_callee 

66 

67 

68class IRRegArray(VEXObject): 

69 """ 

70 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 

71 rotating register files on the guest. 

72 

73 :ivar int base: The offset into the state that this array starts 

74 :ivar str elemTy: The types of the elements in this array, as VEX enum strings 

75 :ivar int nElems: The number of elements in this array 

76 """ 

77 

78 __slots__ = ["base", "elemTy", "nElems"] 

79 

80 def __init__(self, base, elemTy, nElems): 

81 VEXObject.__init__(self) 

82 self.base = base 

83 self.elemTy = elemTy 

84 self.nElems = nElems 

85 

86 def __str__(self): 

87 return "%s:%sx%d" % (self.base, self.elemTy[4:], self.nElems) 

88 

89 @staticmethod 

90 def _from_c(c_arr): 

91 return IRRegArray(c_arr.base, ints_to_enums[c_arr.elemTy], c_arr.nElems) 

92 

93 @staticmethod 

94 def _to_c(arr): 

95 return pvc.mkIRRegArray(arr.base, get_int_from_enum(arr.elemTy), arr.nElems) 

96 

97 

98ints_to_enums: Dict[int, str] = {} 

99enums_to_ints: Dict[str, int] = {} 

100irop_enums_to_ints: Dict[str, int] = {} 

101will_be_overwritten = ["Ircr_GT", "Ircr_LT"] 

102 

103 

104def get_enum_from_int(i): 

105 return ints_to_enums[i] 

106 

107 

108def get_int_from_enum(e): 

109 return enums_to_ints[e] 

110 

111 

112_add_enum_counter = 0 

113 

114 

115def _add_enum(s, i=None): # TODO get rid of this 

116 global _add_enum_counter # pylint: disable=global-statement 

117 if i is None: 

118 while _add_enum_counter in ints_to_enums: 

119 _add_enum_counter += 1 

120 i = _add_enum_counter 

121 _add_enum_counter += 1 # Update for the next iteration 

122 if i in ints_to_enums: 

123 if ints_to_enums[i] not in will_be_overwritten: 

124 raise ValueError("Enum with intkey %d already present" % i) 

125 enums_to_ints[s] = i 

126 ints_to_enums[i] = s 

127 if s.startswith("Iop_"): 

128 irop_enums_to_ints[s] = i 

129 

130 

131for attr in dir(pvc): 

132 if attr[0] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and hasattr(pvc, attr) and isinstance(getattr(pvc, attr), int): 

133 _add_enum(attr, getattr(pvc, attr)) 

134 

135 

136def vex_endness_from_string(endness_str): 

137 return getattr(pvc, endness_str) 

138 

139 

140def default_vex_archinfo() -> Dict[str, Any]: 

141 return { 

142 "hwcaps": 0, 

143 "endness": vex_endness_from_string("VexEndnessLE"), 

144 "ppc_icache_line_szB": 0, 

145 "ppc_dcbz_szB": 0, 

146 "ppc_dcbzl_szB": 0, 

147 "arm64_dMinLine_lg2_szB": 0, 

148 "arm64_iMinLine_lg2_szB": 0, 

149 "hwcache_info": { 

150 "num_levels": 0, 

151 "num_caches": 0, 

152 "caches": None, 

153 "icaches_maintain_coherence": True, 

154 }, 

155 "x86_cr0": 0xFFFFFFFF, 

156 }