Repository: pwndbg/pwndbg

Issue:
IDA Pro Integration - decompile() segfault
Calling `idaapi.decompile()` causes my IDA `6.9.151221 (December 21, 2015)` to segfault (often corrupting the database) when Hex-Rays is not installed.

There needs to be some sanity checking in the [context](https://github.com/pwndbg/pwndbg/blob/master/pwndbg/commands/context.py#L209) command before calling `pwndbg.ida.decompile()`

I'm not sure if this is fixed in the latest IDA `6.95.160808 (August 08, 2016)`


Discussion:
Just for documentation, I confirmed that this bug is still present in the latest IDA `6.95.160808 (August 08, 2016)`
Can you find any docs about how to determine whether the decompiler is
available?

Also, could you report this to support@hexrays?
On Mon, Apr 10, 2017 at 4:30 PM cloud <notifications@github.com> wrote:

> Just for documentation, I confirmed that this bug is still present in the
> latest IDA 6.95.160808 (August 08, 2016)
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/pwndbg/pwndbg/issues/196#issuecomment-293084910>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AAG0GBPpQwCbI_B4oVlb4eGOG39nVp8Qks5rup9wgaJpZM4Mtssx>
> .
>

@zachriggle @cloudburst 
There is [`bool	
init_hexrays_plugin(flags=0)`](https://www.hex-rays.com/products/ida/support/idapython_docs/ida_hexrays-module.html#init_hexrays_plugin) which seems to be the thing we are looking for.

[Here](https://www.hex-rays.com/products/decompiler/sdk.shtml) is something that might be confirmation that it actually returns whether the decompiler is there:
```c
// Initialize the plugin.
int idaapi init(void)
{
  if ( !init_hexrays_plugin() )
    return PLUGIN_SKIP; // no decompiler
```

Also found the documentation for this function in C or C++: https://www.hex-rays.com/products/decompiler/manual/sdk/group__type.shtml#ga1d89ee3fd8c60f34a85ae78f5c8ff400

EDIT:

The `get_hexrays_version` function might be better. However I am not sure what it returns when there is no decompiler:
https://www.hex-rays.com/products/decompiler/manual/sdk/group__type.shtml#ga1e3fdb1249eea9ac159eb5b63504697c
@cloudburst Can you check it?
I've reported the `idaapi.decompile()` issue to hexrays.

If i call `ida_hexrays.get_hexrays_version()` it also segfaults IDA.  However `ida_hexrays.init_hexrays_plugin()` returns `False` so that seems to work for an API to use potentially.
Hex-Rays said this will be patched in the next release.

Resolving patch:
diff --git a/pwndbg/ida.py b/pwndbg/ida.py
--- a/pwndbg/ida.py
+++ b/pwndbg/ida.py
@@ -74,6 +74,12 @@ def __call__(self, *args, **kwargs):
             return self.fn(*args, **kwargs)
         return None
 
+def withHexrays(f):
+    @withIDA
+    @functools.wraps(f)
+    def wrapper(*a, **kw):
+        if _ida.init_hexrays_plugin():
+            return f(*a, **kw)
 
 def takes_address(function):
     @functools.wraps(function)
@@ -322,7 +328,7 @@ def has_cached_cfunc(addr):
     return _ida.has_cached_cfunc(addr)
 
 
-@withIDA
+@withHexrays
 @takes_address
 @pwndbg.memoize.reset_on_stop
 def decompile(addr):


Test patch: