diff options
| author | Glenn Smith <glenn@vector35.com> | 2023-09-01 00:28:41 -0400 |
|---|---|---|
| committer | Alexander Taylor <ajtaylor@fuzyll.com> | 2023-09-01 13:18:59 -0400 |
| commit | 3a2115bcdcfbca0432afad84f4877187f72514a2 (patch) | |
| tree | c33d689280b37e2c04d6f655d6ea66e4adc26fbd | |
| parent | 9cc14bb1edbd111530ee1e936fe5e59de9450832 (diff) | |
Make asserts from jsoncpp more descriptive
| -rw-r--r-- | binaryninjaapi.h | 88 | ||||
| -rw-r--r-- | exceptions.cpp | 102 | ||||
| -rw-r--r-- | exceptions.h | 48 | ||||
| -rw-r--r-- | json/json.h | 16 | ||||
| -rw-r--r-- | json/jsoncpp.cpp | 5 |
5 files changed, 164 insertions, 95 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index d0d86bbd..404651c7 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -42,6 +42,7 @@ #include <optional> #include <memory> #include "binaryninjacore.h" +#include "exceptions.h" #include "json/json.h" #ifdef _MSC_VER @@ -1679,93 +1680,6 @@ namespace BinaryNinja { std::map<std::string, uint64_t> GetMemoryUsageInfo(); - struct ExceptionWithStackTrace : std::exception - { - std::string m_originalMessage; - std::string m_message; - std::string m_stackTrace; - ExceptionWithStackTrace(const std::string& message) - { - m_originalMessage = message; - m_message = message; - if (getenv("BN_DEBUG_EXCEPTION_TRACES")) - { - char* stackTrace = BNGetCurrentStackTraceString(); - if (stackTrace) - { - m_stackTrace = stackTrace; - m_message += "\n"; - m_message += stackTrace; - BNFreeString(stackTrace); - } - } - } - ExceptionWithStackTrace(std::exception_ptr exc1, std::exception_ptr exc2) - { - m_originalMessage = ""; - m_message = ""; - if (exc1) - { - try - { - std::rethrow_exception(exc1); - } - catch (ExceptionWithStackTrace& stacky) - { - m_originalMessage = stacky.m_originalMessage; - m_message = stacky.m_message; - } - catch (std::exception& exc) - { - m_originalMessage = exc.what(); - m_message = exc.what(); - } - catch (...) - { - m_originalMessage = "Some unknown exception"; - m_message = "Some unknown exception"; - } - } - if (exc2) - { - try - { - std::rethrow_exception(exc2); - } - catch (ExceptionWithStackTrace& stacky) - { - m_originalMessage += "\n" + stacky.m_originalMessage; - m_message += "\n" + stacky.m_message; - } - catch (std::exception& exc) - { - m_originalMessage = exc.what(); - m_message = exc.what(); - } - catch (...) - { - m_originalMessage = "Some unknown exception"; - m_message = "Some unknown exception"; - } - } - if (getenv("BN_DEBUG_EXCEPTION_TRACES")) - { - char* stackTrace = BNGetCurrentStackTraceString(); - if (stackTrace) - { - m_stackTrace = stackTrace; - m_message += "\n"; - m_message += stackTrace; - BNFreeString(stackTrace); - } - } - } - const char* what() const noexcept override - { - return m_message.c_str(); - } - }; - /*! \ingroup databuffer */ diff --git a/exceptions.cpp b/exceptions.cpp new file mode 100644 index 00000000..8beb47af --- /dev/null +++ b/exceptions.cpp @@ -0,0 +1,102 @@ +// Copyright (c) 2015-2023 Vector 35 Inc +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +#include "exceptions.h" +#include "binaryninjacore.h" +#include <stdlib.h> + +BinaryNinja::ExceptionWithStackTrace::ExceptionWithStackTrace(const std::string& message) +{ + m_originalMessage = message; + m_message = message; + if (getenv("BN_DEBUG_EXCEPTION_TRACES")) + { + char* stackTrace = BNGetCurrentStackTraceString(); + if (stackTrace) + { + m_stackTrace = stackTrace; + m_message += "\n"; + m_message += stackTrace; + BNFreeString(stackTrace); + } + } +} + + +BinaryNinja::ExceptionWithStackTrace::ExceptionWithStackTrace(std::exception_ptr exc1, std::exception_ptr exc2) +{ + m_originalMessage = ""; + m_message = ""; + if (exc1) + { + try + { + std::rethrow_exception(exc1); + } + catch (ExceptionWithStackTrace& stacky) + { + m_originalMessage = stacky.m_originalMessage; + m_message = stacky.m_message; + } + catch (std::exception& exc) + { + m_originalMessage = exc.what(); + m_message = exc.what(); + } + catch (...) + { + m_originalMessage = "Some unknown exception"; + m_message = "Some unknown exception"; + } + } + if (exc2) + { + try + { + std::rethrow_exception(exc2); + } + catch (ExceptionWithStackTrace& stacky) + { + m_originalMessage += "\n" + stacky.m_originalMessage; + m_message += "\n" + stacky.m_message; + } + catch (std::exception& exc) + { + m_originalMessage = exc.what(); + m_message = exc.what(); + } + catch (...) + { + m_originalMessage = "Some unknown exception"; + m_message = "Some unknown exception"; + } + } + if (getenv("BN_DEBUG_EXCEPTION_TRACES")) + { + char* stackTrace = BNGetCurrentStackTraceString(); + if (stackTrace) + { + m_stackTrace = stackTrace; + m_message += "\n"; + m_message += stackTrace; + BNFreeString(stackTrace); + } + } +}
\ No newline at end of file diff --git a/exceptions.h b/exceptions.h new file mode 100644 index 00000000..02d9c93b --- /dev/null +++ b/exceptions.h @@ -0,0 +1,48 @@ +// Copyright (c) 2015-2023 Vector 35 Inc +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +#pragma once + +#include <exception> +#include <string> + +#ifndef BINARYNINJACORE_LIBRARY +namespace BinaryNinja +{ + struct ExceptionWithStackTrace : std::exception + { + std::string m_originalMessage; + std::string m_message; + std::string m_stackTrace; + ExceptionWithStackTrace(const std::string& message); + ExceptionWithStackTrace(std::exception_ptr exc1, std::exception_ptr exc2); + const char* what() const noexcept override + { + return m_message.c_str(); + } + }; +} +#endif + +#ifdef BINARYNINJACORE_LIBRARY +using ExceptionWithStackTrace = BinaryNinjaCore::ExceptionWithStackTrace; +#else +using ExceptionWithStackTrace = BinaryNinja::ExceptionWithStackTrace; +#endif
\ No newline at end of file diff --git a/json/json.h b/json/json.h index da91470a..399de6ca 100644 --- a/json/json.h +++ b/json/json.h @@ -502,6 +502,12 @@ public: #pragma warning(disable : 4251) #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) +// XXX: BN: Use our exception class for extra details +#include "../exceptions.h" +#define JSONCPP_EXCEPTION ExceptionWithStackTrace +// #define JSONCPP_EXCEPTION std::runtime_error + + #pragma pack(push, 8) /** \brief JSON (JavaScript Object Notation). @@ -512,14 +518,12 @@ namespace Json { * * We use nothing but these internally. Of course, STL can throw others. */ -class JSON_API Exception : public std::exception { +class JSON_API Exception : public JSONCPP_EXCEPTION /* BN: subclass changed */ { public: Exception(JSONCPP_STRING const& msg); ~Exception() JSONCPP_NOEXCEPT JSONCPP_OVERRIDE; - char const* what() const JSONCPP_NOEXCEPT JSONCPP_OVERRIDE; - -protected: - JSONCPP_STRING msg_; + // BN: Member removed + // BN: what() removed }; /** Exceptions which the user cannot easily avoid. @@ -2222,7 +2226,7 @@ JSON_API JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM&, const Value& root); #define JSON_ASSERT(condition) \ { \ if (!(condition)) { \ - Json::throwLogicError("assert json failed"); \ + Json::throwLogicError(#condition); \ } \ } diff --git a/json/jsoncpp.cpp b/json/jsoncpp.cpp index 4154200b..307cf560 100644 --- a/json/jsoncpp.cpp +++ b/json/jsoncpp.cpp @@ -2653,9 +2653,10 @@ static inline void releaseStringValue(char* value, unsigned) { free(value); } namespace Json { -Exception::Exception(JSONCPP_STRING const& msg) : msg_(msg) {} +// BN: subclass +Exception::Exception(JSONCPP_STRING const& msg) : JSONCPP_EXCEPTION(msg) {} Exception::~Exception() JSONCPP_NOEXCEPT {} -char const* Exception::what() const JSONCPP_NOEXCEPT { return msg_.c_str(); } +// BN: removed what() RuntimeError::RuntimeError(JSONCPP_STRING const& msg) : Exception(msg) {} LogicError::LogicError(JSONCPP_STRING const& msg) : Exception(msg) {} JSONCPP_NORETURN void throwRuntimeError(JSONCPP_STRING const& msg) { |
