| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
| [email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| danakj | e75c6c81 | 2024-07-26 20:37:47 | [diff] [blame] | 5 | #include "base/debug/stack_trace.h" |
| danakj | 51d26a40 | 2024-04-25 14:23:56 | [diff] [blame] | 6 | |
| avi | ebe805c | 2015-12-24 08:20:28 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 9 | #include <limits> |
| [email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 10 | #include <sstream> |
| 11 | #include <string> |
| 12 | |
| danakj | e75c6c81 | 2024-07-26 20:37:47 | [diff] [blame] | 13 | #include "base/allocator/buildflags.h" |
| Alex Gough | 0bbe87d | 2025-09-12 21:12:11 | [diff] [blame] | 14 | #include "base/containers/contains.h" |
| Scott Violet | 4416579 | 2018-02-22 02:08:08 | [diff] [blame] | 15 | #include "base/debug/debugging_buildflags.h" |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 16 | #include "base/immediate_crash.h" |
| [email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 17 | #include "base/logging.h" |
| [email protected] | dd4b5126 | 2013-07-25 21:38:23 | [diff] [blame] | 18 | #include "base/process/kill.h" |
| 19 | #include "base/process/process_handle.h" |
| ssid | c1d5a3d | 2020-09-11 05:20:30 | [diff] [blame] | 20 | #include "base/profiler/stack_buffer.h" |
| 21 | #include "base/profiler/stack_copier.h" |
| Greg Thompson | 8b1b295b | 2024-04-10 07:44:14 | [diff] [blame] | 22 | #include "base/strings/cstring_view.h" |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 23 | #include "base/test/test_timeouts.h" |
| avi | ebe805c | 2015-12-24 08:20:28 | [diff] [blame] | 24 | #include "build/build_config.h" |
| danakj | e75c6c81 | 2024-07-26 20:37:47 | [diff] [blame] | 25 | #include "partition_alloc/partition_alloc.h" |
| Alex Gough | 0bbe87d | 2025-09-12 21:12:11 | [diff] [blame] | 26 | #include "testing/gmock/include/gmock/gmock.h" |
| [email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 27 | #include "testing/gtest/include/gtest/gtest.h" |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 28 | #include "testing/multiprocess_func_list.h" |
| Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 29 | #if PA_BUILDFLAG(USE_ALLOCATOR_SHIM) |
| Yuki Shiino | 985ab91e | 2024-03-14 07:20:46 | [diff] [blame] | 30 | #include "partition_alloc/shim/allocator_shim.h" |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 31 | #endif |
| 32 | |
| Xiaohan Wang | 131aa4d | 2022-01-15 19:39:41 | [diff] [blame] | 33 | #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS) |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 34 | #include "base/test/multiprocess_test.h" |
| 35 | #endif |
| [email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 36 | |
| Peter Kasting | 811504a7 | 2025-01-09 03:18:50 | [diff] [blame] | 37 | namespace base::debug { |
| [email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 38 | |
| Xiaohan Wang | 131aa4d | 2022-01-15 19:39:41 | [diff] [blame] | 39 | #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS) |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 40 | typedef MultiProcessTest StackTraceTest; |
| 41 | #else |
| 42 | typedef testing::Test StackTraceTest; |
| 43 | #endif |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 44 | typedef testing::Test StackTraceDeathTest; |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 45 | |
| Mike West | 040aca7 | 2019-11-13 08:10:21 | [diff] [blame] | 46 | #if !defined(__UCLIBC__) && !defined(_AIX) |
| Wez | 7c0794d | 2019-11-14 18:30:41 | [diff] [blame] | 47 | // StackTrace::OutputToStream() is not implemented under uclibc, nor AIX. |
| 48 | // See https://crbug.com/706728 |
| 49 | |
| 50 | TEST_F(StackTraceTest, OutputToStream) { |
| [email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 51 | StackTrace trace; |
| 52 | |
| 53 | // Dump the trace into a string. |
| 54 | std::ostringstream os; |
| 55 | trace.OutputToStream(&os); |
| 56 | std::string backtrace_message = os.str(); |
| 57 | |
| [email protected] | d4114ba | 2011-10-12 16:13:40 | [diff] [blame] | 58 | // ToString() should produce the same output. |
| 59 | EXPECT_EQ(backtrace_message, trace.ToString()); |
| 60 | |
| Daniel Cheng | a0e290d | 2023-10-16 18:47:24 | [diff] [blame] | 61 | span<const void* const> addresses = trace.addresses(); |
| Wez | 7c0794d | 2019-11-14 18:30:41 | [diff] [blame] | 62 | |
| Fabrice de Gans-Riberi | 4c5ebe4 | 2019-11-20 04:45:58 | [diff] [blame] | 63 | #if defined(OFFICIAL_BUILD) && \ |
| Xiaohan Wang | 131aa4d | 2022-01-15 19:39:41 | [diff] [blame] | 64 | ((BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE)) || BUILDFLAG(IS_FUCHSIA)) |
| Wez | 7c0794d | 2019-11-14 18:30:41 | [diff] [blame] | 65 | // Stack traces require an extra data table that bloats our binaries, |
| 66 | // so they're turned off for official builds. Stop the test here, so |
| 67 | // it at least verifies that StackTrace calls don't crash. |
| 68 | return; |
| Fabrice de Gans-Riberi | 4c5ebe4 | 2019-11-20 04:45:58 | [diff] [blame] | 69 | #endif // defined(OFFICIAL_BUILD) && |
| Xiaohan Wang | 131aa4d | 2022-01-15 19:39:41 | [diff] [blame] | 70 | // ((BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE)) || |
| 71 | // BUILDFLAG(IS_FUCHSIA)) |
| Wez | eaf1553 | 2019-11-15 22:00:31 | [diff] [blame] | 72 | |
| Daniel Cheng | a0e290d | 2023-10-16 18:47:24 | [diff] [blame] | 73 | ASSERT_GT(addresses.size(), 5u) << "Too few frames found."; |
| Peter Kasting | 654bb625 | 2024-11-16 02:29:08 | [diff] [blame] | 74 | ASSERT_NE(nullptr, addresses[0]); |
| Wez | 7c0794d | 2019-11-14 18:30:41 | [diff] [blame] | 75 | |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 76 | if (!StackTrace::WillSymbolizeToStreamForTesting()) { |
| Gabriel Charette | ae1bb01 | 2021-05-06 19:31:21 | [diff] [blame] | 77 | return; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 78 | } |
| [email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 79 | |
| 80 | // Check if the output has symbol initialization warning. If it does, fail. |
| [email protected] | de1b764f | 2009-08-24 15:36:41 | [diff] [blame] | 81 | ASSERT_EQ(backtrace_message.find("Dumping unresolved backtrace"), |
| Wez | 7c0794d | 2019-11-14 18:30:41 | [diff] [blame] | 82 | std::string::npos) |
| 83 | << "Unable to resolve symbols."; |
| [email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 84 | |
| [email protected] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 85 | // Expect a demangled symbol. |
| Wez | 7c0794d | 2019-11-14 18:30:41 | [diff] [blame] | 86 | // Note that Windows Release builds omit the function parameters from the |
| Zequan Wu | 76eee5e9 | 2020-07-06 17:38:40 | [diff] [blame] | 87 | // demangled stack output, otherwise this could be "testing::UnitTest::Run()". |
| 88 | EXPECT_TRUE(backtrace_message.find("testing::UnitTest::Run") != |
| 89 | std::string::npos) |
| [email protected] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 90 | << "Expected a demangled symbol in backtrace:\n" |
| 91 | << backtrace_message; |
| [email protected] | 889da7e4 | 2009-12-31 02:28:09 | [diff] [blame] | 92 | |
| [email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 93 | // Expect to at least find main. |
| 94 | EXPECT_TRUE(backtrace_message.find("main") != std::string::npos) |
| 95 | << "Expected to find main in backtrace:\n" |
| 96 | << backtrace_message; |
| 97 | |
| [email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 98 | // Expect to find this function as well. |
| 99 | // Note: This will fail if not linked with -rdynamic (aka -export_dynamic) |
| 100 | EXPECT_TRUE(backtrace_message.find(__func__) != std::string::npos) |
| 101 | << "Expected to find " << __func__ << " in backtrace:\n" |
| 102 | << backtrace_message; |
| [email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 103 | } |
| [email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 104 | |
| halliwell | 5e1e4fa | 2017-03-28 03:21:08 | [diff] [blame] | 105 | #if !defined(OFFICIAL_BUILD) && !defined(NO_UNWIND_TABLES) |
| wez | ec97f91 | 2017-02-05 01:29:25 | [diff] [blame] | 106 | // Disabled in Official builds, where Link-Time Optimization can result in two |
| 107 | // or fewer stack frames being available, causing the test to fail. |
| wez | 73f8d7e | 2017-01-29 06:18:13 | [diff] [blame] | 108 | TEST_F(StackTraceTest, TruncatedTrace) { |
| 109 | StackTrace trace; |
| 110 | |
| Daniel Cheng | a0e290d | 2023-10-16 18:47:24 | [diff] [blame] | 111 | ASSERT_LT(2u, trace.addresses().size()); |
| wez | 73f8d7e | 2017-01-29 06:18:13 | [diff] [blame] | 112 | |
| 113 | StackTrace truncated(2); |
| Daniel Cheng | a0e290d | 2023-10-16 18:47:24 | [diff] [blame] | 114 | EXPECT_EQ(2u, truncated.addresses().size()); |
| wez | 73f8d7e | 2017-01-29 06:18:13 | [diff] [blame] | 115 | } |
| Wez | 7c0794d | 2019-11-14 18:30:41 | [diff] [blame] | 116 | #endif // !defined(OFFICIAL_BUILD) && !defined(NO_UNWIND_TABLES) |
| wez | 73f8d7e | 2017-01-29 06:18:13 | [diff] [blame] | 117 | |
| [email protected] | a15115f0 | 2010-05-25 20:07:12 | [diff] [blame] | 118 | // The test is used for manual testing, e.g., to see the raw output. |
| scottmg | f063a7e | 2015-02-25 00:17:30 | [diff] [blame] | 119 | TEST_F(StackTraceTest, DebugOutputToStream) { |
| [email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 120 | StackTrace trace; |
| 121 | std::ostringstream os; |
| 122 | trace.OutputToStream(&os); |
| [email protected] | b026e35d | 2010-10-19 02:31:03 | [diff] [blame] | 123 | VLOG(1) << os.str(); |
| [email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 124 | } |
| 125 | |
| [email protected] | a15115f0 | 2010-05-25 20:07:12 | [diff] [blame] | 126 | // The test is used for manual testing, e.g., to see the raw output. |
| scottmg | f063a7e | 2015-02-25 00:17:30 | [diff] [blame] | 127 | TEST_F(StackTraceTest, DebugPrintBacktrace) { |
| [email protected] | 5ddbf1c | 2013-08-29 01:59:38 | [diff] [blame] | 128 | StackTrace().Print(); |
| [email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 129 | } |
| Mason Freed | b9ef2b6 | 2018-09-10 17:17:27 | [diff] [blame] | 130 | |
| 131 | // The test is used for manual testing, e.g., to see the raw output. |
| 132 | TEST_F(StackTraceTest, DebugPrintWithPrefixBacktrace) { |
| 133 | StackTrace().PrintWithPrefix("[test]"); |
| 134 | } |
| 135 | |
| 136 | // Make sure nullptr prefix doesn't crash. Output not examined, much |
| 137 | // like the DebugPrintBacktrace test above. |
| 138 | TEST_F(StackTraceTest, DebugPrintWithNullPrefixBacktrace) { |
| Greg Thompson | 8b1b295b | 2024-04-10 07:44:14 | [diff] [blame] | 139 | StackTrace().PrintWithPrefix({}); |
| Mason Freed | b9ef2b6 | 2018-09-10 17:17:27 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Test OutputToStreamWithPrefix, mainly to make sure it doesn't |
| 143 | // crash. Any "real" stack trace testing happens above. |
| 144 | TEST_F(StackTraceTest, DebugOutputToStreamWithPrefix) { |
| 145 | StackTrace trace; |
| Greg Thompson | 8b1b295b | 2024-04-10 07:44:14 | [diff] [blame] | 146 | cstring_view prefix_string = "[test]"; |
| Mason Freed | b9ef2b6 | 2018-09-10 17:17:27 | [diff] [blame] | 147 | std::ostringstream os; |
| 148 | trace.OutputToStreamWithPrefix(&os, prefix_string); |
| 149 | std::string backtrace_message = os.str(); |
| 150 | |
| 151 | // ToStringWithPrefix() should produce the same output. |
| 152 | EXPECT_EQ(backtrace_message, trace.ToStringWithPrefix(prefix_string)); |
| 153 | } |
| 154 | |
| 155 | // Make sure nullptr prefix doesn't crash. Output not examined, much |
| 156 | // like the DebugPrintBacktrace test above. |
| 157 | TEST_F(StackTraceTest, DebugOutputToStreamWithNullPrefix) { |
| 158 | StackTrace trace; |
| 159 | std::ostringstream os; |
| Greg Thompson | 8b1b295b | 2024-04-10 07:44:14 | [diff] [blame] | 160 | trace.OutputToStreamWithPrefix(&os, {}); |
| 161 | trace.ToStringWithPrefix({}); |
| Mason Freed | b9ef2b6 | 2018-09-10 17:17:27 | [diff] [blame] | 162 | } |
| 163 | |
| Wez | 7c0794d | 2019-11-14 18:30:41 | [diff] [blame] | 164 | #endif // !defined(__UCLIBC__) && !defined(_AIX) |
| [email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 165 | |
| Xiaohan Wang | 131aa4d | 2022-01-15 19:39:41 | [diff] [blame] | 166 | #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID) |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 167 | // Since Mac's base::debug::StackTrace().Print() is not malloc-free, skip |
| 168 | // StackDumpSignalHandlerIsMallocFree if BUILDFLAG(IS_MAC). |
| Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 169 | #if PA_BUILDFLAG(USE_ALLOCATOR_SHIM) && !BUILDFLAG(IS_MAC) |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 170 | |
| 171 | namespace { |
| 172 | |
| 173 | // ImmediateCrash if a signal handler incorrectly uses malloc(). |
| 174 | // In an actual implementation, this could cause infinite recursion into the |
| 175 | // signal handler or other problems. Because malloc() is not guaranteed to be |
| 176 | // async signal safe. |
| Yuki Shiino | 3b11049 | 2024-08-08 06:11:22 | [diff] [blame] | 177 | void* BadMalloc(size_t, void*) { |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 178 | base::ImmediateCrash(); |
| hans | cd4cce3 | 2015-05-19 17:03:14 | [diff] [blame] | 179 | } |
| 180 | |
| Yuki Shiino | 3b11049 | 2024-08-08 06:11:22 | [diff] [blame] | 181 | void* BadCalloc(size_t, size_t, void* context) { |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 182 | base::ImmediateCrash(); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 183 | } |
| 184 | |
| Yuki Shiino | 3b11049 | 2024-08-08 06:11:22 | [diff] [blame] | 185 | void* BadAlignedAlloc(size_t, size_t, void*) { |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 186 | base::ImmediateCrash(); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 187 | } |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 188 | |
| Yuki Shiino | 3b11049 | 2024-08-08 06:11:22 | [diff] [blame] | 189 | void* BadAlignedRealloc(void*, size_t, size_t, void*) { |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 190 | base::ImmediateCrash(); |
| 191 | } |
| 192 | |
| Yuki Shiino | 3b11049 | 2024-08-08 06:11:22 | [diff] [blame] | 193 | void* BadRealloc(void*, size_t, void*) { |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 194 | base::ImmediateCrash(); |
| 195 | } |
| 196 | |
| Yuki Shiino | 3b11049 | 2024-08-08 06:11:22 | [diff] [blame] | 197 | void BadFree(void*, void*) { |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 198 | base::ImmediateCrash(); |
| 199 | } |
| 200 | |
| 201 | allocator_shim::AllocatorDispatch g_bad_malloc_dispatch = { |
| 202 | &BadMalloc, /* alloc_function */ |
| 203 | &BadMalloc, /* alloc_unchecked_function */ |
| 204 | &BadCalloc, /* alloc_zero_initialized_function */ |
| Aldo Culquicondor | 189a4b5 | 2025-09-12 16:22:03 | [diff] [blame] | 205 | &BadCalloc, /* alloc_zero_initialized_unchecked_function */ |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 206 | &BadAlignedAlloc, /* alloc_aligned_function */ |
| 207 | &BadRealloc, /* realloc_function */ |
| danakj | 19cd9b8 | 2024-07-05 14:03:40 | [diff] [blame] | 208 | &BadRealloc, /* realloc_unchecked_function */ |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 209 | &BadFree, /* free_function */ |
| mikt | 989d9789 | 2025-04-23 12:45:18 | [diff] [blame] | 210 | nullptr, /* free_with_size_function */ |
| 211 | nullptr, /* free_with_alignment_function */ |
| 212 | nullptr, /* free_with_size_and_alignment_function */ |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 213 | nullptr, /* get_size_estimate_function */ |
| Benoit Lize | cd75515a | 2023-11-29 14:47:42 | [diff] [blame] | 214 | nullptr, /* good_size_function */ |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 215 | nullptr, /* claimed_address_function */ |
| 216 | nullptr, /* batch_malloc_function */ |
| 217 | nullptr, /* batch_free_function */ |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 218 | nullptr, /* try_free_default_function */ |
| 219 | &BadAlignedAlloc, /* aligned_malloc_function */ |
| danakj | 19cd9b8 | 2024-07-05 14:03:40 | [diff] [blame] | 220 | &BadAlignedAlloc, /* aligned_malloc_unchecked_function */ |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 221 | &BadAlignedRealloc, /* aligned_realloc_function */ |
| danakj | 19cd9b8 | 2024-07-05 14:03:40 | [diff] [blame] | 222 | &BadAlignedRealloc, /* aligned_realloc_unchecked_function */ |
| Takashi Sakamoto | b998055 | 2023-10-17 20:14:11 | [diff] [blame] | 223 | &BadFree, /* aligned_free_function */ |
| 224 | nullptr, /* next */ |
| 225 | }; |
| 226 | |
| 227 | } // namespace |
| 228 | |
| 229 | // Regression test for StackDumpSignalHandler async-signal unsafety. |
| 230 | // Since malloc() is not guaranteed to be async signal safe, it is not allowed |
| 231 | // to use malloc() inside StackDumpSignalHandler(). |
| 232 | TEST_F(StackTraceDeathTest, StackDumpSignalHandlerIsMallocFree) { |
| 233 | EXPECT_DEATH_IF_SUPPORTED( |
| 234 | [] { |
| 235 | // On Android, base::debug::EnableInProcessStackDumping() does not |
| 236 | // change any actions taken by signals to be StackDumpSignalHandler. So |
| 237 | // the StackDumpSignalHandlerIsMallocFree test doesn't work on Android. |
| 238 | EnableInProcessStackDumping(); |
| 239 | allocator_shim::InsertAllocatorDispatch(&g_bad_malloc_dispatch); |
| 240 | // Raise SIGSEGV to invoke StackDumpSignalHandler(). |
| 241 | kill(getpid(), SIGSEGV); |
| 242 | }(), |
| 243 | "\\[end of stack trace\\]\n"); |
| 244 | } |
| Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 245 | #endif // PA_BUILDFLAG(USE_ALLOCATOR_SHIM) |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 246 | |
| 247 | namespace { |
| 248 | |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 249 | std::string itoa_r_wrapper(intptr_t i, size_t sz, int base, size_t padding) { |
| danakj | d53babfe | 2024-10-09 14:04:16 | [diff] [blame] | 250 | std::array<char, 1024> buffer; |
| 251 | internal::itoa_r(i, base, padding, base::span(buffer).first(sz)); |
| 252 | EXPECT_NE(buffer[0], '\0'); |
| 253 | for (char c : buffer) { |
| 254 | if (c == '\0') { |
| 255 | return std::string(buffer.data()); |
| 256 | } |
| 257 | } |
| 258 | ADD_FAILURE() << "buffer is not NUL terminated"; |
| 259 | return std::string(""); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | } // namespace |
| 263 | |
| 264 | TEST_F(StackTraceTest, itoa_r) { |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 265 | EXPECT_EQ("0", itoa_r_wrapper(0, 128, 10, 0)); |
| 266 | EXPECT_EQ("-1", itoa_r_wrapper(-1, 128, 10, 0)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 267 | |
| 268 | // Test edge cases. |
| 269 | if (sizeof(intptr_t) == 4) { |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 270 | EXPECT_EQ("ffffffff", itoa_r_wrapper(-1, 128, 16, 0)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 271 | EXPECT_EQ("-2147483648", |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 272 | itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 10, 0)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 273 | EXPECT_EQ("2147483647", |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 274 | itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 10, 0)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 275 | |
| 276 | EXPECT_EQ("80000000", |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 277 | itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 16, 0)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 278 | EXPECT_EQ("7fffffff", |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 279 | itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 16, 0)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 280 | } else if (sizeof(intptr_t) == 8) { |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 281 | EXPECT_EQ("ffffffffffffffff", itoa_r_wrapper(-1, 128, 16, 0)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 282 | EXPECT_EQ("-9223372036854775808", |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 283 | itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 10, 0)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 284 | EXPECT_EQ("9223372036854775807", |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 285 | itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 10, 0)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 286 | |
| 287 | EXPECT_EQ("8000000000000000", |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 288 | itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 16, 0)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 289 | EXPECT_EQ("7fffffffffffffff", |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 290 | itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 16, 0)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 291 | } else { |
| 292 | ADD_FAILURE() << "Missing test case for your size of intptr_t (" |
| 293 | << sizeof(intptr_t) << ")"; |
| 294 | } |
| 295 | |
| 296 | // Test hex output. |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 297 | EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 0)); |
| 298 | EXPECT_EQ("deadbeef", itoa_r_wrapper(0xdeadbeef, 128, 16, 0)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 299 | |
| 300 | // Check that itoa_r respects passed buffer size limit. |
| danakj | d53babfe | 2024-10-09 14:04:16 | [diff] [blame] | 301 | std::array<char, 1024> buffer; |
| 302 | internal::itoa_r(0xdeadbeef, 16, 0, base::span(buffer).first(10u)); |
| 303 | EXPECT_NE(buffer[0u], '\0'); |
| 304 | internal::itoa_r(0xdeadbeef, 16, 0, base::span(buffer).first(9u)); |
| 305 | EXPECT_NE(buffer[0u], '\0'); |
| 306 | internal::itoa_r(0xdeadbeef, 16, 0, base::span(buffer).first(8u)); |
| 307 | EXPECT_EQ(buffer[0u], '\0'); |
| 308 | internal::itoa_r(0xdeadbeef, 16, 0, base::span(buffer).first(7u)); |
| 309 | EXPECT_EQ(buffer[0u], '\0'); |
| 310 | internal::itoa_r(0xbeef, 16, 4, base::span(buffer).first(5u)); |
| 311 | EXPECT_NE(buffer[0u], '\0'); |
| 312 | internal::itoa_r(0xbeef, 16, 5, base::span(buffer).first(5u)); |
| 313 | EXPECT_EQ(buffer[0u], '\0'); |
| 314 | internal::itoa_r(0xbeef, 16, 6, base::span(buffer).first(5u)); |
| 315 | EXPECT_EQ(buffer[0u], '\0'); |
| [email protected] | 22d5b982 | 2013-01-10 18:21:52 | [diff] [blame] | 316 | |
| 317 | // Test padding. |
| 318 | EXPECT_EQ("1", itoa_r_wrapper(1, 128, 10, 0)); |
| 319 | EXPECT_EQ("1", itoa_r_wrapper(1, 128, 10, 1)); |
| 320 | EXPECT_EQ("01", itoa_r_wrapper(1, 128, 10, 2)); |
| 321 | EXPECT_EQ("001", itoa_r_wrapper(1, 128, 10, 3)); |
| 322 | EXPECT_EQ("0001", itoa_r_wrapper(1, 128, 10, 4)); |
| 323 | EXPECT_EQ("00001", itoa_r_wrapper(1, 128, 10, 5)); |
| 324 | EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 0)); |
| 325 | EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 1)); |
| 326 | EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 2)); |
| 327 | EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 3)); |
| 328 | EXPECT_EQ("0688", itoa_r_wrapper(0x688, 128, 16, 4)); |
| 329 | EXPECT_EQ("00688", itoa_r_wrapper(0x688, 128, 16, 5)); |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 330 | } |
| Xiaohan Wang | 131aa4d | 2022-01-15 19:39:41 | [diff] [blame] | 331 | #endif // BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID) |
| [email protected] | 1e218b7 | 2012-11-14 19:32:23 | [diff] [blame] | 332 | |
| wez | 460b124 | 2017-04-10 21:55:36 | [diff] [blame] | 333 | #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| erikchen | f7c8a0d | 2017-04-06 21:15:27 | [diff] [blame] | 334 | |
| ssid | c1d5a3d | 2020-09-11 05:20:30 | [diff] [blame] | 335 | class CopyFunction : public StackCopier { |
| 336 | public: |
| 337 | using StackCopier::CopyStackContentsAndRewritePointers; |
| 338 | }; |
| 339 | |
| dskiba | 79080da0 | 2016-04-23 00:10:27 | [diff] [blame] | 340 | template <size_t Depth> |
| danakj | e75c6c81 | 2024-07-26 20:37:47 | [diff] [blame] | 341 | NOINLINE NOOPT void ExpectStackFramePointers(span<const void*> frames) { |
| Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 342 | code_start: |
| dskiba | 79080da0 | 2016-04-23 00:10:27 | [diff] [blame] | 343 | // Calling __builtin_frame_address() forces compiler to emit |
| 344 | // frame pointers, even if they are not enabled. |
| 345 | EXPECT_NE(nullptr, __builtin_frame_address(0)); |
| danakj | e75c6c81 | 2024-07-26 20:37:47 | [diff] [blame] | 346 | ExpectStackFramePointers<Depth - 1>(frames); |
| dskiba | 79080da0 | 2016-04-23 00:10:27 | [diff] [blame] | 347 | |
| 348 | constexpr size_t frame_index = Depth - 1; |
| 349 | const void* frame = frames[frame_index]; |
| 350 | EXPECT_GE(frame, &&code_start) << "For frame at index " << frame_index; |
| 351 | EXPECT_LE(frame, &&code_end) << "For frame at index " << frame_index; |
| Hans Wennborg | 4073b55 | 2023-03-13 17:39:18 | [diff] [blame] | 352 | code_end: |
| 353 | return; |
| 354 | } |
| dskiba | 79080da0 | 2016-04-23 00:10:27 | [diff] [blame] | 355 | |
| Hans Wennborg | 4073b55 | 2023-03-13 17:39:18 | [diff] [blame] | 356 | template <> |
| danakj | e75c6c81 | 2024-07-26 20:37:47 | [diff] [blame] | 357 | NOINLINE NOOPT void ExpectStackFramePointers<1>(span<const void*> frames) { |
| Hans Wennborg | 4073b55 | 2023-03-13 17:39:18 | [diff] [blame] | 358 | code_start: |
| dskiba | 79080da0 | 2016-04-23 00:10:27 | [diff] [blame] | 359 | // Calling __builtin_frame_address() forces compiler to emit |
| 360 | // frame pointers, even if they are not enabled. |
| 361 | EXPECT_NE(nullptr, __builtin_frame_address(0)); |
| danakj | e75c6c81 | 2024-07-26 20:37:47 | [diff] [blame] | 362 | size_t count = TraceStackFramePointers(frames, 0u); |
| 363 | ASSERT_EQ(frames.size(), count); |
| dskiba | 79080da0 | 2016-04-23 00:10:27 | [diff] [blame] | 364 | |
| 365 | const void* frame = frames[0]; |
| 366 | EXPECT_GE(frame, &&code_start) << "For the top frame"; |
| 367 | EXPECT_LE(frame, &&code_end) << "For the top frame"; |
| Hans Wennborg | 4073b55 | 2023-03-13 17:39:18 | [diff] [blame] | 368 | code_end: |
| 369 | return; |
| 370 | } |
| dskiba | 79080da0 | 2016-04-23 00:10:27 | [diff] [blame] | 371 | |
| 372 | #if defined(MEMORY_SANITIZER) |
| 373 | // The test triggers use-of-uninitialized-value errors on MSan bots. |
| 374 | // This is expected because we're walking and reading the stack, and |
| 375 | // sometimes we read fp / pc from the place that previously held |
| 376 | // uninitialized value. |
| 377 | #define MAYBE_TraceStackFramePointers DISABLED_TraceStackFramePointers |
| 378 | #else |
| 379 | #define MAYBE_TraceStackFramePointers TraceStackFramePointers |
| 380 | #endif |
| 381 | TEST_F(StackTraceTest, MAYBE_TraceStackFramePointers) { |
| 382 | constexpr size_t kDepth = 5; |
| 383 | const void* frames[kDepth]; |
| danakj | e75c6c81 | 2024-07-26 20:37:47 | [diff] [blame] | 384 | ExpectStackFramePointers<kDepth>(frames); |
| dskiba | 79080da0 | 2016-04-23 00:10:27 | [diff] [blame] | 385 | } |
| 386 | |
| Xiaohan Wang | 131aa4d | 2022-01-15 19:39:41 | [diff] [blame] | 387 | #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_APPLE) |
| erikchen | d6b2b82 | 2017-02-22 21:10:31 | [diff] [blame] | 388 | #define MAYBE_StackEnd StackEnd |
| 389 | #else |
| 390 | #define MAYBE_StackEnd DISABLED_StackEnd |
| 391 | #endif |
| 392 | |
| 393 | TEST_F(StackTraceTest, MAYBE_StackEnd) { |
| 394 | EXPECT_NE(0u, GetStackEnd()); |
| 395 | } |
| 396 | |
| wez | 460b124 | 2017-04-10 21:55:36 | [diff] [blame] | 397 | #endif // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| dskiba | 79080da0 | 2016-04-23 00:10:27 | [diff] [blame] | 398 | |
| Xiaohan Wang | 131aa4d | 2022-01-15 19:39:41 | [diff] [blame] | 399 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_ANDROID) |
| Andre Kempe | b54981c | 2021-04-13 08:59:53 | [diff] [blame] | 400 | |
| 401 | #if !defined(ADDRESS_SANITIZER) && !defined(UNDEFINED_SANITIZER) |
| 402 | |
| Levi Zim | 14960c8 | 2025-10-09 16:12:39 | [diff] [blame] | 403 | #if defined(ARCH_CPU_X86_FAMILY) |
| 404 | // Division by zero raising SIGFPE is mostly a x86 specific thing. |
| Andre Kempe | b54981c | 2021-04-13 08:59:53 | [diff] [blame] | 405 | // On Arm architecture invalid math operations such as division by zero are not |
| 406 | // trapped and do not trigger a SIGFPE. |
| Levi Zim | 14960c8 | 2025-10-09 16:12:39 | [diff] [blame] | 407 | // On RISC-V architecture, division by zero does not trigger SIGFPE. |
| 408 | // Hence enable the test only for x86 platform |
| Andre Kempe | b54981c | 2021-04-13 08:59:53 | [diff] [blame] | 409 | TEST(CheckExitCodeAfterSignalHandlerDeathTest, CheckSIGFPE) { |
| 410 | // Values are volatile to prevent reordering of instructions, i.e. for |
| 411 | // optimization. Reordering may lead to tests erroneously failing due to |
| 412 | // SIGFPE being raised outside of EXPECT_EXIT. |
| 413 | volatile int const nominator = 23; |
| 414 | volatile int const denominator = 0; |
| Avi Drissman | dea3205 | 2022-01-13 21:31:18 | [diff] [blame] | 415 | [[maybe_unused]] volatile int result; |
| Andre Kempe | b54981c | 2021-04-13 08:59:53 | [diff] [blame] | 416 | |
| 417 | EXPECT_EXIT(result = nominator / denominator, |
| 418 | ::testing::KilledBySignal(SIGFPE), ""); |
| 419 | } |
| Levi Zim | 14960c8 | 2025-10-09 16:12:39 | [diff] [blame] | 420 | #endif // defined(ARCH_CPU_X86_FAMILY) |
| Andre Kempe | b54981c | 2021-04-13 08:59:53 | [diff] [blame] | 421 | |
| 422 | TEST(CheckExitCodeAfterSignalHandlerDeathTest, CheckSIGSEGV) { |
| 423 | // Pointee and pointer are volatile to prevent reordering of instructions, |
| 424 | // i.e. for optimization. Reordering may lead to tests erroneously failing due |
| 425 | // to SIGSEGV being raised outside of EXPECT_EXIT. |
| 426 | volatile int* const volatile p_int = nullptr; |
| 427 | |
| 428 | EXPECT_EXIT(*p_int = 1234, ::testing::KilledBySignal(SIGSEGV), ""); |
| 429 | } |
| 430 | |
| Benoit Lize | bf3f3aea | 2022-11-22 11:11:18 | [diff] [blame] | 431 | #if defined(ARCH_CPU_X86_64) |
| 432 | TEST(CheckExitCodeAfterSignalHandlerDeathTest, |
| 433 | CheckSIGSEGVNonCanonicalAddress) { |
| 434 | // Pointee and pointer are volatile to prevent reordering of instructions, |
| 435 | // i.e. for optimization. Reordering may lead to tests erroneously failing due |
| 436 | // to SIGSEGV being raised outside of EXPECT_EXIT. |
| 437 | // |
| 438 | // On Linux, the upper half of the address space is reserved by the kernel, so |
| 439 | // all upper bits must be 0 for canonical addresses. |
| 440 | volatile int* const volatile p_int = |
| 441 | reinterpret_cast<int*>(0xabcdabcdabcdabcdULL); |
| 442 | |
| 443 | EXPECT_EXIT(*p_int = 1234, ::testing::KilledBySignal(SIGSEGV), "SI_KERNEL"); |
| 444 | } |
| 445 | #endif |
| 446 | |
| Andre Kempe | b54981c | 2021-04-13 08:59:53 | [diff] [blame] | 447 | #endif // #if !defined(ADDRESS_SANITIZER) && !defined(UNDEFINED_SANITIZER) |
| 448 | |
| 449 | TEST(CheckExitCodeAfterSignalHandlerDeathTest, CheckSIGILL) { |
| Sorin Jianu | ad4cc623 | 2024-10-08 19:06:01 | [diff] [blame] | 450 | auto const raise_sigill = [] { |
| Andre Kempe | b54981c | 2021-04-13 08:59:53 | [diff] [blame] | 451 | #if defined(ARCH_CPU_X86_FAMILY) |
| 452 | asm("ud2"); |
| 453 | #elif defined(ARCH_CPU_ARM_FAMILY) |
| 454 | asm("udf 0"); |
| Levi Zim | 0e308347 | 2025-10-09 16:13:43 | [diff] [blame] | 455 | #elif defined(ARCH_CPU_RISCV_FAMILY) |
| 456 | asm("unimp"); |
| Andre Kempe | b54981c | 2021-04-13 08:59:53 | [diff] [blame] | 457 | #else |
| 458 | #error Unsupported platform! |
| 459 | #endif |
| 460 | }; |
| 461 | |
| 462 | EXPECT_EXIT(raise_sigill(), ::testing::KilledBySignal(SIGILL), ""); |
| 463 | } |
| 464 | |
| Xiaohan Wang | 131aa4d | 2022-01-15 19:39:41 | [diff] [blame] | 465 | #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_ANDROID) |
| Andre Kempe | b54981c | 2021-04-13 08:59:53 | [diff] [blame] | 466 | |
| Alex Gough | d007d62 | 2025-09-11 17:55:49 | [diff] [blame] | 467 | #if BUILDFLAG(IS_WIN) |
| 468 | TEST(StackTraceTest, EnabledStackTraces) { |
| 469 | // This is slightly pointless as this is also enabled by the test harness, but |
| 470 | // it ensures we are exercising the InProcessStackDumpingEnabled() path. |
| 471 | EXPECT_TRUE(base::debug::EnableInProcessStackDumping()); |
| 472 | EXPECT_TRUE(base::debug::InProcessStackDumpingEnabled()); |
| 473 | } |
| Alex Gough | 0bbe87d | 2025-09-12 21:12:11 | [diff] [blame] | 474 | |
| 475 | TEST(StackTraceTest, UnsymbolizedStackTraces) { |
| 476 | EXPECT_TRUE(base::debug::DisableInProcessStackDumpingForTesting()); |
| 477 | EXPECT_FALSE(base::debug::InProcessStackDumpingEnabled()); |
| 478 | |
| 479 | StackTrace trace; |
| 480 | auto as_string = trace.ToString(); |
| 481 | EXPECT_THAT(as_string, |
| 482 | ::testing::ContainsRegex("Dumping unresolved backtrace")); |
| 483 | |
| 484 | // Restore global state. |
| 485 | EXPECT_TRUE(base::debug::EnableInProcessStackDumping()); |
| 486 | } |
| Alex Gough | d007d62 | 2025-09-11 17:55:49 | [diff] [blame] | 487 | #endif // BUILDFLAG(IS_WIN) |
| 488 | |
| Peter Kasting | 811504a7 | 2025-01-09 03:18:50 | [diff] [blame] | 489 | } // namespace base::debug |