binaryninja-api icon indicating copy to clipboard operation
binaryninja-api copied to clipboard

Don't allow 0 width structure members get added

Open mwales opened this issue 1 year ago • 2 comments

Version and Platform (required):

  • Binary Ninja Version: [4.1.5747 enterprise
  • OS: Ubuntu
  • OS Version: 22.04
  • CPU Architecture: x64

Bug Description: Created a snippet to create vtable structures using existing information from functions that binary ninja had already analyzed. structure looks messed up afterwards. zznop identified that the type i was adding was 0 width, and that I should instead wrap my function type with a Type.pointer to fix.

Steps To Reproduce:

s = types.StructureBuilder.create()
member_func = bv.read_pointer(addr + i)
cur_func = bv.get_function_at(member_func)
...
s.add_member_at_offset(memberName, cur_func.type, offset_val)
bv.define_user_type(vtable_class_name + "_vtable", s)
​

Fixed by instead doing:

s.add_member_at_offset(memberName, Type.pointer(bv.arch,cur_func.type), offset_val)

Expected Behavior: Adding 0 width items to structure will probably just create confusion

Screenshots/Video Recording: image

mwales avatar Sep 13 '24 17:09 mwales

For what it's worth, we already do warn in the UI. We'll do some further discussion internally next week to figure out whether this is something we should address purely with documentation or whether we should do something more like warn or except at the python layer

psifertex avatar Sep 13 '24 17:09 psifertex

I disagree that 0 width structure members are not useful. They can be useful in cases where a placeholder might be needed. For instance say I've discovered a generic structure type:

template <typename T>
struct Foo {
	T bar;
	int baz;
};

Even though its never a valid implementation it can be useful to type data as that structure type to indicate the actual type of the data is one of multiple possible specialized types of Foo. Additionally it also serves as a template, so if I come across another specialized implementation of the type I know how to define it.

WeiN76LQh avatar Oct 15 '24 19:10 WeiN76LQh