feat(Core): Added ABORT() macro to prevent the usage of ASSERT(false) as a quick hack to crash the core misusing assert (#2273)

This commit is contained in:
Viste
2019-09-26 10:51:34 +03:00
committed by Stoabrogga
parent 58f3cfe387
commit 854b426978
28 changed files with 127 additions and 74 deletions

View File

@@ -6,26 +6,45 @@
#include "Errors.h"
#include <ace/Stack_Trace.h>
#include <ace/OS_NS_unistd.h>
#include <thread>
#include <cstdlib>
namespace Trinity {
void Assert(char const* file, int line, char const* function, char const* message)
{
ACE_Stack_Trace st;
fprintf(stderr, "\n%s:%i in %s ASSERTION FAILED:\n %s\n%s\n",
file, line, function, message, st.c_str());
fprintf(stderr, "\n%s:%i in %s ASSERTION FAILED:\n %s\n",
file, line, function, message);
*((volatile int*)NULL) = 0;
exit(1);
}
void Fatal(char const* file, int line, char const* function, char const* message)
void Assert(char const* file, int line, char const* function, char const* message, char const* format, ...)
{
fprintf(stderr, "\n%s:%i in %s FATAL ERROR:\n %s\n",
file, line, function, message);
ACE_OS::sleep(10);
va_list args;
va_start(args, format);
fprintf(stderr, "\n%s:%i in %s ASSERTION FAILED:\n %s ", file, line, function, message);
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
fflush(stderr);
va_end(args);
*((volatile int*)NULL) = 0;
exit(1);
}
void Fatal(char const* file, int line, char const* function, char const* message, ...)
{
va_list args;
va_start(args, message);
fprintf(stderr, "\n%s:%i in %s FATAL ERROR:\n ", file, line, function);
vfprintf(stderr, message, args);
fprintf(stderr, "\n");
fflush(stderr);
std::this_thread::sleep_for(std::chrono::seconds(10));
*((volatile int*)NULL) = 0;
exit(1);
}
@@ -44,4 +63,20 @@ void Warning(char const* file, int line, char const* function, char const* messa
file, line, function, message);
}
void Abort(char const* file, int line, char const* function)
{
fprintf(stderr, "\n%s:%i in %s ABORTED.\n",
file, line, function);
*((volatile int*)NULL) = 0;
exit(1);
}
void AbortHandler(int /*sigval*/)
{
// nothing useful to log here, no way to pass args
*((volatile int*)NULL) = 0;
exit(1);
}
} // namespace Trinity

View File

@@ -13,26 +13,44 @@ namespace Trinity
{
DECLSPEC_NORETURN void Assert(char const* file, int line, char const* function, char const* message) ATTR_NORETURN;
DECLSPEC_NORETURN void Assert(char const* file, int line, char const* function, char const* message, char const* format, ...) ATTR_NORETURN ATTR_PRINTF(5, 6);
DECLSPEC_NORETURN void Fatal(char const* file, int line, char const* function, char const* message) ATTR_NORETURN;
DECLSPEC_NORETURN void Fatal(char const* file, int line, char const* function, char const* message, ...) ATTR_NORETURN ATTR_PRINTF(4, 5);
DECLSPEC_NORETURN void Error(char const* file, int line, char const* function, char const* message) ATTR_NORETURN;
DECLSPEC_NORETURN void Abort(char const* file, int line, char const* function) ATTR_NORETURN;
void Warning(char const* file, int line, char const* function, char const* message);
DECLSPEC_NORETURN void AbortHandler(int sigval) ATTR_NORETURN;
} // namespace Trinity
#define WPAssert(cond) do { if (!(cond)) Trinity::Assert(__FILE__, __LINE__, __FUNCTION__, #cond); } while (0)
#define WPFatal(cond, msg) do { if (!(cond)) Trinity::Fatal(__FILE__, __LINE__, __FUNCTION__, (msg)); } while (0)
#define WPError(cond, msg) do { if (!(cond)) Trinity::Error(__FILE__, __LINE__, __FUNCTION__, (msg)); } while (0)
#define WPWarning(cond, msg) do { if (!(cond)) Trinity::Warning(__FILE__, __LINE__, __FUNCTION__, (msg)); } while (0)
#if COMPILER == COMPILER_MICROSOFT
#define ASSERT_BEGIN __pragma(warning(push)) __pragma(warning(disable: 4127))
#define ASSERT_END __pragma(warning(pop))
#else
#define ASSERT_BEGIN
#define ASSERT_END
#endif
#define WPAssert(cond, ...) ASSERT_BEGIN do { if (!(cond)) Trinity::Assert(__FILE__, __LINE__, __FUNCTION__, #cond, ##__VA_ARGS__); } while(0) ASSERT_END
#define WPFatal(cond, ...) ASSERT_BEGIN do { if (!(cond)) Trinity::Fatal(__FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); } while(0) ASSERT_END
#define WPError(cond, msg) ASSERT_BEGIN do { if (!(cond)) Trinity::Error(__FILE__, __LINE__, __FUNCTION__, (msg)); } while(0) ASSERT_END
#define WPWarning(cond, msg) ASSERT_BEGIN do { if (!(cond)) Trinity::Warning(__FILE__, __LINE__, __FUNCTION__, (msg)); } while(0) ASSERT_END
#define WPAbort() ASSERT_BEGIN do { Trinity::Abort(__FILE__, __LINE__, __FUNCTION__); } while(0) ASSERT_END
#define ASSERT WPAssert
#define ABORT WPAbort
template <typename T> inline T* ASSERT_NOTNULL(T* pointer)
template <typename T>
inline T* ASSERT_NOTNULL_IMPL(T* pointer, char const* expr)
{
ASSERT(pointer);
ASSERT(pointer, "%s", expr);
return pointer;
}
#endif
#define ASSERT_NOTNULL(pointer) ASSERT_NOTNULL_IMPL(pointer, #pointer)
#endif