1 //===-- sanitizer_block_signals.cpp ---------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of sanitizer_common unit tests.
11 //===----------------------------------------------------------------------===//
15 #include "gtest/gtest.h"
16 #include "sanitizer_common/sanitizer_linux.h"
18 namespace __sanitizer
{
20 #if SANITIZER_LINUX && !SANITIZER_ANDROID
21 volatile int received_sig
= -1;
23 void signal_handler(int signum
) { received_sig
= signum
; }
25 TEST(SanitizerCommon
, NoBlockSignals
) {
27 signal(SIGUSR1
, signal_handler
);
29 EXPECT_EQ(received_sig
, SIGUSR1
);
32 signal(SIGPIPE
, signal_handler
);
34 EXPECT_EQ(received_sig
, SIGPIPE
);
37 TEST(SanitizerCommon
, BlockSignalsPlain
) {
38 // ScopedBlockSignals; SIGUSR1 should be blocked but not SIGPIPE
40 __sanitizer_sigset_t sigset
= {};
41 ScopedBlockSignals
block(&sigset
);
44 signal(SIGUSR1
, signal_handler
);
46 EXPECT_EQ(received_sig
, -1);
49 signal(SIGPIPE
, signal_handler
);
51 EXPECT_EQ(received_sig
, SIGPIPE
);
53 EXPECT_EQ(received_sig
, SIGUSR1
);
56 TEST(SanitizerCommon
, BlockSignalsExceptPipe
) {
57 // Manually block SIGPIPE; ScopedBlockSignals should not unblock this
58 sigset_t block_sigset
;
59 sigemptyset(&block_sigset
);
60 sigaddset(&block_sigset
, SIGPIPE
);
61 sigprocmask(SIG_BLOCK
, &block_sigset
, NULL
);
63 __sanitizer_sigset_t sigset
= {};
64 ScopedBlockSignals
block(&sigset
);
67 signal(SIGPIPE
, signal_handler
);
69 EXPECT_EQ(received_sig
, -1);
71 sigprocmask(SIG_UNBLOCK
, &block_sigset
, NULL
);
72 EXPECT_EQ(received_sig
, SIGPIPE
);
74 #endif // SANITIZER_LINUX && !SANITIZER_ANDROID
76 } // namespace __sanitizer