|
| 1 | +from typing import Dict, Optional, Callable |
| 2 | +from binaryninja import BinaryView, log_info, log_error |
| 3 | +from reait.api import RE_poll_ai_decompilation, RE_begin_ai_decompilation |
| 4 | +from revengai.utils import get_function_id_by_addr as get_function_id_by_addr_util, AddressChangeMonitor, AIDecompilerChecker |
| 5 | + |
| 6 | +class AIDecompiler: |
| 7 | + def __init__(self, config): |
| 8 | + self.config = config |
| 9 | + self._current_checker = None |
| 10 | + self._track_timer = None |
| 11 | + self.tracking_enabled = False |
| 12 | + self._address_monitor = None |
| 13 | + self._timer_monitor = None |
| 14 | + self.dialog = None |
| 15 | + |
| 16 | + def stop_ai_decompiler(self): |
| 17 | + try: |
| 18 | + if self._current_checker: |
| 19 | + self._current_checker.stop() |
| 20 | + self._current_checker = None |
| 21 | + log_info("RevEng.AI | Stopped AI decompiler") |
| 22 | + except Exception as e: |
| 23 | + log_error(f"RevEng.AI | Error stopping AI decompiler: {str(e)}") |
| 24 | + |
| 25 | + def stop_tracking(self): |
| 26 | + try: |
| 27 | + if self._track_timer: |
| 28 | + self._track_timer.stop() |
| 29 | + self._track_timer = None |
| 30 | + log_info("RevEng.AI | Stopped active line tracking") |
| 31 | + except Exception as e: |
| 32 | + log_error(f"RevEng.AI | Error stopping active line tracking: {str(e)}") |
| 33 | + |
| 34 | + def start_address_tracking(self, callback: Optional[Callable] = None, use_timer: bool = True): |
| 35 | + try: |
| 36 | + self.stop_address_tracking() |
| 37 | + self._address_monitor = AddressChangeMonitor(self.address_change_callback) |
| 38 | + log_info("RevEng.AI | Started notification-based address tracking") |
| 39 | + |
| 40 | + except Exception as e: |
| 41 | + log_error(f"RevEng.AI | Error starting address tracking: {str(e)}") |
| 42 | + |
| 43 | + def stop_address_tracking(self): |
| 44 | + try: |
| 45 | + if self._address_monitor: |
| 46 | + self._address_monitor.unregister() |
| 47 | + self._address_monitor = None |
| 48 | + log_info("RevEng.AI | Stopped notification-based address tracking") |
| 49 | + |
| 50 | + if self._timer_monitor: |
| 51 | + self._timer_monitor.stop() |
| 52 | + self._timer_monitor = None |
| 53 | + log_info("RevEng.AI | Stopped timer-based address tracking") |
| 54 | + |
| 55 | + except Exception as e: |
| 56 | + log_error(f"RevEng.AI | Error stopping address tracking: {str(e)}") |
| 57 | + |
| 58 | + def set_address_tracking_callback(self, callback: Callable): |
| 59 | + try: |
| 60 | + if self._address_monitor: |
| 61 | + self._address_monitor.set_callback(callback) |
| 62 | + if self._timer_monitor: |
| 63 | + self._timer_monitor.set_callback(callback) |
| 64 | + else: |
| 65 | + self.start_address_tracking(callback) |
| 66 | + except Exception as e: |
| 67 | + log_error(f"RevEng.AI | Error setting address tracking callback: {str(e)}") |
| 68 | + |
| 69 | + def address_change_callback(self, context, view, addr, change_type): |
| 70 | + if change_type == "address_changed" and addr is not None: |
| 71 | + log_info(f"RevEng.AI | Address changed to 0x{addr:x} - could trigger AI decompilation here") |
| 72 | + if self.dialog: |
| 73 | + log_info(f"RevEng.AI | Pre-tab setup for address 0x{addr:x}") |
| 74 | + bv = view.getCurrentViewInterface().getData() |
| 75 | + self.dialog.pre_tab_setup(bv, addr) |
| 76 | + |
| 77 | + def start_ai_decompiler(self, bv: BinaryView, options: Dict) -> None: |
| 78 | + try: |
| 79 | + if not self.tracking_enabled: |
| 80 | + self.start_address_tracking(self.address_change_callback) |
| 81 | + self.tracking_enabled = True |
| 82 | + |
| 83 | + log_info("RevEng.AI | Starting function searching in portal") |
| 84 | + editor = options.get("editor") |
| 85 | + tab_name = options.get("tab_name") |
| 86 | + function = options.get("function") |
| 87 | + callback = options.get("callback") |
| 88 | + binary_id = self.config.get_binary_id(bv) |
| 89 | + function_id = get_function_id_by_addr_util(bv, function.start, binary_id) |
| 90 | + |
| 91 | + res = RE_poll_ai_decompilation( |
| 92 | + function_id, |
| 93 | + summarise=True, |
| 94 | + ).json() |
| 95 | + |
| 96 | + if not res.get("status", False): |
| 97 | + callback(editor, "AI Decompilation failed.") |
| 98 | + return |
| 99 | + |
| 100 | + poll_status = res.get("data").get("status", "uninitialised") |
| 101 | + log_info(f"RevEng.AI | Polling AI decompilation: {poll_status}") |
| 102 | + |
| 103 | + if poll_status == "uninitialised": |
| 104 | + log_info(f"RevEng.AI | Starting AI Decompilation for function at 0x{function.start:x}") |
| 105 | + |
| 106 | + try: |
| 107 | + res2 = RE_begin_ai_decompilation( |
| 108 | + function_id |
| 109 | + ).json() |
| 110 | + except Exception as e: |
| 111 | + log_error(f"RevEng.AI | Error beginning AI decompilation: {str(e)}") |
| 112 | + callback(editor, "AI Decompilation failed.") |
| 113 | + return |
| 114 | + |
| 115 | + if not res2.get("status", False): |
| 116 | + callback(editor, "AI Decompilation failed.") |
| 117 | + return |
| 118 | + |
| 119 | + log_info("RevEng.AI | AI Decompilation started") |
| 120 | + periodic_checker = AIDecompilerChecker() |
| 121 | + periodic_checker.start_ai_decompiler_checking(function_id, callback, editor, tab_name) |
| 122 | + self._current_checker = periodic_checker |
| 123 | + |
| 124 | + if poll_status == "success": |
| 125 | + log_info(f"RevEng.AI | AI Decompilation for function at 0x{function.start:x} is completed") |
| 126 | + callback(editor, res.get("data").get("decompilation")) |
| 127 | + |
| 128 | + if poll_status == "error": |
| 129 | + log_info(f"RevEng.AI | AI Decompilation for function at 0x{function.start:x} failed") |
| 130 | + callback(editor, "AI Decompilation failed.") |
| 131 | + |
| 132 | + except Exception as e: |
| 133 | + log_error(f"RevEng.AI | Error in AI decompiler: {str(e)}") |
| 134 | + return False, str(e) |
0 commit comments