aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/game_eh.py (2909B)


      1 """Include MSVC catch continuations that normal control flow cannot reach."""
      2 
      3 import capstone.x86_const
      4 
      5 from tools.lift import eh32
      6 
      7 
      8 def add_exception_resumes(disassembler, address, functions, instructions, leaders):
      9     # Read the actual entry, even when a shared block precedes it in address order.
     10     head = list(disassembler.disassemble_at(address, 64))[:eh32.PROLOGUE_WINDOW]
     11     if len(head) < 2 or head[0].mnemonic != "push" or head[0].op_str != "ebp" \
     12             or head[1].mnemonic != "mov" or head[1].op_str != "ebp, esp":
     13         return set()
     14     handler = None
     15     info = None
     16     for instruction in head:
     17         if instruction.mnemonic == "push" and instruction.op_str.startswith("0x"):
     18             handler = int(instruction.op_str, 16)
     19         elif instruction.mnemonic == "mov" and instruction.op_str.endswith("dword ptr fs:[0]") \
     20                 and handler is not None:
     21             info = eh32.funcinfo(disassembler.read_bytes, handler)
     22             break
     23     if info is None:
     24         return set()
     25 
     26     # Follow each catch's CFG; a sequential scan can run past RET into another catch.
     27     resumes = set()
     28     for address in eh32.catch_funclets(disassembler.read_bytes, info):
     29         function = functions.get(address)
     30         if function is None:
     31             function = disassembler.disassemble_function(address)
     32         assert function is not None, f"Cannot decode catch funclet {address:08X}"
     33         for block in function.blocks.values():
     34             if not block.instructions[-1].is_ret:
     35                 continue
     36             # x87 stores and register restores can separate the EAX value from RET.
     37             for instruction in reversed(block.instructions[:-1]):
     38                 if instruction.mnemonic == "mov" and instruction.op_str.startswith("eax, 0x"):
     39                     resumes.add(int(instruction.op_str[5:], 16))
     40                     break
     41                 decoded = next(disassembler.md.disasm(instruction.bytes, instruction.address))
     42                 if instruction.is_call or set(decoded.regs_access()[1]) & {
     43                         capstone.x86_const.X86_REG_EAX, capstone.x86_const.X86_REG_AX,
     44                         capstone.x86_const.X86_REG_AL, capstone.x86_const.X86_REG_AH}:
     45                     break
     46 
     47     # Preserve the normal body and add every catch-only continuation and its blocks.
     48     for address in sorted(resumes):
     49         function = functions.get(address)
     50         if function is None:
     51             function = disassembler.disassemble_function(address)
     52         assert function is not None, f"Cannot decode catch continuation {address:08X}"
     53         leaders.update(function.blocks)
     54         for block in function.blocks.values():
     55             for instruction in block.instructions:
     56                 instructions.setdefault(instruction.address, instruction)
     57         assert address in instructions, f"Missing catch continuation {address:08X}"
     58     return resumes