File: UnixSignalsTest.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (213 lines) | stat: -rw-r--r-- 7,746 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//===-- UnixSignalsTest.cpp -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <string>

#include "gtest/gtest.h"

#include "lldb/Target/UnixSignals.h"
#include "llvm/Support/FormatVariadic.h"

using namespace lldb;
using namespace lldb_private;

class TestSignals : public UnixSignals {
public:
  TestSignals() {
    m_signals.clear();
    AddSignal(2, "SIG2", false, true, true, "DESC2");
    AddSignal(4, "SIG4", true, false, true, "DESC4");
    AddSignal(8, "SIG8", true, true, true, "DESC8");
    AddSignal(16, "SIG16", true, false, false, "DESC16");
    AddSignalCode(16, 1, "a specific type of SIG16");
    AddSignalCode(16, 2, "SIG16 with a fault address",
                  SignalCodePrintOption::Address);
    AddSignalCode(16, 3, "bounds violation", SignalCodePrintOption::Bounds);
  }
};

void ExpectEqArrays(llvm::ArrayRef<int32_t> expected,
                    llvm::ArrayRef<int32_t> observed, const char *file,
                    int line) {
  std::string location = llvm::formatv("{0}:{1}", file, line);
  ASSERT_EQ(expected.size(), observed.size()) << location;

  for (size_t i = 0; i < observed.size(); ++i) {
    ASSERT_EQ(expected[i], observed[i])
        << "array index: " << i << "location:" << location;
  }
}

#define EXPECT_EQ_ARRAYS(expected, observed)                                   \
  ExpectEqArrays((expected), (observed), __FILE__, __LINE__);

TEST(UnixSignalsTest, Iteration) {
  TestSignals signals;

  EXPECT_EQ(4, signals.GetNumSignals());
  EXPECT_EQ(2, signals.GetFirstSignalNumber());
  EXPECT_EQ(4, signals.GetNextSignalNumber(2));
  EXPECT_EQ(8, signals.GetNextSignalNumber(4));
  EXPECT_EQ(16, signals.GetNextSignalNumber(8));
  EXPECT_EQ(LLDB_INVALID_SIGNAL_NUMBER, signals.GetNextSignalNumber(16));
}

TEST(UnixSignalsTest, Reset) {
  TestSignals signals;
  bool stop_val     = signals.GetShouldStop(2);
  bool notify_val   = signals.GetShouldNotify(2);
  bool suppress_val = signals.GetShouldSuppress(2);
  
  // Change two, then reset one and make sure only that one was reset:
  EXPECT_EQ(true, signals.SetShouldNotify(2, !notify_val));
  EXPECT_EQ(true, signals.SetShouldSuppress(2, !suppress_val));
  EXPECT_EQ(true, signals.ResetSignal(2, false, true, false));
  EXPECT_EQ(stop_val, signals.GetShouldStop(2));
  EXPECT_EQ(notify_val, signals.GetShouldStop(2));
  EXPECT_EQ(!suppress_val, signals.GetShouldNotify(2));
  
  // Make sure reset with no arguments resets them all:
  EXPECT_EQ(true, signals.SetShouldSuppress(2, !suppress_val));
  EXPECT_EQ(true, signals.SetShouldNotify(2, !notify_val));
  EXPECT_EQ(true, signals.ResetSignal(2));
  EXPECT_EQ(stop_val, signals.GetShouldStop(2));
  EXPECT_EQ(notify_val, signals.GetShouldNotify(2));
  EXPECT_EQ(suppress_val, signals.GetShouldSuppress(2));
}

TEST(UnixSignalsTest, GetInfo) {
  TestSignals signals;

  bool should_suppress = false, should_stop = false, should_notify = false;
  int32_t signo = 4;
  std::string name =
      signals.GetSignalInfo(signo, should_suppress, should_stop, should_notify);
  EXPECT_EQ("SIG4", name);
  EXPECT_EQ(true, should_suppress);
  EXPECT_EQ(false, should_stop);
  EXPECT_EQ(true, should_notify);

  EXPECT_EQ(true, signals.GetShouldSuppress(signo));
  EXPECT_EQ(false, signals.GetShouldStop(signo));
  EXPECT_EQ(true, signals.GetShouldNotify(signo));
  EXPECT_EQ(name, signals.GetSignalAsCString(signo));
}

TEST(UnixSignalsTest, GetAsCString) {
  TestSignals signals;

  ASSERT_EQ(nullptr, signals.GetSignalAsCString(100));
  std::string name = signals.GetSignalAsCString(16);
  ASSERT_EQ("SIG16", name);
}

TEST(UnixSignalsTest, GetAsString) {
  TestSignals signals;

  ASSERT_EQ("", signals.GetSignalDescription(100, std::nullopt));
  ASSERT_EQ("SIG16", signals.GetSignalDescription(16, std::nullopt));
  ASSERT_EQ("", signals.GetSignalDescription(100, 100));
  ASSERT_EQ("SIG16", signals.GetSignalDescription(16, 100));
  ASSERT_EQ("SIG16: a specific type of SIG16",
            signals.GetSignalDescription(16, 1));

  // Unknown code, won't use the address.
  ASSERT_EQ("SIG16", signals.GetSignalDescription(16, 100, 0xCAFEF00D));
  // Known code, that shouldn't print fault address.
  ASSERT_EQ("SIG16: a specific type of SIG16",
            signals.GetSignalDescription(16, 1, 0xCAFEF00D));
  // Known code that should.
  ASSERT_EQ("SIG16: SIG16 with a fault address (fault address: 0xcafef00d)",
            signals.GetSignalDescription(16, 2, 0xCAFEF00D));
  // No address given just print the code description.
  ASSERT_EQ("SIG16: SIG16 with a fault address",
            signals.GetSignalDescription(16, 2));

  const char *expected = "SIG16: bounds violation";
  // Must pass all needed info to get full output.
  ASSERT_EQ(expected, signals.GetSignalDescription(16, 3));
  ASSERT_EQ(expected, signals.GetSignalDescription(16, 3, 0xcafef00d));
  ASSERT_EQ(expected, signals.GetSignalDescription(16, 3, 0xcafef00d, 0x1234));

  ASSERT_EQ("SIG16: upper bound violation (fault address: 0x5679, lower bound: "
            "0x1234, upper bound: 0x5678)",
            signals.GetSignalDescription(16, 3, 0x5679, 0x1234, 0x5678));
  ASSERT_EQ("SIG16: lower bound violation (fault address: 0x1233, lower bound: "
            "0x1234, upper bound: 0x5678)",
            signals.GetSignalDescription(16, 3, 0x1233, 0x1234, 0x5678));
}

TEST(UnixSignalsTest, VersionChange) {
  TestSignals signals;

  int32_t signo = 8;
  uint64_t ver = signals.GetVersion();
  EXPECT_GT(ver, 0ull);
  EXPECT_EQ(true, signals.GetShouldSuppress(signo));
  EXPECT_EQ(true, signals.GetShouldStop(signo));
  EXPECT_EQ(true, signals.GetShouldNotify(signo));

  EXPECT_EQ(signals.GetVersion(), ver);

  signals.SetShouldSuppress(signo, false);
  EXPECT_LT(ver, signals.GetVersion());
  ver = signals.GetVersion();

  signals.SetShouldStop(signo, true);
  EXPECT_LT(ver, signals.GetVersion());
  ver = signals.GetVersion();

  signals.SetShouldNotify(signo, false);
  EXPECT_LT(ver, signals.GetVersion());
  ver = signals.GetVersion();

  EXPECT_EQ(false, signals.GetShouldSuppress(signo));
  EXPECT_EQ(true, signals.GetShouldStop(signo));
  EXPECT_EQ(false, signals.GetShouldNotify(signo));

  EXPECT_EQ(ver, signals.GetVersion());
}

TEST(UnixSignalsTest, GetFilteredSignals) {
  TestSignals signals;

  auto all_signals =
      signals.GetFilteredSignals(std::nullopt, std::nullopt, std::nullopt);
  std::vector<int32_t> expected = {2, 4, 8, 16};
  EXPECT_EQ_ARRAYS(expected, all_signals);

  auto supressed = signals.GetFilteredSignals(true, std::nullopt, std::nullopt);
  expected = {4, 8, 16};
  EXPECT_EQ_ARRAYS(expected, supressed);

  auto not_supressed =
      signals.GetFilteredSignals(false, std::nullopt, std::nullopt);
  expected = {2};
  EXPECT_EQ_ARRAYS(expected, not_supressed);

  auto stopped = signals.GetFilteredSignals(std::nullopt, true, std::nullopt);
  expected = {2, 8};
  EXPECT_EQ_ARRAYS(expected, stopped);

  auto not_stopped =
      signals.GetFilteredSignals(std::nullopt, false, std::nullopt);
  expected = {4, 16};
  EXPECT_EQ_ARRAYS(expected, not_stopped);

  auto notified = signals.GetFilteredSignals(std::nullopt, std::nullopt, true);
  expected = {2, 4, 8};
  EXPECT_EQ_ARRAYS(expected, notified);

  auto not_notified =
      signals.GetFilteredSignals(std::nullopt, std::nullopt, false);
  expected = {16};
  EXPECT_EQ_ARRAYS(expected, not_notified);

  auto signal4 = signals.GetFilteredSignals(true, false, true);
  expected = {4};
  EXPECT_EQ_ARRAYS(expected, signal4);
}