Debian Codegraph @

unittest/gunit/segfault-t.cc

raw symbols SCIP
1 /* Copyright (c) 2011, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 #include <limits.h>
27 #include <thread>
28
29 #include "my_config.h"
30
31 #include "m_string.h"
32 #include "my_inttypes.h"
33 #include "my_stacktrace.h"
34 #include "unittest/gunit/test_utils.h"
35 #include "unittest/gunit/thread_utils.h"
36
37 namespace segfault_unittest {
38
39 using my_testing::Mock_error_handler;
40 using my_testing::Server_initializer;
41
42 size_t get_number_of_occurrences(std::string hay, std::string needle) {
43 size_t pos = hay.find(needle);
44 size_t count = 0;
45 if (pos != std::string::npos) {
46 for (;;) {
47 count++;
48 const auto new_pos = hay.substr(pos + 1).find(needle);
49 if (new_pos == std::string::npos) {
50 break;
51 }
52 pos += 1 + new_pos;
53 }
54 }
55 return count;
56 }
57
58 MATCHER_P3(ContainsRangeOfOccurrences, n, m, str, "") {
59 const auto count = get_number_of_occurrences(arg, str);
60 *result_listener << "where the actual count found is " << count;
61 return n <= static_cast<int>(count) && static_cast<int>(count) <= m;
62 }
63 static bool contains_cached_result;
64 MATCHER_P3(ContainsRangeOfOccurrencesCached, n, m, str, "") {
65 const auto count = get_number_of_occurrences(arg, str);
66 std::cout << "ContainsRangeOfOccurrencesCached(" << n << ", " << m
67 << ") seen " << count << " occurrences and ";
68 if (n <= static_cast<int>(count) && static_cast<int>(count) <= m) {
69 contains_cached_result = true;
70 std::cout << "matched." << std::endl;
71 } else {
72 std::cout << "did not match." << std::endl;
73 }
74 return true;
75 }
76
77 class FatalSignalDeathTest : public ::testing::Test {
78 protected:
79 void SetUp() override {
80 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
81 initializer.SetUp();
82 }
83 void TearDown() override { initializer.TearDown(); }
84
85 Server_initializer initializer;
86
87 const std::string expected_backtrace_string =
88 "Attempting backtrace. You can use the following "
89 "information to find out";
90 };
91
92 TEST_F(FatalSignalDeathTest, Abort) {
93 #if defined(_WIN32)
94 EXPECT_DEATH_IF_SUPPORTED(
95 abort(), ContainsRangeOfOccurrences(1, 1, " UTC - mysqld got exception"));
96 #else
97 EXPECT_DEATH_IF_SUPPORTED(
98 abort(), ContainsRangeOfOccurrences(1, 1, " UTC - mysqld got signal 6"));
99 #endif
100 }
101
102 TEST_F(FatalSignalDeathTest, CrashOnMyAbort) {
103 EXPECT_DEATH_IF_SUPPORTED(
104 my_abort(), ContainsRangeOfOccurrences(1, 1, expected_backtrace_string));
105 }
106 TEST_F(FatalSignalDeathTest, CrashOnTerminate) {
107 EXPECT_DEATH_IF_SUPPORTED(
108 std::terminate(),
109 ContainsRangeOfOccurrences(1, 1, expected_backtrace_string));
110 }
111
112 static void test_parallel_crash() {
113 thread::Notification go;
114 thread::Notification ready[10];
115 auto test = [&ready, &go](int i) {
116 my_thread_init();
117 ready[i].notify();
118 go.wait_for_notification();
119 my_abort();
120 my_thread_end();
121 };
122 std::thread *t[10];
123 for (int i = 0; i < 10; ++i) {
124 t[i] = new std::thread{test, i};
125 }
126 for (int i = 0; i < 10; ++i) {
127 ready[i].wait_for_notification();
128 }
129 go.notify();
130 for (int i = 0; i < 10; ++i) {
131 t[i]->join();
132 delete t[i];
133 }
134 }
135
136 TEST_F(FatalSignalDeathTest, CrashOnParallelAbort) {
137 contains_cached_result = false;
138 for (size_t count = 0; count < 1000 && !contains_cached_result; ++count) {
139 EXPECT_DEATH_IF_SUPPORTED(
140 test_parallel_crash(),
141 ContainsRangeOfOccurrencesCached(2, 10, expected_backtrace_string));
142 }
143 EXPECT_TRUE(contains_cached_result);
144 }
145
146 // Verifies that my_safe_utoa behaves like sprintf(_, "%llu", _)
147 TEST(PrintUtilities, Utoa) {
148 char buff[22];
149 ulonglong intarr[] = {0, 1, 8, 12, 1234, 88888, ULONG_MAX, ULLONG_MAX};
150 char sprintbuff[22];
151 for (size_t ix = 0; ix < array_elements(intarr); ++ix) {
152 char *my_res;
153 sprintf(sprintbuff, "%llu", intarr[ix]);
154 my_res = my_safe_utoa(10, intarr[ix], &buff[sizeof(buff) - 1]);
155 EXPECT_STREQ(sprintbuff, my_res);
156
157 if (intarr[ix] <= ULONG_MAX) {
158 sprintf(sprintbuff, "%lu", (ulong)intarr[ix]);
159 my_res = my_safe_utoa(10, (ulong)intarr[ix], &buff[sizeof(buff) - 1]);
160 EXPECT_STREQ(sprintbuff, my_res);
161 }
162 }
163 }
164
165 // Verifies that my_safe_itoa behaves like sprintf(_, "%lld", _)
166 TEST(PrintUtilities, Itoa) {
167 char buff[22];
168 char sprintbuff[22];
169 longlong intarr[] = {0, 1, 8, 12, 1234, 88888, LONG_MAX, LLONG_MAX};
170
171 for (size_t ix = 0; ix < array_elements(intarr); ++ix) {
172 char *my_res;
173 sprintf(sprintbuff, "%lld", intarr[ix]);
174 my_res = my_safe_itoa(10, intarr[ix], &buff[sizeof(buff) - 1]);
175 EXPECT_STREQ(sprintbuff, my_res);
176
177 ll2str(intarr[ix], buff, 10, false);
178 EXPECT_STREQ(sprintbuff, buff);
179
180 sprintf(sprintbuff, "%lld", -intarr[ix]);
181 my_res = my_safe_itoa(10, -intarr[ix], &buff[sizeof(buff) - 1]);
182 EXPECT_STREQ(sprintbuff, my_res);
183
184 // This one fails ....
185 // ll2str(-intarr[ix], buff, 10, false);
186 // EXPECT_STREQ(sprintbuff, buff)
187 // << "failed for " << -intarr[ix];
188
189 sprintf(sprintbuff, "%llx", intarr[ix]);
190 my_res = my_safe_itoa(16, intarr[ix], &buff[sizeof(buff) - 1]);
191 EXPECT_STREQ(sprintbuff, my_res);
192
193 ll2str(intarr[ix], buff, 16, false);
194 EXPECT_STREQ(sprintbuff, buff);
195
196 sprintf(sprintbuff, "%llx", -intarr[ix]);
197 my_res = my_safe_itoa(16, -intarr[ix], &buff[sizeof(buff) - 1]);
198 EXPECT_STREQ(sprintbuff, my_res) << "failed for " << -intarr[ix];
199
200 ll2str(-intarr[ix], buff, 16, false);
201 EXPECT_STREQ(sprintbuff, buff);
202 }
203 }
204
205 // Various tests for my_safe_snprintf.
206 TEST(PrintUtilities, Printf) {
207 char buff[512];
208 char sprintfbuff[512];
209 const char *null_str = nullptr;
210
211 my_safe_snprintf(buff, sizeof(buff), "hello");
212 EXPECT_STREQ("hello", buff);
213
214 my_safe_snprintf(buff, sizeof(buff), "hello %s hello", "hello");
215 EXPECT_STREQ("hello hello hello", buff);
216 my_safe_snprintf(buff, sizeof(buff), "hello %s hello", null_str);
217 EXPECT_STREQ("hello (null) hello", buff);
218
219 my_safe_snprintf(buff, sizeof(buff), "hello %d hello", 42);
220 EXPECT_STREQ("hello 42 hello", buff);
221 my_safe_snprintf(buff, sizeof(buff), "hello %i hello", 42);
222 EXPECT_STREQ("hello 42 hello", buff);
223 my_safe_snprintf(buff, sizeof(buff), "hello %u hello", (unsigned)42);
224 EXPECT_STREQ("hello 42 hello", buff);
225
226 my_safe_snprintf(buff, sizeof(buff), "hello %llu hello", ULLONG_MAX);
227 sprintf(sprintfbuff, "hello %llu hello", ULLONG_MAX);
228 EXPECT_STREQ(sprintfbuff, buff);
229
230 my_safe_snprintf(buff, sizeof(buff), "hello %x hello", 42);
231 EXPECT_STREQ("hello 2a hello", buff);
232
233 my_safe_snprintf(buff, sizeof(buff), "hello %x hello", -42);
234 sprintf(sprintfbuff, "hello %x hello", -42);
235 EXPECT_STREQ("hello ffffffd6 hello", buff);
236 EXPECT_STREQ(sprintfbuff, buff);
237
238 my_safe_snprintf(buff, sizeof(buff), "hello %llx hello", (longlong)-42);
239 sprintf(sprintfbuff, "hello %llx hello", (longlong)-42);
240 EXPECT_STREQ("hello ffffffffffffffd6 hello", buff);
241 EXPECT_STREQ(sprintfbuff, buff);
242
243 void *p = this;
244 my_safe_snprintf(buff, sizeof(buff), "hello 0x%p hello", p);
245 snprintf(sprintfbuff, sizeof(sprintfbuff), "hello 0x%llx hello",
246 (long long)p);
247 EXPECT_STREQ(sprintfbuff, buff) << "my_safe_snprintf:" << buff;
248 }
249
250 } // namespace segfault_unittest