Skip to content

Commit

Permalink
Don't allow internal_error to pass an error test (#8458)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreinking authored Nov 6, 2024
1 parent 72749c5 commit 2e73b3c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmake/HalideTestHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ if (NOT TARGET Halide::ExpectAbort)
# Add an OBJECT (not static) library to convert abort calls into exit(1).
add_library(Halide_expect_abort OBJECT ${Halide_SOURCE_DIR}/test/common/expect_abort.cpp)
add_library(Halide::ExpectAbort ALIAS Halide_expect_abort)
target_link_libraries(Halide_expect_abort PRIVATE Halide::Halide)
endif ()

if (NOT TARGET Halide::TerminateHandler)
Expand Down
41 changes: 31 additions & 10 deletions test/common/expect_abort.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
#include <Halide.h>

#include <csignal>
#include <cstdlib>
#include <exception>
#include <iostream>

// This is a hack to implement death tests in CTest.
extern "C" void hl_error_test_handle_abort(int) {
std::_Exit(EXIT_FAILURE);
}
std::atomic<bool> suppress_abort{true};

struct hl_override_abort {
hl_override_abort() noexcept {
std::signal(SIGABRT, hl_error_test_handle_abort);
}
};
auto handler = ([]() {
#ifdef HALIDE_WITH_EXCEPTIONS
std::set_terminate([]() { //
try {
if (const auto e = std::current_exception()) {
std::rethrow_exception(e);
}
} catch (const Halide::InternalError &e) {
std::cerr << e.what() << "\n"
<< std::flush;
suppress_abort = false;
std::abort(); // We should never EXPECT an internal error
} catch (const std::exception &e) {
std::cerr << e.what() << "\n"
<< std::flush;
} catch (...) {}
std::_Exit(EXIT_FAILURE);
});
#endif

hl_override_abort handler{};
// If exceptions are disabled, we just hope for the best.
return std::signal(SIGABRT, [](int) {
if (suppress_abort) {
std::_Exit(EXIT_FAILURE);
}
});
})();

0 comments on commit 2e73b3c

Please sign in to comment.