Skip to content

Commit 7147b8f

Browse files
committed
Default to __builtin_trap() when possible instead of abort()
abort() raises SIGABRT which may run more code in process, while __builtin_trap may generate a trap that debuggers can catch in debug builds and produces smaller codegen (no function call). See also: https://discourse.llvm.org/t/rfc-hardening-in-libc/73925#termination-2
1 parent cc38848 commit 7147b8f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

sus/assertions/panic.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ void print_panic_location(const std::source_location& location) noexcept;
5858

5959
/// Terminate the program.
6060
///
61-
/// The default behaviour of this function is to `abort()`. The behaviour of
62-
/// this function can be overridden by defining a `SUS_PROVIDE_PANIC_HANDLER()`
63-
/// macro when compiling the library.
61+
/// The default behaviour of this function is to `__builtin_trap()` when
62+
/// possible and `abort() otherwise`. The behaviour of this function can be
63+
/// overridden by defining a `SUS_PROVIDE_PANIC_HANDLER()` macro when compiling
64+
/// the library.
6465
///
6566
/// The panic message will be printed to stderr before aborting. This behaviour
6667
/// can be overridden by defining a `SUS_PROVIDE_PRINT_PANIC_LOCATION_HANDLER()`
67-
/// macro when compiling the library.
68+
/// macro when compiling. The same handler must be used as when building the
69+
/// library itself. So if used as a shared library, it can not be modified by
70+
/// the calling code.
6871
///
6972
/// # Safety
7073
///
@@ -78,8 +81,11 @@ void print_panic_location(const std::source_location& location) noexcept;
7881
#else
7982
__private::print_panic_location(location);
8083
#endif
84+
8185
#if defined(SUS_PROVIDE_PANIC_HANDLER)
8286
SUS_PROVIDE_PANIC_HANDLER();
87+
#elif __has_builtin(__builtin_trap)
88+
__builtin_trap();
8389
#else
8490
abort();
8591
#endif

0 commit comments

Comments
 (0)