[libc][NFC] Move aligned access implementations to separate header
[llvm-project.git] / lldb / unittests / Signals / UnixSignalsTest.cpp
blob2ae1b4ee5138f3c10fc2f6d6364b25ac8d5c37dc
1 //===-- UnixSignalsTest.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 #include <string>
10 #include "gtest/gtest.h"
12 #include "lldb/Target/UnixSignals.h"
13 #include "llvm/Support/FormatVariadic.h"
15 using namespace lldb;
16 using namespace lldb_private;
18 class TestSignals : public UnixSignals {
19 public:
20 TestSignals() {
21 m_signals.clear();
22 AddSignal(2, "SIG2", false, true, true, "DESC2");
23 AddSignal(4, "SIG4", true, false, true, "DESC4");
24 AddSignal(8, "SIG8", true, true, true, "DESC8");
25 AddSignal(16, "SIG16", true, false, false, "DESC16");
26 AddSignalCode(16, 1, "a specific type of SIG16");
27 AddSignalCode(16, 2, "SIG16 with a fault address",
28 SignalCodePrintOption::Address);
29 AddSignalCode(16, 3, "bounds violation", SignalCodePrintOption::Bounds);
33 void ExpectEqArrays(llvm::ArrayRef<int32_t> expected,
34 llvm::ArrayRef<int32_t> observed, const char *file,
35 int line) {
36 std::string location = llvm::formatv("{0}:{1}", file, line);
37 ASSERT_EQ(expected.size(), observed.size()) << location;
39 for (size_t i = 0; i < observed.size(); ++i) {
40 ASSERT_EQ(expected[i], observed[i])
41 << "array index: " << i << "location:" << location;
45 #define EXPECT_EQ_ARRAYS(expected, observed) \
46 ExpectEqArrays((expected), (observed), __FILE__, __LINE__);
48 TEST(UnixSignalsTest, Iteration) {
49 TestSignals signals;
51 EXPECT_EQ(4, signals.GetNumSignals());
52 EXPECT_EQ(2, signals.GetFirstSignalNumber());
53 EXPECT_EQ(4, signals.GetNextSignalNumber(2));
54 EXPECT_EQ(8, signals.GetNextSignalNumber(4));
55 EXPECT_EQ(16, signals.GetNextSignalNumber(8));
56 EXPECT_EQ(LLDB_INVALID_SIGNAL_NUMBER, signals.GetNextSignalNumber(16));
59 TEST(UnixSignalsTest, Reset) {
60 TestSignals signals;
61 bool stop_val = signals.GetShouldStop(2);
62 bool notify_val = signals.GetShouldNotify(2);
63 bool suppress_val = signals.GetShouldSuppress(2);
65 // Change two, then reset one and make sure only that one was reset:
66 EXPECT_EQ(true, signals.SetShouldNotify(2, !notify_val));
67 EXPECT_EQ(true, signals.SetShouldSuppress(2, !suppress_val));
68 EXPECT_EQ(true, signals.ResetSignal(2, false, true, false));
69 EXPECT_EQ(stop_val, signals.GetShouldStop(2));
70 EXPECT_EQ(notify_val, signals.GetShouldStop(2));
71 EXPECT_EQ(!suppress_val, signals.GetShouldNotify(2));
73 // Make sure reset with no arguments resets them all:
74 EXPECT_EQ(true, signals.SetShouldSuppress(2, !suppress_val));
75 EXPECT_EQ(true, signals.SetShouldNotify(2, !notify_val));
76 EXPECT_EQ(true, signals.ResetSignal(2));
77 EXPECT_EQ(stop_val, signals.GetShouldStop(2));
78 EXPECT_EQ(notify_val, signals.GetShouldNotify(2));
79 EXPECT_EQ(suppress_val, signals.GetShouldSuppress(2));
82 TEST(UnixSignalsTest, GetInfo) {
83 TestSignals signals;
85 bool should_suppress = false, should_stop = false, should_notify = false;
86 int32_t signo = 4;
87 std::string name =
88 signals.GetSignalInfo(signo, should_suppress, should_stop, should_notify);
89 EXPECT_EQ("SIG4", name);
90 EXPECT_EQ(true, should_suppress);
91 EXPECT_EQ(false, should_stop);
92 EXPECT_EQ(true, should_notify);
94 EXPECT_EQ(true, signals.GetShouldSuppress(signo));
95 EXPECT_EQ(false, signals.GetShouldStop(signo));
96 EXPECT_EQ(true, signals.GetShouldNotify(signo));
97 EXPECT_EQ(name, signals.GetSignalAsCString(signo));
100 TEST(UnixSignalsTest, GetAsCString) {
101 TestSignals signals;
103 ASSERT_EQ(nullptr, signals.GetSignalAsCString(100));
104 std::string name = signals.GetSignalAsCString(16);
105 ASSERT_EQ("SIG16", name);
108 TEST(UnixSignalsTest, GetAsString) {
109 TestSignals signals;
111 ASSERT_EQ("", signals.GetSignalDescription(100, std::nullopt));
112 ASSERT_EQ("SIG16", signals.GetSignalDescription(16, std::nullopt));
113 ASSERT_EQ("", signals.GetSignalDescription(100, 100));
114 ASSERT_EQ("SIG16", signals.GetSignalDescription(16, 100));
115 ASSERT_EQ("SIG16: a specific type of SIG16",
116 signals.GetSignalDescription(16, 1));
118 // Unknown code, won't use the address.
119 ASSERT_EQ("SIG16", signals.GetSignalDescription(16, 100, 0xCAFEF00D));
120 // Known code, that shouldn't print fault address.
121 ASSERT_EQ("SIG16: a specific type of SIG16",
122 signals.GetSignalDescription(16, 1, 0xCAFEF00D));
123 // Known code that should.
124 ASSERT_EQ("SIG16: SIG16 with a fault address (fault address: 0xcafef00d)",
125 signals.GetSignalDescription(16, 2, 0xCAFEF00D));
126 // No address given just print the code description.
127 ASSERT_EQ("SIG16: SIG16 with a fault address",
128 signals.GetSignalDescription(16, 2));
130 const char *expected = "SIG16: bounds violation";
131 // Must pass all needed info to get full output.
132 ASSERT_EQ(expected, signals.GetSignalDescription(16, 3));
133 ASSERT_EQ(expected, signals.GetSignalDescription(16, 3, 0xcafef00d));
134 ASSERT_EQ(expected, signals.GetSignalDescription(16, 3, 0xcafef00d, 0x1234));
136 ASSERT_EQ("SIG16: upper bound violation (fault address: 0x5679, lower bound: "
137 "0x1234, upper bound: 0x5678)",
138 signals.GetSignalDescription(16, 3, 0x5679, 0x1234, 0x5678));
139 ASSERT_EQ("SIG16: lower bound violation (fault address: 0x1233, lower bound: "
140 "0x1234, upper bound: 0x5678)",
141 signals.GetSignalDescription(16, 3, 0x1233, 0x1234, 0x5678));
144 TEST(UnixSignalsTest, VersionChange) {
145 TestSignals signals;
147 int32_t signo = 8;
148 uint64_t ver = signals.GetVersion();
149 EXPECT_GT(ver, 0ull);
150 EXPECT_EQ(true, signals.GetShouldSuppress(signo));
151 EXPECT_EQ(true, signals.GetShouldStop(signo));
152 EXPECT_EQ(true, signals.GetShouldNotify(signo));
154 EXPECT_EQ(signals.GetVersion(), ver);
156 signals.SetShouldSuppress(signo, false);
157 EXPECT_LT(ver, signals.GetVersion());
158 ver = signals.GetVersion();
160 signals.SetShouldStop(signo, true);
161 EXPECT_LT(ver, signals.GetVersion());
162 ver = signals.GetVersion();
164 signals.SetShouldNotify(signo, false);
165 EXPECT_LT(ver, signals.GetVersion());
166 ver = signals.GetVersion();
168 EXPECT_EQ(false, signals.GetShouldSuppress(signo));
169 EXPECT_EQ(true, signals.GetShouldStop(signo));
170 EXPECT_EQ(false, signals.GetShouldNotify(signo));
172 EXPECT_EQ(ver, signals.GetVersion());
175 TEST(UnixSignalsTest, GetFilteredSignals) {
176 TestSignals signals;
178 auto all_signals =
179 signals.GetFilteredSignals(std::nullopt, std::nullopt, std::nullopt);
180 std::vector<int32_t> expected = {2, 4, 8, 16};
181 EXPECT_EQ_ARRAYS(expected, all_signals);
183 auto supressed = signals.GetFilteredSignals(true, std::nullopt, std::nullopt);
184 expected = {4, 8, 16};
185 EXPECT_EQ_ARRAYS(expected, supressed);
187 auto not_supressed =
188 signals.GetFilteredSignals(false, std::nullopt, std::nullopt);
189 expected = {2};
190 EXPECT_EQ_ARRAYS(expected, not_supressed);
192 auto stopped = signals.GetFilteredSignals(std::nullopt, true, std::nullopt);
193 expected = {2, 8};
194 EXPECT_EQ_ARRAYS(expected, stopped);
196 auto not_stopped =
197 signals.GetFilteredSignals(std::nullopt, false, std::nullopt);
198 expected = {4, 16};
199 EXPECT_EQ_ARRAYS(expected, not_stopped);
201 auto notified = signals.GetFilteredSignals(std::nullopt, std::nullopt, true);
202 expected = {2, 4, 8};
203 EXPECT_EQ_ARRAYS(expected, notified);
205 auto not_notified =
206 signals.GetFilteredSignals(std::nullopt, std::nullopt, false);
207 expected = {16};
208 EXPECT_EQ_ARRAYS(expected, not_notified);
210 auto signal4 = signals.GetFilteredSignals(true, false, true);
211 expected = {4};
212 EXPECT_EQ_ARRAYS(expected, signal4);