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