Hex-Rays v1.6 vs. v1.5 Decompiler Comparison Page

Hex-Rays v1.6 vs. v1.5 Decompiler Comparison Page

Below you will find side-by-side comparisons of v1.5 and v1.6 decompilations. Please maximize the window too see both columns simultaneously.

The following examples are displayed on this page:

NOTE: these are just some selected examples that can be illustrated as a side-by-side difference. Hex-Rays Decompiler v1.6 includes are many other improvements and new features that are not mentioned on this page - simply because there was nothing to compare them with. Also, some improvements have already been illustrated in the previous comparisons. Please refer to the news page for more details.


Inline CFString constants

This simple improvement can substantially speed up analysis of Objective C code: CFString text constants are immediately visible in the output.

void *__cdecl _PMProxy_showHistroyWindow__()
{
  void *v0; // eax@1

  v0 = objc_msgSend("NSWorkspace", "sharedWorkspace");
  return objc_msgSend(v0, "openFile:withApplication:", CFSTR("/var/log/cups/error_log"), CFSTR("Console"));
}

More humanly if-conditions

The decompiler generates much more readable text by dividing complex conditions into smaller chunks. The output is longer but hey, sometimes it makes sense to be verbose! :)

  pos1 = *mark1;
  err.x = parseMark(_viewer, &_text, mark1, _pInfo, &_iLineNo, &point1);
  if ( err.x )
    return err.x;
  if ( mark2 )
  {
    pos2 = *mark2;
    err.x = parseMark(_viewer, &pageNo2, mark2, _pInfo, &lineNo2, &err);
    if ( err.x )
      return err.x;
  }
  if ( pRects )
  {
    curPage = _viewer->nPage;
    if ( pageNo2 < curPage || _text > curPage )
      return err.x;
  }

Support for CONTAINING_RECORD macro

iv class="cmptell"> The decompiler knows how to use the CONTAINING_RECORD macro it the output to get rid of typecasts. As soon as the variable types are correctly set, it replaces casts with a simple macro call.

  _HW_STREAM_OBJECT *HwStreamObject; 
  _STREAM_OBJECT *StreamObject; // esi@1
  StreamObject = CONTAINING_RECORD(HwStreamObject, _STREAM_OBJECT, HwStreamObject);

Support for LIST_ENTRY macros

We added recognition of LIST_ENTRY macros. While not all cases are handled yet, it usually works quite well. Saves you from the mental effort of recognizing these macros yourself.

void __stdcall SCStartRequestOnStream(_STREAM_OBJECT *a1, _DEVICE_EXTENSION *a2)
{
  KSPIN_LOCK *v2; // ebx@1                                                            

  v2 = &a2->SpinLock;
  KefAcquireSpinLockAtDpcLevel(&a2->SpinLock);
  if ( a1->ReadyForNextDataReq && !IsListEmpty(&a1->DataPendingQueue) )
  {
    SCDequeueAndStartStreamDataRequest(a1);
    KefAcquireSpinLockAtDpcLevel(v2);
  }
  if ( !a1->ReadyForNextControlReq || IsListEmpty(&a1->ControlPendingQueue) )
    KefReleaseSpinLockFromDpcLevel(v2);
  else
    SCDequeueAndStartStreamControlRequest(a1);
}

Better tail call recognition

A much better recognition of tail call optimization leads to less JUMPOUT() calls in the output. The call arguments are recognized correctly. The function return value is not lost anymore.

BOOL __stdcall BaseDllInitialize(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
  if ( fdwReason == 1 )
    __security_init_cookie();
  return _BaseDllInitialize(hinstDLL, fdwReason, lpReserved);
}

Improved memset recognition

Six non-trivial lines of code have been collapsed into one simple line. We are happy with this improvement!

  memset(v10, 0, v10->cbSize);

Support for TEB/KPCR references

No more cryptic offsets anymore. As an additional bonus, the decompiler automatically determines variable types because it can use the TEB layout. By the way, KPCR fields are recognized too, you just need to load the corresponding til file!

SessionId = NtCurrentTeb()->ProcessEnvironmentBlock->SessionId;

Better char/short variable recognition

The previous version of the decompiler failed to create a 16-bit variable that was stored by the compiler in bx. This had some very nasty consequences: the function prototype had an incorrect input argument (ebx) and the calling convention was wrong. While it was possible to correct it by specifying the function prototype manually, the new version lifts this burden from you.

The new version takes care of this situation much better. It uses a more fine-grained approach to variable allocation. It created a small 16-bit variable v4. No more ugly LOWORD() macro, the output is cleaner. The correctly determined function prototype will help when decompiling other functions as well because there will be less parasitic arguments and less confusion.

signed int __thiscall sub_600112F7(void *this, int a2, int a3)
{
  unsigned __int16 v4; // bx@1
  v10 = this;
  v4 = *(_WORD *)(a2 + 12);

Better recognition of inline functions

Sorry for a long sample, the previous version of the decompiler was not handling strlen() well enough. It is a never ending fight and perfection is impossible, but we still continue to work on it. Recognition of inline functions is an incredibly hard problem, but the new version has a better engine to recognize them. There is plenty of room for improvement, to put it mildly.

  v18 = (char *)operator new(v15);
  strcpy(v18, *v3);
  strcat(v18, " ");
  strcat(v18, v3[1]);
  strcat(v18, " ");
  strcat(v18, v3[2]);
  if ( v3[3] )
  {
    argca = v3 + 3;
    v25 = v3 + 3;
    do
    {
      strcat(v18, " \"");
      strcat(v18, *argca);
      if ( (*argca)[strlen(*argca) - 1] == 92 )
        strcat(v18, "\\");
      strcat(v18, "\"");
      ++v25;
      argca = v25;
    }
    while ( *v25 );
  }

Structure copying - 1

While it is not pure C, we feel that using C++ style structure copying operations in the output adds to clarity. The above sample is almost perfect, this only possible improvement is to map _this to this. The new decompiler can do that, read our blog post for more info.

void __thiscall FileInfo::InitTime(FileInfo *this)
{
  FileInfo *_this; // ST08_4@1
  _SYSTEMTIME st; // [sp+4h] [bp-18h]@1
  _FILETIME ft; // [sp+14h] [bp-8h]@1

  _this = this;
  GetSystemTime(&st);
  SystemTimeToFileTime(&st, &ft);
  _this->m_CreateTime = ft;
  _this->m_AccessTime = ft;
  _this->m_WriteTime = ft;
}

Structure copying - 2

Please note that after collapsing several assignments into one we also got rid of the intermediary v30 variable. Very good!

(*pRects)[roff / 0x10u] = r;

Sometimes compilers copy structures by DWORD's, regardless of member types. The previous version of the decompiler diligently represented these assignments in the best form it could. However, using a structure copy operation is much better, it is concise and precise.

  _hostobj = &machine->curHostObject;
  *_hostobj = machine->pCallObject->this;

Support for union fields

Finally the decompiler has proper support for union fields. Previously analysing code with unions could quickly turn into a nightmare because the decompiler would just use the first union field and would not allow you to change it. The code, while it had the field names, was very misleading because these names could be completely wrong. This is what we have on the left sample.

The new version is much better in this aspect. First, it tries to determine the best union field using several heuristic rules. It checks the disassembly listing for 'structure offset' operands, checks the current context to select the best fit union field. In many cases no user intervention is required. However, if the decompiler fails to pick the corrent union field, the user can always correct it by selecting the desired union field manually. Even complex situations like a union with another nested union or structure are supported. Anonymous nested unions are represented correctly too.

   PIO_STACK_LOCATION _stacklocation; // [sp+10h] [bp-14h]@1

  if ( stacklocation->Parameters.DeviceIoControl.IoControlCode == 0x224010 )
  {
    v8 = stacklocation->Parameters.Create.Options == 20;
    Semaphore = 0;
    if ( !v8 )
      goto LABEL_18;
    if ( stacklocation->Parameters.Read.Length < 1 )
      goto LABEL_87;
    buf = Irp->AssociatedIrp.SystemBuffer;
    v33 = &Semaphore;
    v32 = stacklocation->FileObject;
    memcpy(&v27, buf, 0x14u);
    DeviceObjecta = ChanMgrGetByHandleAndFileObject(v27, v28, v29, v30, v31, v32, &Semaphore);
    if ( DeviceObjecta < 0 )
      goto LABEL_92;
    v24 = Irp->AssociatedIrp.SystemBuffer;
    v33 = &v36;
    v32 = stacklocation->Parameters.DeviceIoControl.OutputBufferLength;
    v6 = ChannelIRead(Semaphore, v24, v32, &v36);
LABEL_90:
    v33 = Semaphore;
    goto LABEL_91;
  }
  if ( stacklocation->Parameters.DeviceIoControl.IoControlCode == 2244628 )
  {
    v8 = stacklocation->Parameters.DeviceIoControl.InputBufferLength == 20;
    Semaphore = 0;
    if ( v8 && stacklocation->Parameters.DeviceIoControl.OutputBufferLength == 1 )
    {
      v22 = Irp->AssociatedIrp.SystemBuffer;

Support for merged calls

There is a common compiler optimization that reuses the same call instruction for different calls. Of the left side, the WinHelpA() call is used in two different situations. The decompiler had to use a goto statement because it could not represent the code with structured programming constructs.

The new version unmerged the call and got rid of the goto statement. Isn't it nice?

INT_PTR __stdcall EditBinaryValueDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPHELPINFO hinfo)
{
  LONG v5; // eax@8                                                                   

  if ( uMsg == WM_HELP )
  {
    WinHelpW(hinfo->hItemHandle, g_pHelpFileName, HELP_WM_HELP, (ULONG_PTR)s_EditBinaryValueHelpIDs);
  }
  else
  {
    if ( uMsg == WM_CONTEXTMENU )
    {
      WinHelpW((HWND)wParam, g_pHelpFileName, HELP_CONTEXTMENU, (ULONG_PTR)s_EditBinaryValueHelpIDs);
    }
    else
    {
      if ( uMsg == WM_INITDIALOG )
        return EditBinaryValue_OnInitDialog(hwndDlg, wParam, (LONG)hinfo);
      if ( uMsg != 273 || (signed int)(unsigned __int16)wParam <= 0 || (signed int)(unsigned __int16)wParam > 2 )
        return 0;
      v5 = GetWindowLongW(hwndDlg, 8);
      *(_DWORD *)(v5 + 8) = dword_105A048;
      *(_DWORD *)(v5 + 4) = hMem;
      hMem = 0;
      EndDialog(hwndDlg, (unsigned __int16)wParam);
    }
  }
  return 1;
}

Last updated