diff options
| author | KyleMiles <krm504@nyu.edu> | 2020-10-09 20:57:42 +0000 |
|---|---|---|
| committer | KyleMiles <krm504@nyu.edu> | 2020-10-19 18:17:44 +0000 |
| commit | c7ccd714ceeaabbe5ee682e361250c3cfd54f78f (patch) | |
| tree | e9f9a6da19ca097f82b4a8f8c00ebba4ea14a590 /demangle.cpp | |
| parent | 3083903d0ee4ccd18099cb3315e0220c7f5993fb (diff) | |
Expose Template Simplifier to the API, Adds Tests, and Adds Rust String FFI support to Python (see note)
This introduces the ability to receive and free string from Rust in Python.
For it to work, your exposed function name needs to begin with "BNRust", and the generator will create the appropriate code for freeing string return values and arguments.
Diffstat (limited to 'demangle.cpp')
| -rw-r--r-- | demangle.cpp | 94 |
1 files changed, 90 insertions, 4 deletions
diff --git a/demangle.cpp b/demangle.cpp index 9ea2bfa0..2cdc0288 100644 --- a/demangle.cpp +++ b/demangle.cpp @@ -1,5 +1,5 @@ #include "binaryninjaapi.h" - +#include <string> using namespace std; namespace BinaryNinja @@ -7,10 +7,17 @@ namespace BinaryNinja bool DemangleMS(Architecture* arch, const std::string& mangledName, Type** outType, QualifiedName& outVarName, const Ref<BinaryView>& view) { + const bool simplify = Settings::Instance()->Get<bool>("analysis.types.TemplateSimplifier", view); + return DemangleMS(arch, mangledName, outType, outVarName, simplify); + } + + bool DemangleMS(Architecture* arch, const std::string& mangledName, Type** outType, + QualifiedName& outVarName, const bool simplify) + { BNType* localType = nullptr; char** localVarName = nullptr; size_t localSize = 0; - if (!BNDemangleMS(arch->GetObject(), mangledName.c_str(), &localType, &localVarName, &localSize, view->GetObject())) + if (!BNDemangleMS(arch->GetObject(), mangledName.c_str(), &localType, &localVarName, &localSize, simplify)) return false; if (!localType) return false; @@ -24,14 +31,20 @@ namespace BinaryNinja return true; } - bool DemangleGNU3(Ref<Architecture> arch, const std::string& mangledName, Type** outType, QualifiedName& outVarName, const Ref<BinaryView>& view) { + const bool simplify = Settings::Instance()->Get<bool>("analysis.types.TemplateSimplifier", view); + return DemangleGNU3(arch, mangledName, outType, outVarName, simplify); + } + + bool DemangleGNU3(Ref<Architecture> arch, const std::string& mangledName, Type** outType, + QualifiedName& outVarName, const bool simplify) + { BNType* localType; char** localVarName = nullptr; size_t localSize = 0; - if (!BNDemangleGNU3(arch->GetObject(), mangledName.c_str(), &localType, &localVarName, &localSize, view->GetObject())) + if (!BNDemangleGNU3(arch->GetObject(), mangledName.c_str(), &localType, &localVarName, &localSize, simplify)) return false; if (!localType) return false; @@ -44,4 +57,77 @@ namespace BinaryNinja delete [] localVarName; return true; } + + + string SimplifyName::to_string(const string& input) + { + return (string)SimplifyName(input, SimplifierDest::str, true); + } + + + string SimplifyName::to_string(const QualifiedName& input) + { + return (string)SimplifyName(input.GetString(), SimplifierDest::str, true); + } + + + QualifiedName SimplifyName::to_qualified_name(const string& input, bool simplify) + { + return (QualifiedName)SimplifyName(input, SimplifierDest::fqn, simplify); + } + + + QualifiedName SimplifyName::to_qualified_name(const QualifiedName& input) + { + return (QualifiedName)SimplifyName(input.GetString(), SimplifierDest::fqn, true); + } + + + SimplifyName::SimplifyName(const string& input, const SimplifierDest dest, const bool simplify) : + m_rust_string(nullptr), m_rust_array(nullptr), m_length(0) + { + if (dest == SimplifierDest::str) + m_rust_string = BNRustSimplifyStrToStr(input.c_str()); + else + m_rust_array = BNRustSimplifyStrToFQN(input.c_str(), simplify); + } + + + SimplifyName::~SimplifyName() + { + if (m_rust_string) + BNRustFreeString(m_rust_string); + if (m_rust_array) + { + if (m_length == 0) + { + // Should never reach here + LogWarn("Deallocating SimplifyName without having been used; Likely misuse of API.\n"); + uint64_t index = 0; + while (m_rust_array[index][0] != 0x0) + ++index; + m_length = index + 1; + } + BNRustFreeStringArray(m_rust_array, m_length); + } + } + + + SimplifyName::operator string() const + { + return string(m_rust_string); + } + + + SimplifyName::operator QualifiedName() + { + QualifiedName result; + uint64_t index = 0; + while (m_rust_array[index][0] != 0x0) + { + result.push_back(string(m_rust_array[index++])); + } + m_length = index; + return result; + } } |
