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

Array access on struct with array as the single member confuses the analyser

Open VisualEhrmanntraut opened this issue 1 year ago • 8 comments

Version and Platform (required):

  • Binary Ninja Version: 4.2.5821-dev
  • OS: macOS
  • OS Version: 15.1 Beta (24B5009l)
  • CPU Architecture: Apple Silicon (M3)

Bug Description: Array access using complex math confuses the analyser. Not sure if I've defined something wrong, I'm fairly confident that my type is correct, it seems to be an 0x18-sized structure array inside of a larger structure. The math it does ends up with allocType * 0x18 anyway.

Steps To Reproduce: Not sure, maybe the disassembly gives you a clue.

Expected Behavior:

return (&pAllocations->entries[0]:0x10.q)[zx.q(allocType) * 3]

should be

return pAllocations->entries[allocType].ptr;

Screenshots/Video Recording: Disassembly Screenshot 2024-08-03 at 18 56 15

LLIL Screenshot 2024-08-03 at 18 56 26

MLIL Screenshot 2024-08-03 at 18 56 35

HLIL Screenshot 2024-08-03 at 18 56 42

VisualEhrmanntraut avatar Aug 03 '24 16:08 VisualEhrmanntraut

Are you able to share the .bndb? Or the type definition?

negasora avatar Aug 03 '24 16:08 negasora

@negasora I can share the type definitions. Here they are:

enum AMD_TTL_ALLOCATION_TYPE : uint32_t
{
    ALLOCATION_TYPE_EXT_SVCS = 0x1,
    ALLOCATION_TYPE_IPI = 0x4,
    ALLOCATION_TYPE_TLS = 0x5,
    ALLOCATION_TYPE_CGS_DEVICE = 0x6,
    ALLOCATION_TYPE_MAX = 0x16
};

struct AMD_TTL_ALLOCATION_ENTRY
{
    enum AMD_TTL_ALLOCATION_TYPE allocType;
    uint64_t length;
    void* ptr;
};

struct AMD_TTL_ALLOCATIONS
{
    struct AMD_TTL_ALLOCATION_ENTRY entries[0x16];
};

VisualEhrmanntraut avatar Aug 03 '24 16:08 VisualEhrmanntraut

Seems like it gets fixed if I do this Screenshot 2024-08-03 at 22 31 55

It looks like the problem is that if it's a pointer to a structure with an array at offset 0 it will get confused (?)

VisualEhrmanntraut avatar Aug 03 '24 19:08 VisualEhrmanntraut

Here's a repro of the issue

issue_5820.bndb.zip

plafosse avatar Aug 09 '24 15:08 plafosse

@VisualEhrmanntraut what did you do in the above image?

plafosse avatar Aug 09 '24 15:08 plafosse

Used a pointer to the type used in the array inside of the structure. Compare the HLIL image to the image you are referring to and look at the type definitions to understand further.

VisualEhrmanntraut avatar Aug 09 '24 15:08 VisualEhrmanntraut

ah yeah I see it now thanks

plafosse avatar Aug 09 '24 15:08 plafosse

Fixed in build 4.2.6351

D0ntPanic avatar Nov 04 '24 18:11 D0ntPanic