diff options
| author | Rusty Wagner <rusty@vector35.com> | 2016-07-10 02:49:49 -0400 |
|---|---|---|
| committer | Rusty Wagner <rusty@vector35.com> | 2016-07-10 02:49:49 -0400 |
| commit | ca6d082528f1b4c6d8f66bed18fb2e70f4c87a74 (patch) | |
| tree | 615940ecdbe26eb830d8850f52af7f4dd9f07239 | |
| parent | 7853b6eea8c80b537e2fa199a19dde0f68845449 (diff) | |
| parent | b6b76645f0a5e7e631363c4b4ae6383993669abf (diff) | |
Merge branch 'dev' into linear_view
| -rw-r--r-- | binaryninjaapi.h | 37 | ||||
| -rw-r--r-- | binaryninjacore.h | 169 | ||||
| -rw-r--r-- | demangle.cpp | 23 | ||||
| -rw-r--r-- | python/__init__.py | 46 | ||||
| -rw-r--r-- | python/generator.cpp | 4 | ||||
| -rw-r--r-- | type.cpp | 104 |
6 files changed, 331 insertions, 52 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 47490857..cc17f56c 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -360,6 +360,10 @@ namespace BinaryNinja void SetCurrentPluginLoadOrder(BNPluginLoadOrder order); void AddRequiredPluginDependency(const std::string& name); void AddOptionalPluginDependency(const std::string& name); + bool DemangleMS(Architecture* arch, + const std::string& mangledName, + Type** outType, + std::vector<std::string>& outVarName); class DataBuffer { @@ -1086,7 +1090,10 @@ namespace BinaryNinja typedef size_t ExprId; - //! Architecture is the base class for all architectures + /*! + The Architecture class is the base class for all CPU architectures. This provides disassembly, assembly, + patching, and IL translation lifting for a given architecture. + */ class Architecture: public StaticCoreRefCountObject<BNArchitecture> { protected: @@ -1154,6 +1161,11 @@ namespace BinaryNinja std::vector<InstructionTextToken>& result) = 0; /*! GetInstructionLowLevelIL + Translates an instruction at addr and appends it onto the LowLevelILFunction& il. + \param data pointer to the instruction data to be translated + \param addr address of the instruction data to be translated + \param len length of the instruction data to be translated + \param il the LowLevelILFunction which */ virtual bool GetInstructionLowLevelIL(const uint8_t* data, uint64_t addr, size_t& len, LowLevelILFunction& il); virtual std::string GetRegisterName(uint32_t reg); @@ -1339,6 +1351,7 @@ namespace BinaryNinja size_t GetAlignment() const; bool IsSigned() const; bool IsConst() const; + bool IsVolatile() const; bool IsFloat() const; Ref<Type> GetChildType() const; Ref<CallingConvention> GetCallingConvention() const; @@ -1349,7 +1362,10 @@ namespace BinaryNinja Ref<Enumeration> GetEnumeration() const; uint64_t GetElementCount() const; + void SetFunctionCanReturn(bool canReturn); + std::string GetString() const; + std::string GetTypeAndName(const std::vector<std::string>& name) const; std::string GetStringBeforeName() const; std::string GetStringAfterName() const; @@ -1357,14 +1373,17 @@ namespace BinaryNinja static Ref<Type> VoidType(); static Ref<Type> BoolType(); - static Ref<Type> IntegerType(size_t width, bool sign); - static Ref<Type> FloatType(size_t width); + static Ref<Type> IntegerType(size_t width, bool sign, const std::string& altName = ""); + static Ref<Type> FloatType(size_t width, const std::string& typeName = ""); static Ref<Type> StructureType(Structure* strct); - static Ref<Type> EnumerationType(Architecture* arch, Enumeration* enm, size_t width = 0); - static Ref<Type> PointerType(Architecture* arch, Type* type, bool cnst = false); + static Ref<Type> EnumerationType(Architecture* arch, Enumeration* enm, size_t width = 0, bool issigned = false); + static Ref<Type> PointerType(Architecture* arch, Type* type, bool cnst = false, bool vltl = false, + BNReferenceType refType = PointerReferenceType); static Ref<Type> ArrayType(Type* type, uint64_t elem); static Ref<Type> FunctionType(Type* returnValue, CallingConvention* callingConvention, const std::vector<NameAndType>& params, bool varArg = false); + + static std::string GetQualifiedName(const std::vector<std::string>& names); }; struct StructureMember @@ -1379,8 +1398,8 @@ namespace BinaryNinja public: Structure(BNStructure* s); - std::string GetName() const; - void SetName(const std::string& name); + std::vector<std::string> GetName() const; + void SetName(const std::vector<std::string>& name); std::vector<StructureMember> GetMembers() const; uint64_t GetWidth() const; size_t GetAlignment() const; @@ -1406,8 +1425,8 @@ namespace BinaryNinja public: Enumeration(BNEnumeration* e); - std::string GetName() const; - void SetName(const std::string& name); + std::vector<std::string> GetName() const; + void SetName(const std::vector<std::string>& name); std::vector<EnumerationMember> GetMembers() const; diff --git a/binaryninjacore.h b/binaryninjacore.h index 9784b4ab..e559ec9a 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -101,11 +101,11 @@ extern "C" //! Console log levels enum BNLogLevel { - DebugLog = 0, //!< Debug logging level, most verbose logging level - InfoLog = 1, //!< Information logging level, default logging level - WarningLog = 2, //!< Warning logging level, messages show with warning icon in the UI - ErrorLog = 3, //!< Error logging level, messages show with error icon in the UI - AlertLog = 4 //!< Alert logging level, messages are displayed with popup message box in the UI + DebugLog = 0, //! Debug logging level, most verbose logging level + InfoLog = 1, //! Information logging level, default logging level + WarningLog = 2, //! Warning logging level, messages show with warning icon in the UI + ErrorLog = 3, //! Error logging level, messages show with error icon in the UI + AlertLog = 4 //! Alert logging level, messages are displayed with popup message box in the UI }; enum BNEndianness @@ -327,7 +327,138 @@ extern "C" EnumerationTypeClass = 5, PointerTypeClass = 6, ArrayTypeClass = 7, - FunctionTypeClass = 8 + FunctionTypeClass = 8, + VarArgsTypeClass = 9, + ValueTypeClass = 10 + }; + + enum BNStructureType + { + ClassStructureType = 0, + StructStructureType = 1, + UnionStructureType = 2 + }; + + enum BNMemberScope { + NoScope, + StaticScope, + VirtualScope, + ThunkScope + }; + + enum BNMemberAccess + { + NoAccess, + PrivateAccess, + ProtectedAccess, + PublicAccess + }; + + enum BNReferenceType + { + PointerReferenceType = 0, + ReferenceReferenceType = 1, + RValueReferenceType = 2 + }; + + enum BNPointerSuffix + { + Ptr64Suffix, + UnalignedSuffix, + RestrictSuffix, + ReferenceSuffix, + LvalueSuffix + }; + + enum BNNameType + { + NoNameType, + ConstructorNameType, + DestructorNameType, + OperatorNewNameType, + OperatorDeleteNameType, + OperatorAssignNameType, + OperatorRightShiftNameType, + OperatorLeftShiftNameType, + OperatorNotNameType, + OperatorEqualNameType, + OperatorNotEqualNameType, + OperatorArrayNameType, + OperatorArrowNameType, + OperatorStarNameType, + OperatorIncrementNameType, + OperatorDecrementNameType, + OperatorMinusNameType, + OperatorPlusNameType, + OperatorBitAndNameType, + OperatorArrowStarNameType, + OperatorDivideNameType, + OperatorModulusNameType, + OperatorLessThanNameType, + OperatorLessThanEqualNameType, + OperatorGreaterThanNameType, + OperatorGreaterThanEqualNameType, + OperatorCommaNameType, + OperatorParenthesesNameType, + OperatorTildeNameType, + OperatorXorNameType, + OperatorBitOrNameType, + OperatorLogicalAndNameType, + OperatorLogicalOrNameType, + OperatorStarEqualNameType, + OperatorPlusEqualNameType, + OperatorMinusEqualNameType, + OperatorDivideEqualNameType, + OperatorModulusEqualNameType, + OperatorRightShiftEqualNameType, + OperatorLeftShiftEqualNameType, + OperatorAndEqualNameType, + OperatorOrEqualNameType, + OperatorXorEqualNameType, + VFTableNameType, + VBTableNameType, + VCallNameType, + TypeofNameType, + LocalStaticGuardNameType, + StringNameType, + VBaseDestructorNameType, + VectorDeletingDestructorNameType, + DefaultConstructorClosureNameType, + ScalarDeletingDestructorNameType, + VectorConstructorIteratorNameType, + VectorDestructorIteratorNameType, + VectorVBaseConstructorIteratoreNameType, + VirtualDisplacementMapNameType, + EHVectorConstructorIteratorNameType, + EHVectorDestructorIteratorNameType, + EHVectorVBaseConstructorIteratorNameType, + CopyConstructorClosureNameType, + UDTReturningNameType, + LocalVFTableNameType, + LocalVFTableConstructorClosureNameType, + OperatorNewArrayNameType, + OperatorDeleteArrayNameType, + PlacementDeleteClosureNameType, + PlacementDeleteClosureArrayNameType, + OperatorReturnTypeNameType, + RttiTypeDescriptor, + RttiBaseClassDescriptor, + RttiBaseClassArray, + RttiClassHeirarchyDescriptor, + RttiCompleteObjectLocator + }; + + enum BNCallingConventionName + { + NoCallingConvention, + CdeclCallingConvention, + PascalCallingConvention, + ThisCallCallingConvention, + STDCallCallingConvention, + FastcallCallingConvention, + CLRCallCallingConvention, + EabiCallCallingConvention, + VectorCallCallingConvention }; enum BNStringType @@ -1409,16 +1540,18 @@ extern "C" // Types BINARYNINJACOREAPI BNType* BNCreateVoidType(void); BINARYNINJACOREAPI BNType* BNCreateBoolType(void); - BINARYNINJACOREAPI BNType* BNCreateIntegerType(size_t width, bool sign); - BINARYNINJACOREAPI BNType* BNCreateFloatType(size_t width); + BINARYNINJACOREAPI BNType* BNCreateIntegerType(size_t width, bool sign, const char* altName); + BINARYNINJACOREAPI BNType* BNCreateFloatType(size_t width, const char* altName); BINARYNINJACOREAPI BNType* BNCreateStructureType(BNStructure* s); - BINARYNINJACOREAPI BNType* BNCreateEnumerationType(BNArchitecture* arch, BNEnumeration* e, size_t width); - BINARYNINJACOREAPI BNType* BNCreatePointerType(BNArchitecture* arch, BNType* type, bool cnst); + BINARYNINJACOREAPI BNType* BNCreateEnumerationType(BNArchitecture* arch, BNEnumeration* e, size_t width, bool isSigned); + BINARYNINJACOREAPI BNType* BNCreatePointerType(BNArchitecture* arch, BNType* type, bool cnst, bool vltl, + BNReferenceType refType); BINARYNINJACOREAPI BNType* BNCreateArrayType(BNType* type, uint64_t elem); BINARYNINJACOREAPI BNType* BNCreateFunctionType(BNType* returnValue, BNCallingConvention* callingConvention, BNNameAndType* params, size_t paramCount, bool varArg); BINARYNINJACOREAPI BNType* BNNewTypeReference(BNType* type); BINARYNINJACOREAPI BNType* BNDuplicateType(BNType* type); + BINARYNINJACOREAPI char* BNGetTypeAndName(BNType* type, const char** nameList, size_t nameCount); BINARYNINJACOREAPI void BNFreeType(BNType* type); BINARYNINJACOREAPI BNTypeClass BNGetTypeClass(BNType* type); @@ -1426,6 +1559,7 @@ extern "C" BINARYNINJACOREAPI size_t BNGetTypeAlignment(BNType* type); BINARYNINJACOREAPI bool BNIsTypeSigned(BNType* type); BINARYNINJACOREAPI bool BNIsTypeConst(BNType* type); + BINARYNINJACOREAPI bool BNIsTypeVolatile(BNType* type); BINARYNINJACOREAPI bool BNIsTypeFloatingPoint(BNType* type); BINARYNINJACOREAPI BNType* BNGetChildType(BNType* type); BINARYNINJACOREAPI BNCallingConvention* BNGetTypeCallingConvention(BNType* type); @@ -1436,6 +1570,7 @@ extern "C" BINARYNINJACOREAPI BNStructure* BNGetTypeStructure(BNType* type); BINARYNINJACOREAPI BNEnumeration* BNGetTypeEnumeration(BNType* type); BINARYNINJACOREAPI uint64_t BNGetTypeElementCount(BNType* type); + BINARYNINJACOREAPI void BNSetFunctionCanReturn(BNType* type, bool canReturn); BINARYNINJACOREAPI char* BNGetTypeString(BNType* type); BINARYNINJACOREAPI char* BNGetTypeStringBeforeName(BNType* type); @@ -1445,8 +1580,8 @@ extern "C" BINARYNINJACOREAPI BNStructure* BNNewStructureReference(BNStructure* s); BINARYNINJACOREAPI void BNFreeStructure(BNStructure* s); - BINARYNINJACOREAPI char* BNGetStructureName(BNStructure* s); - BINARYNINJACOREAPI void BNSetStructureName(BNStructure* s, const char* name); + BINARYNINJACOREAPI char** BNGetStructureName(BNStructure* s, size_t* size); + BINARYNINJACOREAPI void BNSetStructureName(BNStructure* s, const char** names, size_t size); BINARYNINJACOREAPI BNStructureMember* BNGetStructureMembers(BNStructure* s, size_t* count); BINARYNINJACOREAPI void BNFreeStructureMemberList(BNStructureMember* members, size_t count); BINARYNINJACOREAPI uint64_t BNGetStructureWidth(BNStructure* s); @@ -1464,8 +1599,8 @@ extern "C" BINARYNINJACOREAPI BNEnumeration* BNNewEnumerationReference(BNEnumeration* e); BINARYNINJACOREAPI void BNFreeEnumeration(BNEnumeration* e); - BINARYNINJACOREAPI char* BNGetEnumerationName(BNEnumeration* e); - BINARYNINJACOREAPI void BNSetEnumerationName(BNEnumeration* e, const char* name); + BINARYNINJACOREAPI char** BNGetEnumerationName(BNEnumeration* e, size_t* size); + BINARYNINJACOREAPI void BNSetEnumerationName(BNEnumeration* e, const char** name, size_t size); BINARYNINJACOREAPI BNEnumerationMember* BNGetEnumerationMembers(BNEnumeration* e, size_t* count); BINARYNINJACOREAPI void BNFreeEnumerationMemberList(BNEnumerationMember* members, size_t count); @@ -1608,6 +1743,12 @@ extern "C" BINARYNINJACOREAPI BNPlatform* BNGetRelatedPlatform(BNPlatform* platform, BNArchitecture* arch); BINARYNINJACOREAPI void BNAddRelatedPlatform(BNPlatform* platform, BNArchitecture* arch, BNPlatform* related); + //Demangler + BINARYNINJACOREAPI bool BNDemangleMS(BNArchitecture* arch, + const char* mangledName, + BNType** outType, + char*** outVarName, + size_t* outVarNameElements); #ifdef __cplusplus } #endif diff --git a/demangle.cpp b/demangle.cpp new file mode 100644 index 00000000..1a998a87 --- /dev/null +++ b/demangle.cpp @@ -0,0 +1,23 @@ +#include "binaryninjaapi.h" + +using namespace BinaryNinja; +using namespace std; + +bool DemangleMS(Architecture* arch, + const std::string& mangledName, + Type** outType, + std::vector<std::string>& outVarName) +{ + BNType* localType = (*outType)->GetObject(); + char** localVarName = nullptr; + size_t localSize = 0; + if (!BNDemangleMS(arch->GetObject(), mangledName.c_str(), &localType, &localVarName, &localSize)) + return false; + for (size_t i = 0; i < localSize; i++) + { + outVarName.push_back(localVarName[i]); + BNFreeString(localVarName[i]); + } + delete [] localVarName; + return true; +} diff --git a/python/__init__.py b/python/__init__.py index 1037e530..cdced08c 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -192,7 +192,7 @@ class FileMetadata(object): @property def filename(self): - """Backing filename""" + """Backing filename (read-only)""" return core.BNGetFilename(self.handle) @filename.setter @@ -201,7 +201,7 @@ class FileMetadata(object): @property def modified(self): - """Boolean result of whether the file is modified""" + """Boolean result of whether the file is modified (read-only)""" return core.BNIsFileModified(self.handle) @modified.setter @@ -218,7 +218,7 @@ class FileMetadata(object): @property def has_database(self): - """Whether the FileMetadata is backed by a database""" + """Whether the FileMetadata is backed by a database (read-only)""" return core.BNIsBackedByDatabase(self.handle) @property @@ -231,7 +231,7 @@ class FileMetadata(object): @property def offset(self): - """Current offset into the file""" + """Current offset into the file (read-only)""" return core.BNGetCurrentOffset(self.handle) @offset.setter @@ -247,7 +247,7 @@ class FileMetadata(object): @property def saved(self): - """Set to mark file as saved""" + """Boolean returns if the file has changed since last save. Set to mark whether the file is saved.""" return not core.BNIsFileModified(self.handle) @saved.setter @@ -2323,6 +2323,20 @@ class Function(object): core.BNFreeFunction(self.handle) @property + def name(self): + """Symbol name for the function""" + return self.symbol.name + + @name.setter + def name(self,value): + if value is None: + if self.symbol is not None: + self.view.undefine_user_symbol(self.symbol) + else: + symbol = Symbol(FunctionSymbol,self.start,value) + self.view.define_user_symbol(symbol) + + @property def view(self): """Function view (read-only)""" return self._view @@ -3000,7 +3014,7 @@ class FunctionGraph(object): @property def function(self): - """Function for a function graph(read-only)""" + """Function for a function graph (read-only)""" func = core.BNGetFunctionForFunctionGraph(self.handle) if func is None: return None @@ -5760,6 +5774,26 @@ def get_time_since_last_update_check(): def updates_checked(): core.BNUpdatesChecked() +def get_qualified_name(names): + out = "" + for i,a in enumerate(names): + if i == 0: + out += a + else: + out += "::" + a + return out + +def demangle_ms(arch, mangledName): + handle = ctypes.POINTER(core.BNType)() + outName = ctypes.POINTER(ctypes.c_char_p)() + outSize = ctypes.c_ulonglong() + names = [] + if core.BNDemangleMS(arch.handle, mangledName, ctypes.byref(handle), ctypes.byref(outName), ctypes.byref(outSize)): + for i in xrange(outSize.value): + names.append(outName[i]) + return (Type(handle), names) + return (None, mangledName) + bundled_plugin_path = core.BNGetBundledPluginDirectory() user_plugin_path = core.BNGetUserPluginDirectory() diff --git a/python/generator.cpp b/python/generator.cpp index f9790619..08485315 100644 --- a/python/generator.cpp +++ b/python/generator.cpp @@ -98,10 +98,10 @@ void OutputType(FILE* out, Type* type, bool isReturnType = false, bool isCallbac fprintf(out, "ctypes.c_double"); break; case StructureTypeClass: - fprintf(out, "%s", type->GetStructure()->GetName().c_str()); + fprintf(out, "%s", type->GetQualifiedName(type->GetStructure()->GetName()).c_str()); break; case EnumerationTypeClass: - fprintf(out, "%s", type->GetEnumeration()->GetName().c_str()); + fprintf(out, "%s", type->GetQualifiedName(type->GetEnumeration()->GetName()).c_str()); break; case PointerTypeClass: if (isCallback || (type->GetChildType()->GetClass() == VoidTypeClass)) @@ -139,6 +139,27 @@ uint64_t Type::GetElementCount() const } +string Type::GetQualifiedName(const vector<string>& names) +{ + bool first = true; + string out; + for (auto &name : names) + { + if (!first) + { + out += "::" + name; + } + else + { + out += name; + } + if (name.length() != 0) + first = false; + } + return out; +} + + string Type::GetString() const { char* str = BNGetTypeString(m_object); @@ -148,6 +169,18 @@ string Type::GetString() const } +string Type::GetTypeAndName(const vector<string>& nameList) const +{ + const char ** str = new const char*[nameList.size()]; + for (size_t i = 0; i < nameList.size(); i++) + { + str[i] = nameList[i].c_str(); + } + char* outName = BNGetTypeAndName(m_object, str, nameList.size()); + delete [] str; + return outName; +} + string Type::GetStringBeforeName() const { char* str = BNGetTypeStringBeforeName(m_object); @@ -184,15 +217,15 @@ Ref<Type> Type::BoolType() } -Ref<Type> Type::IntegerType(size_t width, bool sign) +Ref<Type> Type::IntegerType(size_t width, bool sign, const string& altName) { - return new Type(BNCreateIntegerType(width, sign)); + return new Type(BNCreateIntegerType(width, sign, altName.c_str())); } -Ref<Type> Type::FloatType(size_t width) +Ref<Type> Type::FloatType(size_t width, const string& altName) { - return new Type(BNCreateFloatType(width)); + return new Type(BNCreateFloatType(width, altName.c_str())); } @@ -202,15 +235,15 @@ Ref<Type> Type::StructureType(Structure* strct) } -Ref<Type> Type::EnumerationType(Architecture* arch, Enumeration* enm, size_t width) +Ref<Type> Type::EnumerationType(Architecture* arch, Enumeration* enm, size_t width, bool isSigned) { - return new Type(BNCreateEnumerationType(arch->GetObject(), enm->GetObject(), width)); + return new Type(BNCreateEnumerationType(arch->GetObject(), enm->GetObject(), width, isSigned)); } -Ref<Type> Type::PointerType(Architecture* arch, Type* type, bool cnst) +Ref<Type> Type::PointerType(Architecture* arch, Type* type, bool cnst, bool vltl, BNReferenceType refType) { - return new Type(BNCreatePointerType(arch->GetObject(), type->GetObject(), cnst)); + return new Type(BNCreatePointerType(arch->GetObject(), type->GetObject(), cnst, vltl, refType)); } @@ -238,24 +271,42 @@ Ref<Type> Type::FunctionType(Type* returnValue, CallingConvention* callingConven } +void Type::SetFunctionCanReturn(bool canReturn) +{ + BNSetFunctionCanReturn(m_object, canReturn); +} + + Structure::Structure(BNStructure* s) { m_object = s; } -string Structure::GetName() const +vector<string> Structure::GetName() const { - char* name = BNGetStructureName(m_object); - string result = name; - BNFreeString(name); + size_t size; + char** name = BNGetStructureName(m_object, &size); + vector<string> result; + for (size_t i = 0; i < size; i++) + { + result.push_back(name[i]); + BNFreeString(name[i]); + } + delete [] name; return result; } -void Structure::SetName(const string& name) +void Structure::SetName(const vector<string>& names) { - BNSetStructureName(m_object, name.c_str()); + const char ** nameList = new const char*[names.size()]; + for (size_t i = 0; i < names.size(); i++) + { + nameList[i] = names[i].c_str(); + } + BNSetStructureName(m_object, nameList, names.size()); + delete [] nameList; } @@ -339,18 +390,29 @@ Enumeration::Enumeration(BNEnumeration* e) } -string Enumeration::GetName() const +vector<string> Enumeration::GetName() const { - char* name = BNGetEnumerationName(m_object); - string result = name; - BNFreeString(name); + vector<string> result; + size_t size; + char** name = BNGetEnumerationName(m_object, &size); + for (size_t i = 0; i < size; i++) + { + result.push_back(name[i]); + BNFreeString(name[i]); + } + delete [] name; return result; } - -void Enumeration::SetName(const string& name) +void Enumeration::SetName(const vector<string>& names) { - BNSetEnumerationName(m_object, name.c_str()); + const char **const nameList = new const char*[names.size()]; + for (size_t i = 0; i < names.size(); i++) + { + nameList[i] = names[i].c_str(); + } + BNSetEnumerationName(m_object, nameList, names.size()); + delete [] nameList; } |
