From a48ce3f4aa1266d5b8619cf320a703ba4f697854 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Sun, 31 Jan 2016 02:38:32 -0500 Subject: Refactor API to fix design bug that causes use-after-free on many plugin objects --- function.cpp | 57 ++++++++++++++++++++++++++------------------------------- 1 file changed, 26 insertions(+), 31 deletions(-) (limited to 'function.cpp') diff --git a/function.cpp b/function.cpp index 27d0795b..20b7ee89 100644 --- a/function.cpp +++ b/function.cpp @@ -4,57 +4,52 @@ using namespace BinaryNinja; using namespace std; -Function::Function(BNFunction* func): m_func(func) +Function::Function(BNFunction* func) { -} - - -Function::~Function() -{ - BNFreeFunction(m_func); + m_object = func; } Ref Function::GetPlatform() const { - return new Platform(BNGetFunctionPlatform(m_func)); + return new Platform(BNGetFunctionPlatform(m_object)); } Ref Function::GetArchitecture() const { - return new CoreArchitecture(BNGetFunctionArchitecture(m_func)); + return new CoreArchitecture(BNGetFunctionArchitecture(m_object)); } uint64_t Function::GetStart() const { - return BNGetFunctionStart(m_func); + return BNGetFunctionStart(m_object); } Ref Function::GetSymbol() const { - return new Symbol(BNGetFunctionSymbol(m_func)); + return new Symbol(BNGetFunctionSymbol(m_object)); } bool Function::WasAutomaticallyDiscovered() const { - return BNWasFunctionAutomaticallyDiscovered(m_func); + return BNWasFunctionAutomaticallyDiscovered(m_object); } bool Function::CanReturn() const { - return BNCanFunctionReturn(m_func); + return BNCanFunctionReturn(m_object); } vector> Function::GetBasicBlocks() const { size_t count; - BNBasicBlock** blocks = BNGetFunctionBasicBlockList(m_func, &count); + BNBasicBlock** blocks = BNGetFunctionBasicBlockList(m_object, &count); vector> result; for (size_t i = 0; i < count; i++) @@ -67,13 +62,13 @@ vector> Function::GetBasicBlocks() const void Function::MarkRecentUse() { - BNMarkFunctionAsRecentlyUsed(m_func); + BNMarkFunctionAsRecentlyUsed(m_object); } string Function::GetCommentForAddress(uint64_t addr) const { - char* comment = BNGetCommentForAddress(m_func, addr); + char* comment = BNGetCommentForAddress(m_object, addr); string result = comment; BNFreeString(comment); return result; @@ -83,7 +78,7 @@ string Function::GetCommentForAddress(uint64_t addr) const vector Function::GetCommentedAddresses() const { size_t count; - uint64_t* addrs = BNGetCommentedAddresses(m_func, &count); + uint64_t* addrs = BNGetCommentedAddresses(m_object, &count); vector result; result.insert(result.end(), addrs, &addrs[count]); BNFreeAddressList(addrs); @@ -93,20 +88,20 @@ vector Function::GetCommentedAddresses() const void Function::SetCommentForAddress(uint64_t addr, const string& comment) { - BNSetCommentForAddress(m_func, addr, comment.c_str()); + BNSetCommentForAddress(m_object, addr, comment.c_str()); } Ref Function::GetLowLevelIL() const { - return new LowLevelILFunction(BNGetFunctionLowLevelIL(m_func)); + return new LowLevelILFunction(BNGetFunctionLowLevelIL(m_object)); } vector> Function::GetLowLevelILBasicBlocks() const { size_t count; - BNBasicBlock** blocks = BNGetFunctionLowLevelILBasicBlockList(m_func, &count); + BNBasicBlock** blocks = BNGetFunctionLowLevelILBasicBlockList(m_object, &count); vector> result; for (size_t i = 0; i < count; i++) @@ -119,14 +114,14 @@ vector> Function::GetLowLevelILBasicBlocks() const size_t Function::GetLowLevelILForInstruction(Architecture* arch, uint64_t addr) { - return BNGetLowLevelILForInstruction(m_func, arch->GetArchitectureObject(), addr); + return BNGetLowLevelILForInstruction(m_object, arch->GetObject(), addr); } vector Function::GetLowLevelILExitsForInstruction(Architecture* arch, uint64_t addr) { size_t count; - size_t* exits = BNGetLowLevelILExitsForInstruction(m_func, arch->GetArchitectureObject(), addr, &count); + size_t* exits = BNGetLowLevelILExitsForInstruction(m_object, arch->GetObject(), addr, &count); vector result; result.insert(result.end(), exits, &exits[count]); @@ -138,32 +133,32 @@ vector Function::GetLowLevelILExitsForInstruction(Architecture* arch, ui BNRegisterValue Function::GetRegisterValueAtInstruction(Architecture* arch, uint64_t addr, uint32_t reg) { - return BNGetRegisterValueAtInstruction(m_func, arch->GetArchitectureObject(), addr, reg); + return BNGetRegisterValueAtInstruction(m_object, arch->GetObject(), addr, reg); } BNRegisterValue Function::GetRegisterValueAfterInstruction(Architecture* arch, uint64_t addr, uint32_t reg) { - return BNGetRegisterValueAfterInstruction(m_func, arch->GetArchitectureObject(), addr, reg); + return BNGetRegisterValueAfterInstruction(m_object, arch->GetObject(), addr, reg); } BNRegisterValue Function::GetRegisterValueAtLowLevelILInstruction(size_t i, uint32_t reg) { - return BNGetRegisterValueAtLowLevelILInstruction(m_func, i, reg); + return BNGetRegisterValueAtLowLevelILInstruction(m_object, i, reg); } BNRegisterValue Function::GetRegisterValueAfterLowLevelILInstruction(size_t i, uint32_t reg) { - return BNGetRegisterValueAfterLowLevelILInstruction(m_func, i, reg); + return BNGetRegisterValueAfterLowLevelILInstruction(m_object, i, reg); } vector Function::GetRegistersReadByInstruction(Architecture* arch, uint64_t addr) { size_t count; - uint32_t* regs = BNGetRegistersReadByInstruction(m_func, arch->GetArchitectureObject(), addr, &count); + uint32_t* regs = BNGetRegistersReadByInstruction(m_object, arch->GetObject(), addr, &count); vector result; result.insert(result.end(), regs, ®s[count]); @@ -176,7 +171,7 @@ vector Function::GetRegistersReadByInstruction(Architecture* arch, uin vector Function::GetRegistersWrittenByInstruction(Architecture* arch, uint64_t addr) { size_t count; - uint32_t* regs = BNGetRegistersWrittenByInstruction(m_func, arch->GetArchitectureObject(), addr, &count); + uint32_t* regs = BNGetRegistersWrittenByInstruction(m_object, arch->GetObject(), addr, &count); vector result; result.insert(result.end(), regs, ®s[count]); @@ -188,18 +183,18 @@ vector Function::GetRegistersWrittenByInstruction(Architecture* arch, Ref Function::GetType() const { - return new Type(BNGetFunctionType(m_func)); + return new Type(BNGetFunctionType(m_object)); } void Function::ApplyImportedTypes(Symbol* sym) { - BNApplyImportedTypes(m_func, sym->GetSymbolObject()); + BNApplyImportedTypes(m_object, sym->GetObject()); } Ref Function::CreateFunctionGraph() { - BNFunctionGraph* graph = BNCreateFunctionGraph(m_func); + BNFunctionGraph* graph = BNCreateFunctionGraph(m_object); return new FunctionGraph(graph); } -- cgit v1.3.1