Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8e79ade

Browse files
authoredJan 22, 2025··
[libc][LIBC_ADD_NULL_CHECKS] replace volatile deref with __builtin_trap (#123401)
Also, update the unit tests that were checking for SIGSEGV to not check for a specific signal. To further improve this check, it may be worth: - renaming the configuration option/macro/docs to be clearer about intent. - swap __builtin_trap for __builtin_unreachable, removing the preprocessor variants of LIBC_CRASH_ON_NULLPTR, then unconditionally using `-fsanitize=unreachable -fsanitize-trap=unreachable` in cmake flags when LIBC_ADD_NULL_CHECKS is enabled. - building with `-fno-delete-null-pointer-checks` when LIBC_ADD_NULL_CHECKS (or when some larger yet to be added hardening config) is enabled. Link: #111546
1 parent ddb8607 commit 8e79ade

File tree

6 files changed

+12
-17
lines changed

6 files changed

+12
-17
lines changed
 

‎libc/src/__support/macros/null_check.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,10 @@
1414
#include "src/__support/macros/sanitizer.h"
1515

1616
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
17-
// Use volatile to prevent undefined behavior of dereferencing nullptr.
18-
// Intentionally crashing with SIGSEGV.
19-
#define LIBC_CRASH_ON_NULLPTR(PTR) \
17+
#define LIBC_CRASH_ON_NULLPTR(ptr) \
2018
do { \
21-
if (LIBC_UNLIKELY(PTR == nullptr)) { \
22-
volatile auto *crashing = PTR; \
23-
[[maybe_unused]] volatile auto crash = *crashing; \
19+
if (LIBC_UNLIKELY((ptr) == nullptr)) \
2420
__builtin_trap(); \
25-
} \
2621
} while (0)
2722
#else
2823
#define LIBC_CRASH_ON_NULLPTR(ptr) \

‎libc/test/src/math/smoke/nan_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ TEST_F(LlvmLibcNanTest, RandomString) {
4444
run_test("123 ", 0x7ff8000000000000);
4545
}
4646

47-
#if !defined(LIBC_HAS_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
47+
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
4848
TEST_F(LlvmLibcNanTest, InvalidInput) {
49-
EXPECT_DEATH([] { LIBC_NAMESPACE::nan(nullptr); }, WITH_SIGNAL(SIGSEGV));
49+
EXPECT_DEATH([] { LIBC_NAMESPACE::nan(nullptr); });
5050
}
5151
#endif // LIBC_HAS_ADDRESS_SANITIZER

‎libc/test/src/math/smoke/nanf128_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ TEST_F(LlvmLibcNanf128Test, RandomString) {
5555
QUIET_NAN);
5656
}
5757

58-
#if !defined(LIBC_HAS_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
58+
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
5959
TEST_F(LlvmLibcNanf128Test, InvalidInput) {
60-
EXPECT_DEATH([] { LIBC_NAMESPACE::nanf128(nullptr); }, WITH_SIGNAL(SIGSEGV));
60+
EXPECT_DEATH([] { LIBC_NAMESPACE::nanf128(nullptr); });
6161
}
6262
#endif // LIBC_HAS_ADDRESS_SANITIZER

‎libc/test/src/math/smoke/nanf16_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ TEST_F(LlvmLibcNanf16Test, RandomString) {
4343
run_test("123 ", 0x7e00);
4444
}
4545

46-
#if !defined(LIBC_HAS_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
46+
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
4747
TEST_F(LlvmLibcNanf16Test, InvalidInput) {
48-
EXPECT_DEATH([] { LIBC_NAMESPACE::nanf16(nullptr); }, WITH_SIGNAL(SIGSEGV));
48+
EXPECT_DEATH([] { LIBC_NAMESPACE::nanf16(nullptr); });
4949
}
5050
#endif // LIBC_HAS_ADDRESS_SANITIZER

‎libc/test/src/math/smoke/nanf_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ TEST_F(LlvmLibcNanfTest, RandomString) {
4343
run_test("123 ", 0x7fc00000);
4444
}
4545

46-
#if !defined(LIBC_HAS_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
46+
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
4747
TEST_F(LlvmLibcNanfTest, InvalidInput) {
48-
EXPECT_DEATH([] { LIBC_NAMESPACE::nanf(nullptr); }, WITH_SIGNAL(SIGSEGV));
48+
EXPECT_DEATH([] { LIBC_NAMESPACE::nanf(nullptr); });
4949
}
5050
#endif // LIBC_HAS_ADDRESS_SANITIZER

‎libc/test/src/math/smoke/nanl_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ TEST_F(LlvmLibcNanlTest, RandomString) {
7171
run_test("123 ", expected);
7272
}
7373

74-
#if !defined(LIBC_HAS_ADDRESS_SANITIZER) && defined(LIBC_TARGET_OS_IS_LINUX)
74+
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
7575
TEST_F(LlvmLibcNanlTest, InvalidInput) {
76-
EXPECT_DEATH([] { LIBC_NAMESPACE::nanl(nullptr); }, WITH_SIGNAL(SIGSEGV));
76+
EXPECT_DEATH([] { LIBC_NAMESPACE::nanl(nullptr); });
7777
}
7878
#endif // LIBC_HAS_ADDRESS_SANITIZER

0 commit comments

Comments
 (0)
Please sign in to comment.