Convert to array indexing for accessing out-of-size struct members instead of `__offset()` hex values

Open
#7,140 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp

Research direction

Start by locating the existing array-indexing support and the rendering path that emits struct-member __offset() expressions. Verify signed division and remainder behavior for positive and negative offsets, and define done as equivalent array-indexed member expressions for cross-struct accesses while preserving existing behavior otherwise.

Written by the indexing model from the issue text.

Description

Component: Core Effort: Medium Impact: Medium

What is the feature you'd like to have?
I think there's a refinement it could make to the existing array indexing support. Currently Binary Ninja already supports array indexing notation, but when accessing struct members across boundaries, it still uses offset notation.

Is your feature request related to a problem?
This creates a readability issue when analyzing code that uses pointer arithmetic with negative offsets.

Current behavior:
If sizeof(struct) == 8 then:

ps_op_stack_ptr_2->__offset(0x8).b = tmp.type
ps_op_stack_ptr_2->__offset(0x9).b = tmp.perms  
ps_op_stack_ptr_2->__offset(0xa).w = tmp.length
ps_op_stack_ptr_2->__offset(0xfffffffffffffff8).b = prev_tmp.type  // negative offset

Proposed enhancement:
When the offset crosses struct boundaries, automatically convert to array indexing + member access:

ps_op_stack_ptr_2[1].type = tmp.type           // was ->__offset(0x8).b
ps_op_stack_ptr_2[1].perms = tmp.perms      // was ->__offset(0x9).b  
ps_op_stack_ptr_2[1].length = tmp.length    // was ->__offset(0xa).w
ps_op_stack_ptr_2[-1].type = tmp.prev_type     // was ->__offset(0xfffffffffffffff8).b

Are any alternative solutions acceptable?
Expected Algorithm would be:

  1. Detect when offset >= sizeof(struct)
  2. Calculate array index: array_index = (signed)offset / sizeof(struct)
  3. Calculate member offset: member_offset = offset % sizeof(struct)
  4. Convert to: ptr[array_index].member instead of ptr->__offset(hex_value).member

Additional Information:

Binary is: gentle forest glows calmly
Image

Dominant language
C++
Stars
1.3k
Forks
298
Avg merge
5d 5h
Merged PRs (30d)
19

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.

More from Vector35/binaryninja-api

All issues in Vector35/binaryninja-api

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.