import json
from common import *

asm = ghidra.app.plugin.assembler.Assemblers.getAssembler(currentProgram)

with open('./dyn_results.json', 'r') as f:
    data = json.load(f)

# Unexpected return address patching
for k, v in data['ret'].iteritems():
    (_from, to, delta) = v[0]
    if delta > 10:
        continue

    from_addr = toAddr(_from)
    to_addr   = toAddr(to)
    call_addr = toAddr(k)

    asm.assemble(from_addr, ["NOP"] * (to_addr.subtract(from_addr)))
    disassemble(call_addr)
    disassemble(to_addr)

    if not isRunningHeadless():
        setBackgroundColor(call_addr, (DARK_BLUE))
    setEOLComment(call_addr, "[dyn] return address patch")
