| 1 |
from ptrace.ctypes_tools import formatAddress, formatWordHex |
| 2 |
from ptrace.error import PtraceError |
| 3 |
from ptrace.cpu_info import CPU_I386, CPU_X86_64 |
| 4 |
from ptrace.process_tools import formatProcessStatus |
| 5 |
import re |
| 6 |
|
| 7 |
# Find all Intel registers (in upper case) |
| 8 |
if CPU_I386: |
| 9 |
regex = 'E[A-Z]{2}|[CDEFGS]S|[ABCD]L' |
| 10 |
elif CPU_X86_64: |
| 11 |
regex = '[ER][A-Z]{2}|[CDEFGS]S|[ABCD]L' |
| 12 |
else: |
| 13 |
regex = None |
| 14 |
if regex: |
| 15 |
REGISTER_REGEX = re.compile(r'\b(?:%s)\b' % regex) |
| 16 |
else: |
| 17 |
REGISTER_REGEX = None |
| 18 |
|
| 19 |
|
| 20 |
def extractRegisters(process, instr): |
| 21 |
registers = {} |
| 22 |
if not process or not instr or not REGISTER_REGEX: |
| 23 |
return registers |
| 24 |
asm = instr.text |
| 25 |
asm = asm.upper() |
| 26 |
# Skip the operator ("MOV CL, [EAX]" => "CL, [EAX]") |
| 27 |
asm = asm.split(" ", 1)[1] |
| 28 |
for match in REGISTER_REGEX.finditer(asm): |
| 29 |
name = match.group(0) |
| 30 |
name = name.lower() |
| 31 |
try: |
| 32 |
value = process.getreg(name) |
| 33 |
registers[name] = value |
| 34 |
except PtraceError: |
| 35 |
pass |
| 36 |
return registers |
| 37 |
|
| 38 |
|
| 39 |
def findMappings(addresses, process, size): |
| 40 |
mappings = [] |
| 41 |
if addresses is None or not process: |
| 42 |
return mappings |
| 43 |
if not isinstance(addresses, (list, tuple)): |
| 44 |
addresses = (addresses,) |
| 45 |
if not size: |
| 46 |
size = 0 |
| 47 |
process_mappings = process.readMappings() |
| 48 |
if not process_mappings: |
| 49 |
return mappings |
| 50 |
for address in addresses: |
| 51 |
address_str = formatAddress(address) |
| 52 |
if 1 < size: |
| 53 |
address_str += "..%s" % formatAddress(address + size - 1) |
| 54 |
found = False |
| 55 |
for map in process_mappings: |
| 56 |
if (map.start <= address < map.end) \ |
| 57 |
or (map.start <= (address + size - 1) < map.end): |
| 58 |
found = True |
| 59 |
mappings.append("%s is part of %s" % (address_str, map)) |
| 60 |
if not found: |
| 61 |
mappings.append("%s is not mapped in memory" % address_str) |
| 62 |
return mappings |
| 63 |
|
| 64 |
|
| 65 |
class SignalInfo(Exception): |
| 66 |
|
| 67 |
def __init__(self, name, text, |
| 68 |
address=None, size=None, instr=None, |
| 69 |
process=None, registers=None): |
| 70 |
Exception.__init__(self, text) |
| 71 |
self.name = name |
| 72 |
self.text = text |
| 73 |
self.instr = instr |
| 74 |
self.registers = extractRegisters(process, instr) |
| 75 |
if registers: |
| 76 |
self.registers.update(registers) |
| 77 |
self.mappings = findMappings(address, process, size) |
| 78 |
|
| 79 |
def display(self, log): |
| 80 |
log(self.text) |
| 81 |
self.displayExtra(log) |
| 82 |
if self.instr: |
| 83 |
log("- instruction: %s" % self.instr) |
| 84 |
for mapping in self.mappings: |
| 85 |
log("- mapping: %s" % mapping) |
| 86 |
for name, value in self.registers.items(): |
| 87 |
log("- register %s=%s" % (name, formatWordHex(value))) |
| 88 |
|
| 89 |
def displayExtra(self, log): |
| 90 |
pass |
| 91 |
|
| 92 |
|
| 93 |
class DivisionByZero(SignalInfo): |
| 94 |
|
| 95 |
def __init__(self, instr=None, process=None): |
| 96 |
SignalInfo.__init__(self, "div_by_zero", |
| 97 |
"Division by zero", instr=instr, process=process) |
| 98 |
|
| 99 |
|
| 100 |
class Abort(SignalInfo): |
| 101 |
|
| 102 |
def __init__(self): |
| 103 |
SignalInfo.__init__(self, "abort", |
| 104 |
"Program received signal SIGABRT, Aborted.") |
| 105 |
|
| 106 |
|
| 107 |
class StackOverflow(SignalInfo): |
| 108 |
|
| 109 |
def __init__(self, stack_ptr, stack_map, instr=None, process=None): |
| 110 |
text = "STACK OVERFLOW! Stack pointer is in %s" % stack_map |
| 111 |
SignalInfo.__init__(self, "stack_overflow", text, |
| 112 |
address=stack_ptr, registers={ |
| 113 |
'<stack ptr>': stack_ptr}, |
| 114 |
instr=instr, process=process) |
| 115 |
self.stack_ptr = stack_ptr |
| 116 |
self.stack_map = stack_map |
| 117 |
|
| 118 |
|
| 119 |
class InvalidMemoryAccess(SignalInfo): |
| 120 |
NAME = "invalid_mem_access" |
| 121 |
PREFIX = "Invalid memory access" |
| 122 |
PREFIX_ADDR = "Invalid memory access to %s" |
| 123 |
|
| 124 |
def __init__(self, address=None, size=None, instr=None, registers=None, process=None): |
| 125 |
""" |
| 126 |
address is an integer or a list of integer |
| 127 |
""" |
| 128 |
if address is not None: |
| 129 |
if isinstance(address, (list, tuple)): |
| 130 |
arguments = " or ".join(formatAddress(addr) |
| 131 |
for addr in address) |
| 132 |
else: |
| 133 |
arguments = formatAddress(address) |
| 134 |
message = self.PREFIX_ADDR % arguments |
| 135 |
else: |
| 136 |
message = self.PREFIX |
| 137 |
if size: |
| 138 |
message += " (size=%s bytes)" % size |
| 139 |
name = self.NAME |
| 140 |
if address is not None: |
| 141 |
name += "-" + formatAddress(address).lower() |
| 142 |
SignalInfo.__init__(self, name, message, |
| 143 |
address=address, size=size, instr=instr, |
| 144 |
process=process, registers=registers) |
| 145 |
|
| 146 |
|
| 147 |
class InvalidRead(InvalidMemoryAccess): |
| 148 |
NAME = "invalid_read" |
| 149 |
PREFIX = "Invalid read" |
| 150 |
PREFIX_ADDR = "Invalid read from %s" |
| 151 |
|
| 152 |
|
| 153 |
class InvalidWrite(InvalidMemoryAccess): |
| 154 |
NAME = "invalid_write" |
| 155 |
PREFIX = "Invalid write" |
| 156 |
PREFIX_ADDR = "Invalid write to %s" |
| 157 |
|
| 158 |
|
| 159 |
class InstructionError(SignalInfo): |
| 160 |
|
| 161 |
def __init__(self, address, process=None): |
| 162 |
SignalInfo.__init__(self, "instr_error", |
| 163 |
"UNABLE TO EXECUTE CODE AT %s (SEGMENTATION FAULT)" % formatAddress( |
| 164 |
address), |
| 165 |
address=address, |
| 166 |
process=process, |
| 167 |
registers={'<instr pointer>': address}) |
| 168 |
|
| 169 |
|
| 170 |
class ChildExit(SignalInfo): |
| 171 |
|
| 172 |
def __init__(self, pid=None, status=None, uid=None): |
| 173 |
if pid is not None and status is not None: |
| 174 |
message = formatProcessStatus(status, "Child process %s" % pid) |
| 175 |
else: |
| 176 |
message = "Child process exited" |
| 177 |
SignalInfo.__init__(self, "child_exit", message) |
| 178 |
self.pid = pid |
| 179 |
self.status = status |
| 180 |
self.uid = uid |
| 181 |
|
| 182 |
def displayExtra(self, log): |
| 183 |
if self.uid is not None: |
| 184 |
log("Signal sent by user %s" % self.uid) |