Skip to content

Commit

Permalink
Allow DataVariables to be added to Components
Browse files Browse the repository at this point in the history
  • Loading branch information
0cyn committed Feb 20, 2023
1 parent 982d90f commit fcfa33a
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 84 deletions.
43 changes: 41 additions & 2 deletions binaryninjaapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2017,7 +2017,8 @@ namespace BinaryNinja {
void* ctxt, BNBinaryView* data, BNComponent* formerParent, BNComponent* newParent, BNComponent* component);
static void ComponentFunctionAddedCallback(void* ctxt, BNBinaryView* data, BNComponent* component, BNFunction* function);
static void ComponentFunctionRemovedCallback(void* ctxt, BNBinaryView* data, BNComponent* component, BNFunction* function);

static void ComponentDataVariableAddedCallback(void* ctxt, BNBinaryView* data, BNComponent* component, BNDataVariable* var);
static void ComponentDataVariableRemovedCallback(void* ctxt, BNBinaryView* data, BNComponent* component, BNDataVariable* var);

public:
BinaryDataNotification();
Expand Down Expand Up @@ -2271,6 +2272,32 @@ namespace BinaryNinja {
(void)component;
(void)function;
}

/*! This notification is posted whenever a DataVariable is added to a Component

\param data BinaryView containing the Component and DataVariable
\param component Component the DataVariable was added to
\param var The DataVariable which was added
*/
virtual void OnComponentDataVariableAdded(BinaryView* data, Component* component, const DataVariable& var)
{
(void)data;
(void)component;
(void)var;
}

/*! This notification is posted whenever a DataVariable is removed from a Component

\param data BinaryView containing the Component and DataVariable
\param component Component the DataVariable was removed from
\param var The DataVariable which was removed
*/
virtual void OnComponentDataVariableRemoved(BinaryView* data, Component* component, const DataVariable& var)
{
(void)data;
(void)component;
(void)var;
}
};

/*!
Expand Down Expand Up @@ -4279,6 +4306,9 @@ namespace BinaryNinja {
*/
bool RemoveComponent(std::string guid);

std::vector<Ref<Component>> GetFunctionParentComponents(Ref<Function> function) const;
std::vector<Ref<Component>> GetDataVariableParentComponents(DataVariable var) const;

/*! Check whether the given architecture supports assembling instructions

\param arch Architecture to check
Expand Down Expand Up @@ -8483,7 +8513,6 @@ namespace BinaryNinja {
\return Whether this function needs update
*/
bool NeedsUpdate() const;
std::vector<Ref<Component>> GetParentComponents() const;

/*! Get a list of Basic Blocks for this function

Expand Down Expand Up @@ -14723,6 +14752,8 @@ namespace BinaryNinja {
*/
bool AddComponent(Ref<Component> component);

bool AddDataVariable(DataVariable dataVariable);

/*! Remove a Component from this Component, moving it to the root component.

This will not remove a component from the tree entirely.
Expand All @@ -14741,6 +14772,8 @@ namespace BinaryNinja {
*/
bool RemoveFunction(Ref<Function> func);

bool RemoveDataVariable(DataVariable dataVariable);

/*! Get a list of types referenced by the functions in this Component.

\return vector of Type objects
Expand All @@ -14759,6 +14792,12 @@ namespace BinaryNinja {
*/
std::vector<Ref<Function>> GetContainedFunctions();

/*! Get a list of datavariables added to this component

\return list of DataVariables
*/
std::vector<DataVariable> GetContainedDataVariables();

/*! Get a list of DataVariables referenced by the functions in this Component.

\return vector of DataVariable objects
Expand Down
25 changes: 18 additions & 7 deletions binaryninjacore.h
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,8 @@ extern "C"
void (*componentRemoved)(void*ctxt, BNBinaryView* view, BNComponent* formerParent, BNComponent* component);
void (*componentFunctionAdded)(void*ctxt, BNBinaryView* view, BNComponent* component, BNFunction* function);
void (*componentFunctionRemoved)(void*ctxt, BNBinaryView* view, BNComponent* component, BNFunction* function);
void (*componentDataVariableAdded)(void*ctxt, BNBinaryView* view, BNComponent* component, BNDataVariable* var);
void (*componentDataVariableRemoved)(void*ctxt, BNBinaryView* view, BNComponent* component, BNDataVariable* var);
};

struct BNFileAccessor
Expand Down Expand Up @@ -3786,7 +3788,6 @@ extern "C"
BINARYNINJACOREAPI uint64_t BNGetFunctionHighestAddress(BNFunction* func);
BINARYNINJACOREAPI uint64_t BNGetFunctionLowestAddress(BNFunction* func);
BINARYNINJACOREAPI BNAddressRange* BNGetFunctionAddressRanges(BNFunction* func, size_t* count);
BINARYNINJACOREAPI BNComponent** BNGetFunctionParentComponents(BNFunction *func, size_t* count);

BINARYNINJACOREAPI BNLowLevelILFunction* BNGetFunctionLowLevelIL(BNFunction* func);
BINARYNINJACOREAPI BNLowLevelILFunction* BNGetFunctionLowLevelILIfAvailable(BNFunction* func);
Expand Down Expand Up @@ -5530,6 +5531,9 @@ extern "C"
BINARYNINJACOREAPI void BNRemoveExpressionParserMagicValues(BNBinaryView* view, const char** names, size_t count);
BINARYNINJACOREAPI bool BNGetExpressionParserMagicValue(BNBinaryView* view, const char* name, uint64_t* value);

BINARYNINJACOREAPI BNComponent** BNGetFunctionParentComponents(BNBinaryView* view, BNFunction *func, size_t* count);
BINARYNINJACOREAPI BNComponent** BNGetDataVariableParentComponents(BNBinaryView* view, uint64_t dataVariable, size_t* count);

// Source code processing
BINARYNINJACOREAPI bool BNPreprocessSource(const char* source, const char* fileName, char** output, char** errors,
const char** includeDirs, size_t includeDirCount);
Expand Down Expand Up @@ -6091,6 +6095,7 @@ extern "C"

BINARYNINJACOREAPI BNFunction** BNComponentGetContainedFunctions(BNComponent *component, size_t *count);
BINARYNINJACOREAPI BNComponent** BNComponentGetContainedComponents(BNComponent *component, size_t *count);
BINARYNINJACOREAPI BNDataVariable* BNComponentGetContainedDataVariables(BNComponent *component, size_t *count);

BINARYNINJACOREAPI BNDataVariable* BNComponentGetReferencedDataVariables(BNComponent *component, size_t *count);
BINARYNINJACOREAPI BNDataVariable* BNComponentGetReferencedDataVariablesRecursive(BNComponent *component, size_t *count);
Expand All @@ -6104,17 +6109,23 @@ extern "C"

BINARYNINJACOREAPI bool BNComponentContainsFunction(BNComponent* component, BNFunction *function);
BINARYNINJACOREAPI bool BNComponentContainsComponent(BNComponent *parent, BNComponent *component);
BINARYNINJACOREAPI bool BNComponentContainsDataVariable(BNComponent* component, uint64_t address);

BINARYNINJACOREAPI bool BNComponentAddFunctionReference(BNComponent* component, BNFunction* function);
BINARYNINJACOREAPI bool BNComponentAddComponent(BNComponent* parent, BNComponent* component);
BINARYNINJACOREAPI bool BNComponentAddDataVariable(BNComponent* component, uint64_t address);

BINARYNINJACOREAPI bool BNComponentRemoveComponent(BNComponent* component);
BINARYNINJACOREAPI void BNComponentAddAllMembersFromComponent(BNComponent *component, BNComponent *fromComponent);
BINARYNINJACOREAPI bool BNComponentRemoveFunctionReference(BNComponent *component, BNFunction *function);
BINARYNINJACOREAPI void BNComponentRemoveAllFunctions(BNComponent *component);
BINARYNINJACOREAPI char *BNComponentGetGuid(BNComponent *component);
BINARYNINJACOREAPI bool BNComponentRemoveFunctionReference(BNComponent* component, BNFunction* function);
BINARYNINJACOREAPI void BNComponentRemoveAllFunctions(BNComponent* component);
BINARYNINJACOREAPI bool BNComponentRemoveDataVariable(BNComponent* component, uint64_t address);

BINARYNINJACOREAPI void BNComponentAddAllMembersFromComponent(BNComponent* component, BNComponent* fromComponent);
BINARYNINJACOREAPI char* BNComponentGetGuid(BNComponent* component);
BINARYNINJACOREAPI bool BNComponentsEqual(BNComponent* a, BNComponent* b);
BINARYNINJACOREAPI bool BNComponentsNotEqual(BNComponent* a, BNComponent* b);
BINARYNINJACOREAPI char *BNComponentGetDisplayName(BNComponent* component);
BINARYNINJACOREAPI char *BNComponentGetOriginalName(BNComponent* component);
BINARYNINJACOREAPI char* BNComponentGetDisplayName(BNComponent* component);
BINARYNINJACOREAPI char* BNComponentGetOriginalName(BNComponent* component);
BINARYNINJACOREAPI void BNComponentSetName(BNComponent* component, const char* name);
BINARYNINJACOREAPI BNBinaryView* BNComponentGetView(BNComponent* component);

Expand Down
66 changes: 66 additions & 0 deletions binaryview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,30 @@ void BinaryDataNotification::ComponentFunctionRemovedCallback(void* ctxt, BNBina



void BinaryDataNotification::ComponentDataVariableAddedCallback(void* ctxt, BNBinaryView* data, BNComponent* bnComponent,
BNDataVariable* var)
{
BinaryDataNotification* notify = (BinaryDataNotification*)ctxt;
Ref<BinaryView> view = new BinaryView(BNNewViewReference(data));
Ref<Component> component = new Component(BNNewComponentReference(bnComponent));
DataVariable varObj(var->address,
Confidence<Ref<Type>>(new Type(BNNewTypeReference(var->type)), var->typeConfidence), var->autoDiscovered);
notify->OnComponentDataVariableAdded(view, component, varObj);
}


void BinaryDataNotification::ComponentDataVariableRemovedCallback(void* ctxt, BNBinaryView* data,
BNComponent* bnComponent, BNDataVariable* var)
{
BinaryDataNotification* notify = (BinaryDataNotification*)ctxt;
Ref<BinaryView> view = new BinaryView(BNNewViewReference(data));
Ref<Component> component = new Component(BNNewComponentReference(bnComponent));
DataVariable varObj(var->address,
Confidence<Ref<Type>>(new Type(BNNewTypeReference(var->type)), var->typeConfidence), var->autoDiscovered);
notify->OnComponentDataVariableRemoved(view, component, varObj);
}


BinaryDataNotification::BinaryDataNotification()
{
m_callbacks.context = this;
Expand Down Expand Up @@ -415,6 +439,8 @@ BinaryDataNotification::BinaryDataNotification()
m_callbacks.componentMoved = ComponentMovedCallback;
m_callbacks.componentFunctionAdded = ComponentFunctionAddedCallback;
m_callbacks.componentFunctionRemoved = ComponentFunctionRemovedCallback;
m_callbacks.componentDataVariableAdded = ComponentDataVariableAddedCallback;
m_callbacks.componentDataVariableRemoved = ComponentDataVariableRemovedCallback;
}


Expand Down Expand Up @@ -3094,6 +3120,46 @@ bool BinaryView::RemoveComponent(std::string guid)
}


std::vector<Ref<Component>> BinaryView::GetFunctionParentComponents(Ref<Function> function) const
{
std::vector<Ref<Component>> components;

size_t count;
BNComponent** list = BNGetFunctionParentComponents(m_object, function->m_object, &count);

components.reserve(count);
for (size_t i = 0; i < count; i++)
{
Ref<Component> component = new Component(BNNewComponentReference(list[i]));
components.push_back(component);
}

BNFreeComponents(list, count);

return components;
}


std::vector<Ref<Component>> BinaryView::GetDataVariableParentComponents(DataVariable var) const
{
std::vector<Ref<Component>> components;

size_t count;
BNComponent** list = BNGetDataVariableParentComponents(m_object, var.address, &count);

components.reserve(count);
for (size_t i = 0; i < count; i++)
{
Ref<Component> component = new Component(BNNewComponentReference(list[i]));
components.push_back(component);
}

BNFreeComponents(list, count);

return components;
}


bool BinaryView::CanAssemble(Architecture* arch)
{
return BNCanAssemble(m_object, arch->GetObject());
Expand Down
33 changes: 33 additions & 0 deletions component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ bool Component::AddComponent(Ref<Component> component)
}


bool Component::AddDataVariable(DataVariable dataVariable)
{
return BNComponentAddDataVariable(m_object, dataVariable.address);
}


bool Component::RemoveComponent(Ref<Component> component)
{
return BNComponentRemoveComponent(component->m_object);
Expand All @@ -83,6 +89,12 @@ bool Component::RemoveFunction(Ref<Function> func)
}


bool Component::RemoveDataVariable(DataVariable dataVariable)
{
return BNComponentRemoveDataVariable(m_object, dataVariable.address);
}


std::vector<Ref<Component>> Component::GetContainedComponents()
{
std::vector<Ref<Component>> components;
Expand Down Expand Up @@ -123,6 +135,27 @@ std::vector<Ref<Function>> Component::GetContainedFunctions()
}


std::vector<DataVariable> Component::GetContainedDataVariables()
{
vector<DataVariable> result;

size_t count;
BNDataVariable* variables = BNComponentGetContainedDataVariables(m_object, &count);

result.reserve(count);
for (size_t i = 0; i < count; ++i)
{
result.emplace_back(variables[i].address,
Confidence(new Type(BNNewTypeReference(variables[i].type)), variables[i].typeConfidence),
variables[i].autoDiscovered);
}

BNFreeDataVariables(variables, count);
return result;
}



std::vector<Ref<Type>> Component::GetReferencedTypes()
{
std::vector<Ref<Type>> types;
Expand Down
18 changes: 0 additions & 18 deletions function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1512,24 +1512,6 @@ set<SSAVariable> Function::GetHighLevelILSSAVariablesIfAvailable()
}


std::vector<Ref<Component>> Function::GetParentComponents() const
{
std::vector<Ref<Component>> components;

size_t count;
BNComponent** list = BNGetFunctionParentComponents(m_object, &count);

components.reserve(count);
for (size_t i = 0; i < count; i++)
{
Ref<Component> component = new Component(list[i]);
components.push_back(component);
}

return components;
}


void Function::CreateAutoVariable(
const Variable& var, const Confidence<Ref<Type>>& type, const string& name, bool ignoreDisjointUses)
{
Expand Down
Loading

0 comments on commit fcfa33a

Please sign in to comment.