Vector35 / binaryninja-api

Public API, examples, documentation and issues for Binary Ninja
https://binary.ninja/
MIT License
896 stars 200 forks source link

Optimization for directly using global variables #3919

Open op2786 opened 1 year ago

op2786 commented 1 year ago
140002b7d    HANDLE rax_1
140002b7d    rax_1 = CreateThread(nullptr, nullptr, MainThread, nullptr, THREAD_CREATE_RUN_IMMEDIATELY, nullptr)
140002b83    main_thread_handle = rax_1

could be simplified like this:

main_thread_handle = CreateThread(nullptr, nullptr, MainThread, nullptr, THREAD_CREATE_RUN_IMMEDIATELY, nullptr)

Another example:

140002a54    int64_t* rcx_6 = svc_status_handle
140002a62    ServiceStatus.dwCurrentState = SERVICE_STOPPED
140002a6c    SetServiceStatus(rcx_6, &ServiceStatus)

which could be simplified like this:

140002a62    ServiceStatus.dwCurrentState = SERVICE_STOPPED
140002a6c    SetServiceStatus(svc_status_handle, &ServiceStatus)

Another one:

140002aa0    HANDLE rcx_3 = main_thread_handle
140002aaa    data_140033c5c = 1
140002ab1    WaitForSingleObject(rcx_3, 0xffffffff)

which cold be simplified to:

140002aaa    data_140033c5c = 1
140002ab1    WaitForSingleObject(main_thread_handle, 0xffffffff)

Actually there is cases which codes like above already simplified, but because of some interesting decision these ones does not simplified.

ccarpenter04 commented 1 year ago

In general I support all of these optimizations, however it makes it so the type information isn't right there in your face and you would have to jump to the function to find the type name.

op2786 commented 1 year ago

If I understand you concern correctly, I guess it can be solved by showing variable type on hover.

op2786 commented 1 year ago

Actually this is not related just for global variables. Here is an example that does not uses a global variable:

100126f2          int32_t eax_12 = my_str.max_size
100126f8          if (eax_12 u>= 0x10) {
100126fc            void* var_238_4
100126fc            var_238_4.b = my_str.s[0]
100126fc            var_238_4:1.b = my_str.s[1]
100126fc            var_238_4:2.b = my_str.s[2]
100126fc            var_238_4:3.b = my_str.s[3]
100126ff            std::string::check_params(var_238_4, eax_12 + 1)

I would prefer BN use my_str.max_size directly at 100126f8 and 100126ff.