Skip to content

Commit

Permalink
Fix AARCH64 CPSR and pointer size calculation (#855)
Browse files Browse the repository at this point in the history
* make sure aarch64.ptrsize always gets the ptrsize from runtime info via cpsr

* moved `aarch64.cpsr` -> `arm.cpsr`

* Make `format_address` align address strings properly
  • Loading branch information
hugsy committed Jun 28, 2022
1 parent 885d71a commit 290a984
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions gef.py
Expand Up @@ -2152,9 +2152,8 @@ def is_little_endian() -> bool:
def flags_to_human(reg_value: int, value_table: Dict[int, str]) -> str:
"""Return a human readable string showing the flag states."""
flags = []
for i in value_table:
flag_str = Color.boldify(value_table[i].upper()) if reg_value & (1<<i) else value_table[i].lower()
flags.append(flag_str)
for bit_index, name in value_table.items():
flags.append(Color.boldify(name.upper()) if reg_value & (1<<bit_index) != 0 else name.lower())
return f"[{' '.join(flags)}]"


Expand Down Expand Up @@ -2481,7 +2480,7 @@ class ARM(Architecture):

def is_thumb(self) -> bool:
"""Determine if the machine is currently in THUMB mode."""
return is_alive() and gef.arch.register(self.flag_register) & (1 << 5)
return is_alive() and (self.cpsr & (1 << 5) == 1)

@property
def pc(self) -> Optional[int]:
Expand All @@ -2490,6 +2489,12 @@ def pc(self) -> Optional[int]:
pc += 1
return pc

@property
def cpsr(self) -> int:
if not is_alive():
raise RuntimeError("Cannot get CPSR, program not started?")
return gef.arch.register(self.flag_register)

@property
def mode(self) -> str:
return "THUMB" if self.is_thumb() else "ARM"
Expand Down Expand Up @@ -2616,14 +2621,15 @@ class AARCH64(ARM):
29: "carry",
28: "overflow",
7: "interrupt",
9: "endian",
6: "fast",
5: "t32",
4: "m[4]",
}
function_parameters = ("$x0", "$x1", "$x2", "$x3", "$x4", "$x5", "$x6", "$x7",)
syscall_register = "$x8"
syscall_instructions = ("svc $x0",)

_ptrsize = 8

def is_call(self, insn: Instruction) -> bool:
mnemo = insn.mnemonic
call_mnemos = {"bl", "blr"}
Expand All @@ -2636,6 +2642,25 @@ def flag_register_to_human(self, val: Optional[int] = None) -> str:
val = gef.arch.register(reg)
return flags_to_human(val, self.flags_table)

def is_aarch32(self) -> bool:
"""Determine if the CPU is currently in AARCH32 mode from runtime."""
return (self.cpsr & (1 << 4) != 0) and (self.cpsr & (1 << 5) == 0)

def is_thumb32(self) -> bool:
"""Determine if the CPU is currently in THUMB32 mode from runtime."""
return (self.cpsr & (1 << 4) == 1) and (self.cpsr & (1 << 5) == 1)

@property
def ptrsize(self) -> int:
"""Determine the size of pointer from the current CPU mode"""
if not is_alive():
return 8
if self.is_aarch32():
return 4
if self.is_thumb32():
return 2
return 8

@classmethod
def mprotect_asm(cls, addr: int, size: int, perm: Permission) -> str:
_NR_mprotect = 226
Expand Down Expand Up @@ -3672,9 +3697,9 @@ def format_address(addr: int) -> str:
addr = align_address(addr)

if memalign_size == 4:
return f"{addr:#08x}"
return f"0x{addr:08x}"

return f"{addr:#016x}"
return f"0x{addr:016x}"


def format_address_spaces(addr: int, left: bool = True) -> str:
Expand Down

0 comments on commit 290a984

Please sign in to comment.