Vector35 / Vector35/binaryninja-api

If condition optimization

Open
#6,068 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Core: HLIL Effort: Medium IL Optimization Impact: Medium
Dominant language
C++
Stars
1.3k
Forks
298
Avg merge
5d 5h
Merged PRs (30d)
19

Description

Binja shows the following:
(example taken from SmsRouterSvc.dll at 0x18002994c - CSmsMessageXmlHelper::GetBroadcastMessageXml)

long result_1 = Windows::Sms::Common::CSmsUtil::GetXmlDocument(nullptr, &var_1b8)
long result = result_1
struct IXMLDOMDocument* rdi = var_1b8

if (result_1 s>= 0) {
    long result_2 = Windows::Sms::Common::CSmsUtil::AddElement(rdi, u"Message", nullptr, &var_1a8)
    result = result_2
    rbx = var_1a8
    
    if (result_2 s>= 0) {
        long result_3 = Windows::Sms::Common::CSmsUtil::AddElement(rbx, u"MessageType", u"Broadcast", nullptr)
        result = result_3
        
        if (result_3 s>= 0) {
            long result_4 = Windows::Sms::Common::CSmsUtil::AddElement(rbx, u"SmsDeviceId", r14, nullptr)
            result = result_4
            
            if (result_4 s>= 0) {
                long result_5 = Windows::Sms::Common::CSmsUtil::AddElement(rbx, u"SimIccId", arg5, nullptr)
                result = result_5
                
                if (result_5 s>= 0) {
                    uint16_t* bstrString = nullptr
                    long result_12 = (*(*var_1e0 + 0x30))()
                    result = result_12
                    
                    if (result_12 s>= 0) {
                        long result_6 = Windows::Sms::Common::CSmsUtil::AddElement(rbx, u"Text", bstrString, nullptr)
                        result = result_6

 /* if conditions go much further, this is just a sample */

(As a side note, because of the conditions it feels like new result_* are created each time, which just makes it harder to read)
When it could be optimized into something like:

long result = Windows::Sms::Common::CSmsUtil::GetXmlDocument(nullptr, xmlDoc);
if (result < 0) {
    return result;
}

IXMLDOMElement* messageElement = nullptr;
result = Windows::Sms::Common::CSmsUtil::AddElement(*xmlDoc, L"Message", nullptr, &messageElement);
if (result < 0) {
    return result;
}

result = Windows::Sms::Common::CSmsUtil::AddElement(messageElement, L"MessageType", L"Broadcast", nullptr);
if (result < 0) {
    return result;
}

result = Windows::Sms::Common::CSmsUtil::AddElement(messageElement, L"SmsDeviceId", smsDeviceId, nullptr);
if (result < 0) {
    return result;
}

result = Windows::Sms::Common::CSmsUtil::AddElement(messageElement, L"SimIccId", simIccId, nullptr);
if (result < 0) {
    return result;
}

BSTR bstrString = nullptr;
result = GetMessageText(&bstrString); 
if (result < 0) {
    return result;
}

More examples from the same binary (SmsRouterSvc.dll):
0x18000fd10 - Windows::Sms::Common::CSmsUtil::GetXmlDocument
0x1800286ec - CSmsMessageXmlHelper::GetStatusMessageXml
0x180048484 - GetDeliverMessageXml (specifically around 0x1800488bd)
(^ most references to Windows::Sms::Common::CSmsUtil::GetXmlDocument seems to have the same pattern, and so if conditions)

As #3527 was closed a bit ago, I am opening this one. Below is the dll.
SmsRouterSvc.dll.zip
This was tested on 4.2.6298-dev

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied SmsRouterSvc.dll examples, especially CSmsUtil::GetXmlDocument, GetStatusMessageXml, and GetDeliverMessageXml, and compare their generated pseudocode around the cited addresses. Determine whether the decompiler can reuse the result variable and represent the nested checks as early returns; done means equivalent output for these patterns without the repeated result_* variables.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
reverse-engineering
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.