2018-05-17 07:31:43 +02:00
|
|
|
#include <gmock/gmock.h>
|
2018-05-19 07:29:41 +02:00
|
|
|
#include <csignal>
|
2018-05-17 07:31:43 +02:00
|
|
|
#include "cpp-utils/assert/backtrace.h"
|
2018-08-02 01:29:14 +02:00
|
|
|
#include "cpp-utils/process/subprocess.h"
|
2019-03-24 06:01:26 +01:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include "my-gtest-main.h"
|
2018-05-17 07:31:43 +02:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using testing::HasSubstr;
|
2019-03-24 06:01:26 +01:00
|
|
|
namespace bf = boost::filesystem;
|
2018-05-17 07:31:43 +02:00
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
std::string call_process_exiting_with(const std::string &kind, const std::string &signal = "")
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
#if defined(_MSC_VER)
|
2021-12-09 15:06:45 +01:00
|
|
|
auto executable = bf::canonical(get_executable().parent_path()) / "cpp-utils-test_exit_signal.exe";
|
2018-08-02 01:29:14 +02:00
|
|
|
#else
|
2021-12-09 15:06:45 +01:00
|
|
|
auto executable = bf::canonical(get_executable().parent_path()) / "cpp-utils-test_exit_signal";
|
2018-08-02 01:29:14 +02:00
|
|
|
#endif
|
2021-12-09 15:06:45 +01:00
|
|
|
if (!bf::exists(executable))
|
|
|
|
{
|
2019-03-24 06:01:26 +01:00
|
|
|
throw std::runtime_error(executable.string() + " not found.");
|
|
|
|
}
|
2021-12-12 13:00:07 +01:00
|
|
|
auto result = cpputils::Subprocess::call(executable, {kind, signal}, "");
|
2021-12-09 15:06:45 +01:00
|
|
|
return result.output_stderr;
|
2018-08-02 01:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 01:45:30 +01:00
|
|
|
#if !(defined(_MSC_VER) && defined(NDEBUG))
|
2019-04-02 04:18:49 +02:00
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ContainsTopLevelLine)
|
|
|
|
{
|
|
|
|
string backtrace = cpputils::backtrace();
|
|
|
|
EXPECT_THAT(backtrace, HasSubstr("BacktraceTest"));
|
|
|
|
EXPECT_THAT(backtrace, HasSubstr("ContainsTopLevelLine"));
|
2018-05-17 07:31:43 +02:00
|
|
|
}
|
2019-01-24 01:45:30 +01:00
|
|
|
#endif
|
2018-08-02 01:29:14 +02:00
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
namespace
|
|
|
|
{
|
2023-06-22 05:33:00 +02:00
|
|
|
#if !(defined(__clang__) && defined(NDEBUG))
|
2021-12-09 15:06:45 +01:00
|
|
|
std::string call_process_exiting_with_nullptr_violation()
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
return call_process_exiting_with("nullptr");
|
|
|
|
}
|
2023-06-22 05:33:00 +02:00
|
|
|
#endif
|
2021-12-09 15:06:45 +01:00
|
|
|
std::string call_process_exiting_with_exception(const std::string &message)
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
return call_process_exiting_with("exception", message);
|
2018-07-09 04:34:08 +02:00
|
|
|
}
|
2018-05-19 07:29:41 +02:00
|
|
|
}
|
2018-07-09 04:34:08 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#include <Windows.h>
|
2021-12-09 15:06:45 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
std::string call_process_exiting_with_sigsegv()
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
return call_process_exiting_with("signal", std::to_string(EXCEPTION_ACCESS_VIOLATION));
|
2018-07-09 04:34:08 +02:00
|
|
|
}
|
2021-12-09 15:06:45 +01:00
|
|
|
std::string call_process_exiting_with_sigill()
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
return call_process_exiting_with("signal", std::to_string(EXCEPTION_ILLEGAL_INSTRUCTION));
|
2018-07-09 04:34:08 +02:00
|
|
|
}
|
2021-12-09 15:06:45 +01:00
|
|
|
std::string call_process_exiting_with_code(DWORD code)
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
return call_process_exiting_with("signal", std::to_string(code));
|
2018-07-09 04:34:08 +02:00
|
|
|
}
|
2018-05-17 07:42:03 +02:00
|
|
|
}
|
2018-07-09 04:34:08 +02:00
|
|
|
#else
|
2021-12-09 15:06:45 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
std::string call_process_exiting_with_sigsegv()
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
return call_process_exiting_with("signal", std::to_string(SIGSEGV));
|
2018-07-09 04:34:08 +02:00
|
|
|
}
|
2021-12-09 15:06:45 +01:00
|
|
|
std::string call_process_exiting_with_sigabrt()
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
return call_process_exiting_with("signal", std::to_string(SIGABRT));
|
2018-07-09 04:34:08 +02:00
|
|
|
}
|
2021-12-09 15:06:45 +01:00
|
|
|
std::string call_process_exiting_with_sigill()
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
return call_process_exiting_with("signal", std::to_string(SIGILL));
|
2018-07-09 04:34:08 +02:00
|
|
|
}
|
2018-05-17 07:42:03 +02:00
|
|
|
}
|
2018-07-09 04:34:08 +02:00
|
|
|
#endif
|
2018-05-17 07:42:03 +02:00
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, DoesntCrashOnCaughtException)
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
// This is needed to make sure we don't use some kind of vectored exception handler on Windows
|
|
|
|
// that ignores the call stack and always jumps on when an exception happens.
|
|
|
|
cpputils::showBacktraceOnCrash();
|
2021-12-09 15:06:45 +01:00
|
|
|
try
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
throw std::logic_error("exception");
|
2021-12-09 15:06:45 +01:00
|
|
|
}
|
|
|
|
catch (const std::logic_error &e)
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
// intentionally empty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 01:45:30 +01:00
|
|
|
#if !(defined(_MSC_VER) && defined(NDEBUG))
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ContainsBacktrace)
|
|
|
|
{
|
|
|
|
string backtrace = cpputils::backtrace();
|
2019-04-02 04:18:49 +02:00
|
|
|
#if defined(_MSC_VER)
|
2021-12-09 15:06:45 +01:00
|
|
|
EXPECT_THAT(backtrace, HasSubstr("testing::Test::Run"));
|
2019-04-02 04:18:49 +02:00
|
|
|
#else
|
2021-12-09 15:06:45 +01:00
|
|
|
EXPECT_THAT(backtrace, HasSubstr("BacktraceTest_ContainsBacktrace_Test::TestBody"));
|
2019-04-02 04:18:49 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-06-22 05:33:00 +02:00
|
|
|
#if !(defined(__clang__) && defined(NDEBUG))
|
|
|
|
// TODO Can we also make this work on clang in Release mode?
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnNullptrAccess)
|
|
|
|
{
|
|
|
|
auto output = call_process_exiting_with_nullptr_violation();
|
2019-04-02 04:18:49 +02:00
|
|
|
#if defined(_MSC_VER)
|
2021-12-09 15:06:45 +01:00
|
|
|
EXPECT_THAT(output, HasSubstr("handle_exit_signal"));
|
2019-04-02 04:18:49 +02:00
|
|
|
#else
|
2021-12-09 15:06:45 +01:00
|
|
|
EXPECT_THAT(output, HasSubstr("cpputils::backtrace"));
|
2019-02-28 08:34:49 +01:00
|
|
|
#endif
|
2018-05-17 07:42:03 +02:00
|
|
|
}
|
2023-06-22 05:33:00 +02:00
|
|
|
#endif
|
2018-05-19 07:29:41 +02:00
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigSegv)
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
auto output = call_process_exiting_with_sigsegv();
|
2019-02-28 08:34:49 +01:00
|
|
|
#if defined(_MSC_VER)
|
2021-12-09 15:06:45 +01:00
|
|
|
EXPECT_THAT(output, HasSubstr("handle_exit_signal"));
|
2019-02-28 08:34:49 +01:00
|
|
|
#else
|
2021-12-09 15:06:45 +01:00
|
|
|
EXPECT_THAT(output, HasSubstr("cpputils::backtrace"));
|
2019-02-28 08:34:49 +01:00
|
|
|
#endif
|
2018-08-02 01:29:14 +02:00
|
|
|
}
|
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnUnhandledException)
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
auto output = call_process_exiting_with_exception("my_exception_message");
|
2019-02-28 08:34:49 +01:00
|
|
|
#if defined(_MSC_VER)
|
2021-12-09 15:06:45 +01:00
|
|
|
EXPECT_THAT(output, HasSubstr("handle_exit_signal"));
|
2019-02-28 08:34:49 +01:00
|
|
|
#else
|
2021-12-09 15:06:45 +01:00
|
|
|
EXPECT_THAT(output, HasSubstr("cpputils::backtrace"));
|
2019-02-28 08:34:49 +01:00
|
|
|
#endif
|
2018-05-19 07:29:41 +02:00
|
|
|
}
|
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigIll)
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
auto output = call_process_exiting_with_sigill();
|
2019-02-28 08:34:49 +01:00
|
|
|
#if defined(_MSC_VER)
|
2021-12-09 15:06:45 +01:00
|
|
|
EXPECT_THAT(output, HasSubstr("handle_exit_signal"));
|
2019-02-28 08:34:49 +01:00
|
|
|
#else
|
2021-12-09 15:06:45 +01:00
|
|
|
EXPECT_THAT(output, HasSubstr("cpputils::backtrace"));
|
2019-02-28 08:34:49 +01:00
|
|
|
#endif
|
2018-07-09 04:34:08 +02:00
|
|
|
}
|
2019-01-24 01:45:30 +01:00
|
|
|
#else
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ContainsBacktrace)
|
|
|
|
{
|
2019-04-02 04:18:49 +02:00
|
|
|
string backtrace = cpputils::backtrace();
|
2019-11-01 07:50:55 +01:00
|
|
|
EXPECT_THAT(backtrace, HasSubstr("#0"));
|
2019-04-02 04:18:49 +02:00
|
|
|
}
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnNullptrAccess)
|
|
|
|
{
|
2019-01-24 01:45:30 +01:00
|
|
|
auto output = call_process_exiting_with_nullptr_violation();
|
|
|
|
EXPECT_THAT(output, HasSubstr("#1"));
|
|
|
|
}
|
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigSegv)
|
|
|
|
{
|
2019-01-24 01:45:30 +01:00
|
|
|
auto output = call_process_exiting_with_sigsegv();
|
|
|
|
EXPECT_THAT(output, HasSubstr("#1"));
|
|
|
|
}
|
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnUnhandledException)
|
|
|
|
{
|
2019-01-24 01:45:30 +01:00
|
|
|
auto output = call_process_exiting_with_exception("my_exception_message");
|
|
|
|
EXPECT_THAT(output, HasSubstr("#1"));
|
|
|
|
}
|
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigIll)
|
|
|
|
{
|
2019-01-24 01:45:30 +01:00
|
|
|
auto output = call_process_exiting_with_sigill();
|
|
|
|
EXPECT_THAT(output, HasSubstr("#1"));
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-09 04:34:08 +02:00
|
|
|
|
|
|
|
#if !defined(_MSC_VER)
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigAbrt)
|
|
|
|
{
|
|
|
|
auto output = call_process_exiting_with_sigabrt();
|
|
|
|
EXPECT_THAT(output, HasSubstr("cpputils::backtrace"));
|
2018-05-19 07:29:41 +02:00
|
|
|
}
|
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigAbrt_ShowsCorrectSignalName)
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
auto output = call_process_exiting_with_sigabrt();
|
|
|
|
EXPECT_THAT(output, HasSubstr("SIGABRT"));
|
2018-05-19 07:29:41 +02:00
|
|
|
}
|
2018-07-09 04:34:08 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(_MSC_VER)
|
2021-12-09 15:06:45 +01:00
|
|
|
constexpr const char *sigsegv_message = "SIGSEGV";
|
|
|
|
constexpr const char *sigill_message = "SIGILL";
|
2018-07-09 04:34:08 +02:00
|
|
|
#else
|
2021-12-09 15:06:45 +01:00
|
|
|
constexpr const char *sigsegv_message = "EXCEPTION_ACCESS_VIOLATION";
|
|
|
|
constexpr const char *sigill_message = "EXCEPTION_ILLEGAL_INSTRUCTION";
|
2018-07-09 04:34:08 +02:00
|
|
|
#endif
|
2018-05-19 07:29:41 +02:00
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigSegv_ShowsCorrectSignalName)
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
auto output = call_process_exiting_with_sigsegv();
|
|
|
|
EXPECT_THAT(output, HasSubstr(sigsegv_message));
|
2018-05-19 07:29:41 +02:00
|
|
|
}
|
|
|
|
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigIll_ShowsCorrectSignalName)
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
auto output = call_process_exiting_with_sigill();
|
|
|
|
EXPECT_THAT(output, HasSubstr(sigill_message));
|
2018-05-19 07:29:41 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 01:29:14 +02:00
|
|
|
#if !defined(_MSC_VER)
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnUnhandledException_ShowsCorrectExceptionMessage)
|
|
|
|
{
|
2018-08-02 01:29:14 +02:00
|
|
|
auto output = call_process_exiting_with_exception("my_exception_message");
|
|
|
|
EXPECT_THAT(output, HasSubstr("my_exception_message"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-09 04:34:08 +02:00
|
|
|
#if defined(_MSC_VER)
|
2021-12-09 15:06:45 +01:00
|
|
|
TEST(BacktraceTest, UnknownCode_ShowsCorrectSignalName)
|
|
|
|
{
|
2018-09-19 12:01:31 +02:00
|
|
|
auto output = call_process_exiting_with_code(0x1234567);
|
|
|
|
EXPECT_THAT(output, HasSubstr("UNKNOWN_CODE(0x1234567)"));
|
2018-05-19 07:29:41 +02:00
|
|
|
}
|
2018-07-09 04:34:08 +02:00
|
|
|
#endif
|