Skip to content

Commit

Permalink
Merge branch 'dev' into mlil-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ElykDeer authored May 28, 2024
2 parents f173ccf + 2c69eb2 commit 1039110
Show file tree
Hide file tree
Showing 55 changed files with 1,475 additions and 323 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ In addition to the default build setup, you may want to:

There are many examples available. The [Python examples folder](https://github.com/Vector35/binaryninja-api/tree/dev/python/examples) demonstrates many different applications of the Python API, while C++ examples include:

- [background_task](https://github.com/Vector35/binaryninja-api/tree/dev/examples/background_task) is a plugin that demonstrates managing a background task.\*
- [bin-info](https://github.com/Vector35/binaryninja-api/tree/dev/examples/bin-info) is a standalone executable that prints some information about a given binary to the terminal.\*
- [breakpoint](https://github.com/Vector35/binaryninja-api/tree/dev/examples/breakpoint) is a plugin that allows you to select a region within an x86 binary and use the context menu to fill it with breakpoint bytes.
- [command-line disassm](https://github.com/Vector35/binaryninja-api/tree/dev/examples/cmdline_disasm) demonstrates how to dump disassembly to the command line.\*
Expand Down
4 changes: 2 additions & 2 deletions arch/mips/arch_mips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class MipsArchitecture: public Architecture
if (instr.operands[0].immediate != addr + 8)
result.AddBranch(CallDestination, instr.operands[0].immediate, nullptr, hasBranchDelay);
else
result.branchDelay = 1; // We have a "get pc" mnemonic; do nothing
result.delaySlots = 1; // We have a "get pc" mnemonic; do nothing
break;

case MIPS_JAL:
Expand All @@ -311,7 +311,7 @@ class MipsArchitecture: public Architecture
//Jmp to register register value is unknown
case MIPS_JALR:
case MIPS_JALR_HB:
result.branchDelay = 1;
result.delaySlots = 1;
break;

case MIPS_BGEZAL:
Expand Down
2 changes: 1 addition & 1 deletion arch/riscv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ impl<D: 'static + RiscVDisassembler + Send + Sync> architecture::Architecture fo
_ => return None,
};

let mut res = InstructionInfo::new(inst_len, false);
let mut res = InstructionInfo::new(inst_len, 0);

match op {
Op::Jal(ref j) => {
Expand Down
9 changes: 5 additions & 4 deletions architecture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ InstructionInfo::InstructionInfo()
length = 0;
archTransitionByTargetAddr = false;
branchCount = 0;
branchDelay = false;
delaySlots = 0;
}


void InstructionInfo::AddBranch(BNBranchType type, uint64_t target, Architecture* arch, bool hasDelaySlot)
void InstructionInfo::AddBranch(BNBranchType type, uint64_t target, Architecture* arch, uint8_t delaySlot)
{
if (branchCount >= BN_MAX_INSTRUCTION_BRANCHES)
return;
branchDelay = hasDelaySlot;
delaySlots = delaySlot;
branchType[branchCount] = type;
branchTarget[branchCount] = target;
branchArch[branchCount++] = arch ? arch->GetObject() : nullptr;
Expand Down Expand Up @@ -242,6 +242,7 @@ bool Architecture::GetInstructionInfoCallback(
CallbackRef<Architecture> arch(ctxt);

InstructionInfo info;
info.delaySlots = result->delaySlots;
bool ok = arch->GetInstructionInfo(data, addr, maxLen, info);
*result = info;
return ok;
Expand Down Expand Up @@ -1372,7 +1373,7 @@ Ref<CallingConvention> Architecture::GetFastcallCallingConvention()

Ref<Platform> Architecture::GetStandalonePlatform()
{
return new Platform(BNGetArchitectureStandalonePlatform(m_object));
return new CorePlatform(BNGetArchitectureStandalonePlatform(m_object));
}


Expand Down
126 changes: 107 additions & 19 deletions binaryninjaapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,7 @@ namespace BinaryNinja {
std::vector<uint8_t> GetRaw() const;
std::vector<Ref<Metadata>> GetArray() const;
std::map<std::string, Ref<Metadata>> GetKeyValueStore() const;
std::string GetJsonString() const;

// For key-value data only
/*! Get a Metadata object by key. Only for if IsKeyValueStore == true
Expand Down Expand Up @@ -1483,15 +1484,12 @@ namespace BinaryNinja {
\param filename Path to filename or BNDB to open.
\param updateAnalysis If true, UpdateAnalysisAndWait() will be called after opening
a BinaryView.
\param options A Json string whose keys are setting identifiers and whose values are the desired settings.
\param progress Optional function to be called with progress updates as the view is
being loaded. If the function returns false, it will cancel Load.
\param options A Json object whose keys are setting identifiers and whose values are
the desired settings.
\return Constructed view, or a nullptr Ref<BinaryView>
*/
Ref<BinaryView> Load(const std::string& filename, bool updateAnalysis = true,
std::function<bool(size_t, size_t)> progress = {}, Ref<Metadata> options = new Metadata(MetadataType::KeyValueDataType));

Ref<BinaryView> Load(const std::string& filename, bool updateAnalysis = true, const std::string& options = "{}", std::function<bool(size_t, size_t)> progress = {});
/*! Open a BinaryView from a raw data buffer, initializing data views and loading settings.

@threadmainonly
Expand All @@ -1502,14 +1500,12 @@ namespace BinaryNinja {
\param rawData Buffer with raw binary data to load (cannot load from bndb)
\param updateAnalysis If true, UpdateAnalysisAndWait() will be called after opening
a BinaryView.
\param options A Json string whose keys are setting identifiers and whose values are the desired settings.
\param progress Optional function to be called with progress updates as the view is
being loaded. If the function returns false, it will cancel Load.
\param options A Json object whose keys are setting identifiers and whose values are
the desired settings.
\return Constructed view, or a nullptr Ref<BinaryView>
*/
Ref<BinaryView> Load(const DataBuffer& rawData, bool updateAnalysis = true,
std::function<bool(size_t, size_t)> progress = {}, Ref<Metadata> options = new Metadata(MetadataType::KeyValueDataType));
Ref<BinaryView> Load(const DataBuffer& rawData, bool updateAnalysis = true, const std::string& options = "{}", std::function<bool(size_t, size_t)> progress = {});


/*! Open a BinaryView from a raw BinaryView, initializing data views and loading settings.
Expand All @@ -1522,16 +1518,27 @@ namespace BinaryNinja {
\param rawData BinaryView with raw binary data to load
\param updateAnalysis If true, UpdateAnalysisAndWait() will be called after opening
a BinaryView.
\param options A Json string whose keys are setting identifiers and whose values are the desired settings.
\param progress Optional function to be called with progress updates as the view is
being loaded. If the function returns false, it will cancel Load.
\param options A Json object whose keys are setting identifiers and whose values are
the desired settings.
\param isDatabase True if the view being loaded is the raw view of an already opened database.
\return Constructed view, or a nullptr Ref<BinaryView>
*/
Ref<BinaryView> Load(Ref<BinaryView> rawData, bool updateAnalysis = true,
std::function<bool(size_t, size_t)> progress = {}, Ref<Metadata> options = new Metadata(MetadataType::KeyValueDataType),
bool isDatabase = false);
Ref<BinaryView> Load(Ref<BinaryView> rawData, bool updateAnalysis = true, const std::string& options = "{}", std::function<bool(size_t, size_t)> progress = {});

/*!
Deprecated. Use non-metadata version.
*/
Ref<BinaryView> Load(const std::string& filename, bool updateAnalysis, std::function<bool(size_t, size_t)> progress, Ref<Metadata> options = new Metadata(MetadataType::KeyValueDataType));

/*!
Deprecated. Use non-metadata version.
*/
Ref<BinaryView> Load(const DataBuffer& rawData, bool updateAnalysis, std::function<bool(size_t, size_t)> progress, Ref<Metadata> options = new Metadata(MetadataType::KeyValueDataType));

/*!
Deprecated. Use non-metadata version.
*/
Ref<BinaryView> Load(Ref<BinaryView> rawData, bool updateAnalysis, std::function<bool(size_t, size_t)> progress, Ref<Metadata> options = new Metadata(MetadataType::KeyValueDataType), bool isDatabase = false);

/*! Demangles using LLVM's demangler

Expand Down Expand Up @@ -4939,6 +4946,12 @@ namespace BinaryNinja {
*/
void AddEntryPointForAnalysis(Platform* platform, uint64_t start);

/*! adds an function to all entry function list

\param func Function to add
*/
void AddToEntryFunctions(Function* func);

/*! removes a function from the list of functions

\param func Function to be removed
Expand Down Expand Up @@ -5082,6 +5095,11 @@ namespace BinaryNinja {
*/
Ref<Function> GetAnalysisEntryPoint();

/*! Get all entry functions (including user-defined ones)

\return vector of Functions
*/
std::vector<Ref<Function>> GetAllEntryFunctions();

/*! Get most recently used Basic Block containing a virtual address

Expand Down Expand Up @@ -7569,7 +7587,7 @@ namespace BinaryNinja {
struct InstructionInfo : public BNInstructionInfo
{
InstructionInfo();
void AddBranch(BNBranchType type, uint64_t target = 0, Architecture* arch = nullptr, bool hasDelaySlot = false);
void AddBranch(BNBranchType type, uint64_t target = 0, Architecture* arch = nullptr, uint8_t delaySlots = 0);
};

struct NameAndType
Expand Down Expand Up @@ -8594,6 +8612,12 @@ namespace BinaryNinja {

uint64_t GetElementCount() const;
uint64_t GetOffset() const;
BNPointerBaseType GetPointerBaseType() const;
int64_t GetPointerBaseOffset() const;

std::set<BNPointerSuffix> GetPointerSuffix() const;
std::string GetPointerSuffixString() const;
std::vector<InstructionTextToken> GetPointerSuffixTokens(uint8_t baseConfidence = BN_FULL_CONFIDENCE) const;

std::string GetString(Platform* platform = nullptr, BNTokenEscapingType escaping = NoTokenEscapingType) const;
std::string GetTypeAndName(const QualifiedName& name, BNTokenEscapingType escaping = NoTokenEscapingType) const;
Expand Down Expand Up @@ -8963,6 +8987,8 @@ namespace BinaryNinja {
Ref<Enumeration> GetEnumeration() const;
Ref<NamedTypeReference> GetNamedTypeReference() const;
Confidence<BNMemberScope> GetScope() const;
TypeBuilder& SetWidth(size_t width);
TypeBuilder& SetAlignment(size_t alignment);
TypeBuilder& SetNamedTypeReference(NamedTypeReference* ntr);
TypeBuilder& SetScope(const Confidence<BNMemberScope>& scope);
TypeBuilder& SetConst(const Confidence<bool>& cnst);
Expand All @@ -8978,11 +9004,21 @@ namespace BinaryNinja {
uint64_t GetElementCount() const;
uint64_t GetOffset() const;
uint32_t GetSystemCallNumber() const;
BNPointerBaseType GetPointerBaseType() const;
int64_t GetPointerBaseOffset() const;

TypeBuilder& SetOffset(uint64_t offset);
TypeBuilder& SetFunctionCanReturn(const Confidence<bool>& canReturn);
TypeBuilder& SetPure(const Confidence<bool>& pure);
TypeBuilder& SetParameters(const std::vector<FunctionParameter>& params);
TypeBuilder& SetPointerBase(BNPointerBaseType baseType, int64_t baseOffset);

std::set<BNPointerSuffix> GetPointerSuffix() const;
std::string GetPointerSuffixString() const;
std::vector<InstructionTextToken> GetPointerSuffixTokens(uint8_t baseConfidence = BN_FULL_CONFIDENCE) const;

TypeBuilder& AddPointerSuffix(BNPointerSuffix ps);
TypeBuilder& SetPointerSuffix(const std::set<BNPointerSuffix>& suffix);

std::string GetString(Platform* platform = nullptr) const;
std::string GetTypeAndName(const QualifiedName& name) const;
Expand Down Expand Up @@ -14477,6 +14513,12 @@ namespace BinaryNinja {
Platform(Architecture* arch, const std::string& name, const std::string& typeFile,
const std::vector<std::string>& includeDirs = std::vector<std::string>());

static void InitCallback(void *ctxt, BNPlatform*);
static void InitViewCallback(void* ctxt, BNBinaryView* view);
static uint32_t* GetGlobalRegistersCallback(void* ctxt, size_t* count);
static void FreeRegisterListCallback(void* ctxt, uint32_t* regs, size_t count);
static BNType* GetGlobalRegisterTypeCallback(void* ctxt, uint32_t reg);

public:
Platform(BNPlatform* platform);

Expand Down Expand Up @@ -14612,6 +14654,30 @@ namespace BinaryNinja {
*/
void SetSystemCallConvention(CallingConvention* cc);

/*! Callback that will be called when the platform of a binaryview
* is set. Allows for the Platform to to do platform-specific
* processing of views just after finalization.
*
* \param view BinaryView that was just set to this Platform
*/
virtual void BinaryViewInit(BinaryView* view);

/*! Get the global register list for this Platform
*
* Allows the Platform to override the global register list
* used by analysis.
*/
virtual std::vector<uint32_t> GetGlobalRegisters();

/*! Get the type of a global register
*
* Called by analysis when the incoming register value of a
* global register is observed.
*
* \param reg The register being queried for type information.
*/
virtual Ref<Type> GetGlobalRegisterType(uint32_t reg);

Ref<Platform> GetRelatedPlatform(Architecture* arch);
void AddRelatedPlatform(Architecture* arch, Platform* platform);
Ref<Platform> GetAssociatedPlatformByAddress(uint64_t& addr);
Expand Down Expand Up @@ -14702,6 +14768,16 @@ namespace BinaryNinja {
const std::string& autoTypeSource = "");
};


class CorePlatform : public Platform
{
public:
CorePlatform(BNPlatform* plat);

virtual std::vector<uint32_t> GetGlobalRegisters() override;
virtual Ref<Type> GetGlobalRegisterType(uint32_t reg) override;
};

/*!
\ingroup typeparser
*/
Expand Down Expand Up @@ -15513,8 +15589,20 @@ namespace BinaryNinja {
public CoreRefCountObject<BNBackgroundTask, BNNewBackgroundTaskReference, BNFreeBackgroundTask>
{
public:
BackgroundTask(BNBackgroundTask* task);
BackgroundTask(const std::string& initialText, bool canCancel);
BackgroundTask(BNBackgroundTask *task);

/*!
Provides a mechanism for reporting progress of
an optionally cancelable task to the user via the status bar in the UI.
If canCancel is is `True`, then the task can be cancelled either
programmatically or by the user via the UI.

\note This API does not provide a means to execute a task. The caller is responsible to execute (and possibly cancel) the task.

\param initialText Text description of the progress of the background task (displayed in status bar of the UI)
\param canCancel Whether the task can be cancelled
*/
BackgroundTask(const std::string &initialText, bool canCancel);

bool CanCancel() const;
bool IsCancelled() const;
Expand Down Expand Up @@ -15770,7 +15858,7 @@ namespace BinaryNinja {
"message" string None Yes An optional message with additional emphasis
"readOnly" bool None Yes Only enforced by UI elements
"optional" bool None Yes Indicates setting can be null
"hidden" bool "type" is "string" Yes Indicates the UI should conceal the content
"hidden" bool "type" is "string" Yes Indicates the UI should conceal the content. The "ignore" property is required to specify the applicable storage scopes
"requiresRestart bool None Yes Enable restart notification in the UI upon change
"uiSelectionAction" string "type" is "string" Yes {"file", "directory", <Registered UIAction Name>} Informs the UI to add a button to open a selection dialog or run a registered UIAction
================== ====================================== ================== ======== =======================================================================
Expand Down
Loading

0 comments on commit 1039110

Please sign in to comment.