Android defaults to pic (#123955)
[llvm-project.git] / compiler-rt / lib / sanitizer_common / tests / sanitizer_block_signals.cpp
blobb43648a8aef2304d7c1f13092f7a2d931578fff1
1 //===-- sanitizer_block_signals.cpp ---------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of sanitizer_common unit tests.
11 //===----------------------------------------------------------------------===//
12 #include <signal.h>
13 #include <stdio.h>
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) {
26 // No signals blocked
27 signal(SIGUSR1, signal_handler);
28 raise(SIGUSR1);
29 EXPECT_EQ(received_sig, SIGUSR1);
31 received_sig = -1;
32 signal(SIGPIPE, signal_handler);
33 raise(SIGPIPE);
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);
43 received_sig = -1;
44 signal(SIGUSR1, signal_handler);
45 raise(SIGUSR1);
46 EXPECT_EQ(received_sig, -1);
48 received_sig = -1;
49 signal(SIGPIPE, signal_handler);
50 raise(SIGPIPE);
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);
66 received_sig = -1;
67 signal(SIGPIPE, signal_handler);
68 raise(SIGPIPE);
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