2015-07-22 06:11:52 +00:00
|
|
|
// trap.h - written and placed in public domain by Jeffrey Walton.
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \file trap.h
|
|
|
|
/// \brief Debugging and diagnostic assertions
|
2019-01-31 23:55:50 +00:00
|
|
|
/// \details <tt>CRYPTOPP_ASSERT</tt> is the library's debugging and diagnostic
|
|
|
|
/// assertion. <tt>CRYPTOPP_ASSERT</tt> is enabled by <tt>CRYPTOPP_DEBUG</tt>,
|
|
|
|
/// <tt>DEBUG</tt> or <tt>_DEBUG</tt>.
|
|
|
|
/// \details <tt>CRYPTOPP_ASSERT</tt> raises a <tt>SIGTRAP</tt> (Unix) or calls
|
|
|
|
/// <tt>__debugbreak()</tt> (Windows). <tt>CRYPTOPP_ASSERT</tt> is only in
|
|
|
|
/// effect when the user requests a debug configuration. Unlike Posix assert,
|
2017-11-29 15:54:33 +00:00
|
|
|
/// <tt>NDEBUG</tt> (or failure to define it) does not affect the library.
|
2019-01-31 23:55:50 +00:00
|
|
|
/// The traditional Posix define <tt>NDEBUG</tt> has no effect on
|
|
|
|
/// <tt>CRYPTOPP_DEBUG</tt> or DebugTrapHandler.
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \since Crypto++ 5.6.5
|
2019-01-31 23:55:50 +00:00
|
|
|
/// \sa DebugTrapHandler, <A
|
|
|
|
/// HREF="http://github.com/weidai11/cryptopp/issues/277">Issue 277</A>,
|
2017-11-29 15:54:33 +00:00
|
|
|
/// <A HREF="http://seclists.org/oss-sec/2016/q3/520">CVE-2016-7420</A>
|
2016-09-16 15:27:15 +00:00
|
|
|
|
2015-07-22 06:11:52 +00:00
|
|
|
#ifndef CRYPTOPP_TRAP_H
|
|
|
|
#define CRYPTOPP_TRAP_H
|
|
|
|
|
2015-08-03 23:17:42 +00:00
|
|
|
#include "config.h"
|
2015-07-22 06:11:52 +00:00
|
|
|
|
2016-10-18 02:00:31 +00:00
|
|
|
#if defined(CRYPTOPP_DEBUG)
|
2015-08-03 23:17:42 +00:00
|
|
|
# include <iostream>
|
|
|
|
# include <sstream>
|
2016-10-14 19:00:49 +00:00
|
|
|
# if defined(UNIX_SIGNALS_AVAILABLE)
|
2016-09-19 00:43:00 +00:00
|
|
|
# include "ossig.h"
|
2016-10-14 19:00:49 +00:00
|
|
|
# elif defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(__CYGWIN__)
|
2017-05-29 04:43:20 +00:00
|
|
|
extern "C" __declspec(dllimport) void __stdcall DebugBreak();
|
|
|
|
extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();
|
2015-08-03 23:17:42 +00:00
|
|
|
# endif
|
|
|
|
#endif // CRYPTOPP_DEBUG
|
|
|
|
|
2015-07-22 06:11:52 +00:00
|
|
|
// ************** run-time assertion ***************
|
|
|
|
|
2016-09-16 15:27:15 +00:00
|
|
|
#if defined(CRYPTOPP_DOXYGEN_PROCESSING)
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Debugging and diagnostic assertion
|
2019-01-31 23:55:50 +00:00
|
|
|
/// \details <tt>CRYPTOPP_ASSERT</tt> is the library's debugging and diagnostic
|
|
|
|
/// assertion. <tt>CRYPTOPP_ASSERT</tt> is enabled by the preprocessor macros
|
|
|
|
/// <tt>CRYPTOPP_DEBUG</tt>, <tt>DEBUG</tt> or <tt>_DEBUG</tt>.
|
|
|
|
/// \details <tt>CRYPTOPP_ASSERT</tt> raises a <tt>SIGTRAP</tt> (Unix) or calls
|
|
|
|
/// <tt>DebugBreak()</tt> (Windows). <tt>CRYPTOPP_ASSERT</tt> is only in effect
|
|
|
|
/// when the user explicitly requests a debug configuration.
|
|
|
|
/// \details If you want to ensure <tt>CRYPTOPP_ASSERT</tt> is inert, then <em>do
|
|
|
|
/// not</em> define <tt>CRYPTOPP_DEBUG</tt>, <tt>DEBUG</tt> or <tt>_DEBUG</tt>.
|
|
|
|
/// Avoiding the defines means <tt>CRYPTOPP_ASSERT</tt> is preprocessed into an
|
|
|
|
/// empty string.
|
|
|
|
/// \details The traditional Posix define <tt>NDEBUG</tt> has no effect on
|
|
|
|
/// <tt>CRYPTOPP_DEBUG</tt>, <tt>CRYPTOPP_ASSERT</tt> or DebugTrapHandler.
|
|
|
|
/// \details An example of using CRYPTOPP_ASSERT and DebugTrapHandler is shown
|
|
|
|
/// below. The library's test program, <tt>cryptest.exe</tt> (from test.cpp),
|
|
|
|
/// exercises the structure:
|
2017-11-29 15:54:33 +00:00
|
|
|
/// <pre>
|
2019-02-01 00:12:27 +00:00
|
|
|
/// \#if defined(CRYPTOPP_DEBUG) && defined(UNIX_SIGNALS_AVAILABLE)
|
2017-11-29 15:54:33 +00:00
|
|
|
/// static const DebugTrapHandler g_dummyHandler;
|
2019-02-01 00:12:27 +00:00
|
|
|
/// \#endif
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
|
|
|
/// int main(int argc, char* argv[])
|
|
|
|
/// {
|
|
|
|
/// CRYPTOPP_ASSERT(argv != nullptr);
|
|
|
|
/// ...
|
|
|
|
/// }
|
|
|
|
/// </pre>
|
|
|
|
/// \since Crypto++ 5.6.5
|
2019-01-31 23:55:50 +00:00
|
|
|
/// \sa DebugTrapHandler, SignalHandler, <A
|
|
|
|
/// HREF="http://github.com/weidai11/cryptopp/issues/277">Issue 277</A>,
|
2017-11-29 15:54:33 +00:00
|
|
|
/// <A HREF="http://seclists.org/oss-sec/2016/q3/520">CVE-2016-7420</A>
|
2016-09-16 15:27:15 +00:00
|
|
|
# define CRYPTOPP_ASSERT(exp) { ... }
|
|
|
|
#endif
|
2015-07-26 19:26:47 +00:00
|
|
|
|
2016-10-18 02:00:31 +00:00
|
|
|
#if defined(CRYPTOPP_DEBUG) && defined(UNIX_SIGNALS_AVAILABLE)
|
2015-07-22 06:11:52 +00:00
|
|
|
# define CRYPTOPP_ASSERT(exp) { \
|
2015-08-03 23:17:42 +00:00
|
|
|
if (!(exp)) { \
|
2015-07-22 06:11:52 +00:00
|
|
|
std::ostringstream oss; \
|
2018-05-10 23:59:21 +00:00
|
|
|
oss << "Assertion failed: " << __FILE__ << "(" \
|
|
|
|
<< __LINE__ << "): " << __func__ \
|
2015-07-22 06:11:52 +00:00
|
|
|
<< std::endl; \
|
|
|
|
std::cerr << oss.str(); \
|
|
|
|
raise(SIGTRAP); \
|
|
|
|
} \
|
|
|
|
}
|
2016-10-14 19:00:49 +00:00
|
|
|
#elif CRYPTOPP_DEBUG && defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(__CYGWIN__)
|
2015-08-03 23:17:42 +00:00
|
|
|
# define CRYPTOPP_ASSERT(exp) { \
|
|
|
|
if (!(exp)) { \
|
|
|
|
std::ostringstream oss; \
|
2018-05-10 23:59:21 +00:00
|
|
|
oss << "Assertion failed: " << __FILE__ << "(" \
|
|
|
|
<< __LINE__ << "): " << __FUNCTION__ \
|
2015-08-03 23:17:42 +00:00
|
|
|
<< std::endl; \
|
|
|
|
std::cerr << oss.str(); \
|
2017-05-29 04:43:20 +00:00
|
|
|
if (IsDebuggerPresent()) {DebugBreak();} \
|
2015-08-03 23:17:42 +00:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
#endif // DEBUG and Unix or Windows
|
2015-07-22 06:11:52 +00:00
|
|
|
|
2016-09-16 15:27:15 +00:00
|
|
|
// Remove CRYPTOPP_ASSERT in non-debug builds.
|
|
|
|
// Can't use CRYPTOPP_UNUSED due to circular dependency
|
|
|
|
#ifndef CRYPTOPP_ASSERT
|
2017-01-04 17:47:04 +00:00
|
|
|
# define CRYPTOPP_ASSERT(exp) (void)0
|
2015-08-03 23:17:42 +00:00
|
|
|
#endif
|
2015-07-22 06:11:52 +00:00
|
|
|
|
2016-09-16 15:27:15 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
|
|
|
// ************** SIGTRAP handler ***************
|
|
|
|
|
2016-10-14 19:00:49 +00:00
|
|
|
#if (CRYPTOPP_DEBUG && defined(UNIX_SIGNALS_AVAILABLE)) || defined(CRYPTOPP_DOXYGEN_PROCESSING)
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Default SIGTRAP handler
|
2019-02-01 00:01:58 +00:00
|
|
|
/// \details DebugTrapHandler() can be used by a program to install an empty
|
2019-02-01 00:04:44 +00:00
|
|
|
/// SIGTRAP handler. If present, the handler ensures there is a signal
|
|
|
|
/// handler in place for <tt>SIGTRAP</tt> raised by
|
|
|
|
/// <tt>CRYPTOPP_ASSERT</tt>. If <tt>CRYPTOPP_ASSERT</tt> raises
|
|
|
|
/// <tt>SIGTRAP</tt> <em>without</em> a handler, then one of two things can
|
|
|
|
/// occur. First, the OS might allow the program to continue. Second, the OS
|
|
|
|
/// might terminate the program. OS X allows the program to continue, while
|
|
|
|
/// some Linuxes terminate the program.
|
|
|
|
/// \details If DebugTrapHandler detects another handler in place, then it will
|
|
|
|
/// not install a handler. This ensures a debugger can gain control of the
|
2019-02-01 00:01:58 +00:00
|
|
|
/// <tt>SIGTRAP</tt> signal without contention. It also allows multiple
|
|
|
|
/// DebugTrapHandler to be created without contentious or unusual behavior.
|
2019-02-01 00:04:44 +00:00
|
|
|
/// Though multiple DebugTrapHandler can be created, a program should only
|
|
|
|
/// create one, if needed.
|
|
|
|
/// \details A DebugTrapHandler is subject to C++ static initialization
|
|
|
|
/// [dis]order. If you need to install a handler and it must be installed
|
|
|
|
/// early, then reference the code associated with
|
|
|
|
/// <tt>CRYPTOPP_INIT_PRIORITY</tt> in cryptlib.cpp and cpu.cpp.
|
2019-02-01 00:01:58 +00:00
|
|
|
/// \details If you want to ensure <tt>CRYPTOPP_ASSERT</tt> is inert, then
|
|
|
|
/// <em>do not</em> define <tt>CRYPTOPP_DEBUG</tt>, <tt>DEBUG</tt> or
|
|
|
|
/// <tt>_DEBUG</tt>. Avoiding the defines means <tt>CRYPTOPP_ASSERT</tt>
|
2017-11-29 15:54:33 +00:00
|
|
|
/// is processed into <tt>((void)(exp))</tt>.
|
2019-02-01 00:01:58 +00:00
|
|
|
/// \details The traditional Posix define <tt>NDEBUG</tt> has no effect on
|
|
|
|
/// <tt>CRYPTOPP_DEBUG</tt>, <tt>CRYPTOPP_ASSERT</tt> or DebugTrapHandler.
|
|
|
|
/// \details An example of using \ref CRYPTOPP_ASSERT "CRYPTOPP_ASSERT" and
|
|
|
|
/// DebugTrapHandler is shown below. The library's test program,
|
|
|
|
/// <tt>cryptest.exe</tt> (from test.cpp), exercises the structure:
|
2017-11-29 15:54:33 +00:00
|
|
|
/// <pre>
|
2019-02-01 00:12:27 +00:00
|
|
|
/// \#if defined(CRYPTOPP_DEBUG) && defined(UNIX_SIGNALS_AVAILABLE)
|
2017-11-29 15:54:33 +00:00
|
|
|
/// static const DebugTrapHandler g_dummyHandler;
|
2019-02-01 00:12:27 +00:00
|
|
|
/// \#endif
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
|
|
|
/// int main(int argc, char* argv[])
|
|
|
|
/// {
|
|
|
|
/// CRYPTOPP_ASSERT(argv != nullptr);
|
|
|
|
/// ...
|
|
|
|
/// }
|
|
|
|
/// </pre>
|
|
|
|
/// \since Crypto++ 5.6.5
|
2019-02-01 00:01:58 +00:00
|
|
|
/// \sa \ref CRYPTOPP_ASSERT "CRYPTOPP_ASSERT", SignalHandler, <A
|
|
|
|
/// HREF="http://github.com/weidai11/cryptopp/issues/277">Issue 277</A>,
|
2017-11-29 15:54:33 +00:00
|
|
|
/// <A HREF="http://seclists.org/oss-sec/2016/q3/520">CVE-2016-7420</A>
|
2016-09-16 15:27:15 +00:00
|
|
|
|
2016-09-17 16:23:02 +00:00
|
|
|
#if defined(CRYPTOPP_DOXYGEN_PROCESSING)
|
|
|
|
class DebugTrapHandler : public SignalHandler<SIGILL, false> { };
|
|
|
|
#else
|
|
|
|
typedef SignalHandler<SIGILL, false> DebugTrapHandler;
|
|
|
|
#endif
|
2016-09-16 15:27:15 +00:00
|
|
|
|
2016-09-17 16:23:02 +00:00
|
|
|
#endif // Linux, Unix and Documentation
|
2016-09-16 15:27:15 +00:00
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
2015-08-03 23:17:42 +00:00
|
|
|
#endif // CRYPTOPP_TRAP_H
|