Vector35 / binaryninja-api

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

Crash when using type APIs headless without prior initialization #3506

Closed comex closed 1 year ago

comex commented 1 year ago

Version and Platform (required):

Bug Description: Trying to use Python APIs without opening a view first causes a crash.

Steps To Reproduce:

 % python3 -c 'import binaryninja; print(binaryninja.types.Type.float(4))'
zsh: segmentation fault  python3 -c 'import binaryninja; print(binaryninja.types.Type.float(4))'

It works if I call open_view beforehand.

Expected Behavior: I'm not sure whether it's intended to be required that you open a view (or perform some other initialization step?) before using these APIs, but if so, it should print an error message rather than crashing.

plafosse commented 1 year ago

Crash is occuring because TypePrinter::GetDefault() is returning null

>>> import binaryninja; print(binaryninja.types.Type.float(4))
Process 22361 stopped
* thread #2, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x000000014b2c7ac4 libbinaryninjacore.1.dylib`::BNGetTypeString(type=0x00000001003f7c30, platform=0x0000000000000000, escaping=NoTokenEscapingType) at typeprinter.cpp:569:50
   566 
   567  char* BNGetTypeString(BNType* type, BNPlatform* platform, BNTokenEscapingType escaping)
   568  {
-> 569          return BNAllocString(TypePrinter::GetDefault()->GetTypeString(
   570                  type->object, platform ? platform->object : nullptr, QualifiedName(), escaping).c_str());
   571  }
plafosse commented 1 year ago

Work around here is calling binaryninja._init_plugins()

CouleeApps commented 1 year ago

Looks like CoreTypePrinter was not being initialized before init plugins. Now it is initialized if you try to print a type and the default type printer does not exist. Will be fixed on dev >= 3.2.3861