Debian Codegraph @

ptrace/pydistorm.py

raw
1 """
2 :[diStorm64 1.7.27}:
3 Copyright RageStorm (C) 2007, Gil Dabah
4
5 diStorm is licensed under the BSD license.
6 http://ragestorm.net/distorm/
7 ---
8 Python binding of diStorm64 library written by Victor Stinner
9 """
10
11 from ctypes import cdll, c_long, c_ulong, c_int, c_uint, c_char, POINTER, Structure, addressof, byref, c_void_p, create_string_buffer, sizeof, cast
12
13 # Define (u)int32_t and (u)int64_t types
14 int32_t = c_int
15 uint32_t = c_uint
16 if sizeof(c_ulong) == 8:
17 int64_t = c_long
18 uint64_t = c_ulong
19 else:
20 from ctypes import c_longlong, c_ulonglong
21 assert sizeof(c_longlong) == 8
22 assert sizeof(c_ulonglong) == 8
23 int64_t = c_longlong
24 uint64_t = c_ulonglong
25
26 SUPPORT_64BIT_OFFSET = True
27 if SUPPORT_64BIT_OFFSET:
28 _OffsetType = uint64_t
29 else:
30 _OffsetType = uint32_t
31
32 LIB_FILENAME = 'libdistorm64.so'
33 distorm = cdll.LoadLibrary(LIB_FILENAME)
34 Decode16Bits = 0
35 Decode32Bits = 1
36 Decode64Bits = 2
37 DECODERS = (Decode16Bits, Decode32Bits, Decode64Bits)
38
39 internal_decode = distorm.internal_decode
40
41 DECRES_NONE = 0
42 DECRES_SUCCESS = 1
43 DECRES_MEMORYERR = 2
44 DECRES_INPUTERR = 3
45
46 MAX_INSTRUCTIONS = 100
47 MAX_TEXT_SIZE = 60
48
49
50 class _WString(Structure):
51 _fields_ = (
52 ("pos", c_uint),
53 ("p", c_char * MAX_TEXT_SIZE),
54 )
55
56 def __str__(self):
57 # FIXME: Use pos?
58 return self.p
59
60
61 class _DecodedInst(Structure):
62 _fields_ = (
63 ("mnemonic", _WString),
64 ("operands", _WString),
65 ("instructionHex", _WString),
66 ("size", c_uint),
67 ("offset", _OffsetType),
68 )
69
70 def __str__(self):
71 return "%s %s" % (self.mnemonic, self.operands)
72
73
74 internal_decode.argtypes = (_OffsetType, c_void_p,
75 c_int, c_int, c_void_p, c_uint, POINTER(c_uint))
76
77
78 def Decode(codeOffset, code, dt=Decode32Bits):
79 """
80 Errors: TypeError, IndexError, MemoryError, ValueError
81 """
82 # Check arguments
83 if not isinstance(codeOffset, int):
84 raise TypeError("codeOffset have to be an integer")
85 if not isinstance(code, bytes):
86 raise TypeError("code have to be a bytes, not %s"
87 % (type(code).__name__,))
88 if dt not in DECODERS:
89 raise IndexError(
90 "Decoding-type must be either Decode16Bits, Decode32Bits or Decode64Bits.")
91
92 # Allocate memory for decoder
93 code_buffer = create_string_buffer(code)
94 decodedInstructionsCount = c_uint()
95 result = create_string_buffer(sizeof(_DecodedInst) * MAX_INSTRUCTIONS)
96
97 # Prepare arguments
98 codeLen = len(code)
99 code = addressof(code_buffer)
100 while codeLen:
101 # Call internal decoder
102 res = internal_decode(codeOffset, code, codeLen, dt, result,
103 MAX_INSTRUCTIONS, byref(decodedInstructionsCount))
104
105 # Check for errors
106 if res == DECRES_INPUTERR:
107 raise ValueError("Invalid argument")
108 count = decodedInstructionsCount.value
109 if res == DECRES_MEMORYERR and not count:
110 raise MemoryError()
111
112 # No more instruction
113 if not count:
114 break
115
116 # Yield instruction and compute decoded size
117 size = 0
118 instr_array = cast(result, POINTER(_DecodedInst))
119 for index in range(count):
120 instr = instr_array[index]
121 size += instr.size
122 yield instr
123
124 # Update parameters to move to next instructions
125 code += size
126 codeOffset += size
127 codeLen -= size