From d99963e5b485d7a00d169ee0a980f7fa89031bb1 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Tue, 31 Oct 2023 16:47:38 +0000 Subject: [PATCH 1/3] [Linux] Disable fatalError() backtraces when the runtime backtracer is active. There's no need for fatalError() to try to generate its own backtraces when the runtime's backtracer is enabled. Not only is the code it uses more fragile but it also doesn't support async or inline frames and it can't look-up symbols properly either. rdar://117470489 --- include/swift/Runtime/Backtrace.h | 4 ++++ stdlib/public/runtime/Errors.cpp | 11 +++++++++-- test/Backtracing/FatalError.swift | 2 ++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/swift/Runtime/Backtrace.h b/include/swift/Runtime/Backtrace.h index 38981d6694b7e..9400efbb81d8e 100644 --- a/include/swift/Runtime/Backtrace.h +++ b/include/swift/Runtime/Backtrace.h @@ -135,6 +135,10 @@ struct BacktraceSettings { SWIFT_RUNTIME_STDLIB_INTERNAL BacktraceSettings _swift_backtraceSettings; +inline bool _swift_backtrace_isEnabled() { + return _swift_backtraceSettings.enabled == OnOffTty::On; +} + SWIFT_RUNTIME_STDLIB_SPI bool _swift_backtrace_isThunkFunction(const char *mangledName); diff --git a/stdlib/public/runtime/Errors.cpp b/stdlib/public/runtime/Errors.cpp index 8d7ad8d867c9c..b758388c17da8 100644 --- a/stdlib/public/runtime/Errors.cpp +++ b/stdlib/public/runtime/Errors.cpp @@ -34,6 +34,7 @@ #include "ImageInspection.h" #include "swift/Demangling/Demangle.h" +#include "swift/Runtime/Backtrace.h" #include "swift/Runtime/Debug.h" #include "swift/Runtime/Portability.h" #include "swift/Runtime/Win32.h" @@ -353,7 +354,13 @@ void swift::swift_reportError(uint32_t flags, const char *message) { #if defined(__APPLE__) && NDEBUG flags &= ~FatalErrorFlags::ReportBacktrace; +#else + // Disable fatalError backtraces if the backtracer is enabled + if (runtime::backtrace::_swift_backtrace_isEnabled()) { + flags &= ~FatalErrorFlags::ReportBacktrace; + } #endif + reportNow(flags, message); reportOnCrash(flags, message); } @@ -388,9 +395,9 @@ swift::warningv(uint32_t flags, const char *format, va_list args) #pragma GCC diagnostic ignored "-Wuninitialized" swift_vasprintf(&log, format, args); #pragma GCC diagnostic pop - + reportNow(flags, log); - + free(log); } diff --git a/test/Backtracing/FatalError.swift b/test/Backtracing/FatalError.swift index 047c742a0f064..b2c76448991ef 100644 --- a/test/Backtracing/FatalError.swift +++ b/test/Backtracing/FatalError.swift @@ -37,6 +37,8 @@ struct FatalError { } } +// CHECK-NOT: Current stack trace: + // CHECK: *** Program crashed: {{Illegal instruction|System trap}} at 0x{{[0-9a-f]+}} *** // CHECK: Thread 0 {{(".*" )?}}crashed: From 8d409230e27da97c51953eac0c199a1e2aa519ad Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Wed, 1 Nov 2023 13:39:12 +0000 Subject: [PATCH 2/3] [Backtracing] Disable backtracer for Runtime/backtrace test. The Runtime/backtrace test is a test of the fatalError() backtracer, which gets turned off when we have the new backtracer enabled. So, to make this test work, we need to turn off the new backtracer. rdar://117470489 --- test/Runtime/backtrace.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Runtime/backtrace.swift b/test/Runtime/backtrace.swift index e425a0f8c3a1d..19ac684128b7b 100644 --- a/test/Runtime/backtrace.swift +++ b/test/Runtime/backtrace.swift @@ -1,6 +1,6 @@ // RUN: %empty-directory(%t) // RUN: %target-build-swift %s -o %t/a.out -// RUN: %{python} %S/../Inputs/not.py "%target-run %t/a.out" 2>&1 | %{python} %utils/backtrace-check +// RUN: env SWIFT_BACKTRACE=enable=no %{python} %S/../Inputs/not.py "%target-run %t/a.out" 2>&1 | %{python} %utils/backtrace-check // NOTE: not.py is used above instead of "not --crash" because %target-run // doesn't pass through the crash, and `not` may not be available when running From 9672193f28e2d673806bfb11f755ae57cbccb7c7 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Wed, 8 Nov 2023 13:56:04 +0000 Subject: [PATCH 3/3] [Backtracing] Fix a Windows build issue. When backtracing is disabled, don't try to refer to `_swift_backtraceSettings`. rdar://117470489 --- stdlib/public/runtime/CMakeLists.txt | 1 + stdlib/public/runtime/Errors.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/public/runtime/CMakeLists.txt b/stdlib/public/runtime/CMakeLists.txt index 13a0451a41a4b..0785194f34c2a 100644 --- a/stdlib/public/runtime/CMakeLists.txt +++ b/stdlib/public/runtime/CMakeLists.txt @@ -151,6 +151,7 @@ add_swift_target_library(swiftRuntime OBJECT_LIBRARY ${swift_runtime_threading_sources} C_COMPILE_FLAGS ${swift_runtime_library_compile_flags} + ${swift_enable_backtracing} LINK_FLAGS ${swift_runtime_linker_flags} SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} INSTALL_IN_COMPONENT never_install) diff --git a/stdlib/public/runtime/Errors.cpp b/stdlib/public/runtime/Errors.cpp index b758388c17da8..ad29262ce0424 100644 --- a/stdlib/public/runtime/Errors.cpp +++ b/stdlib/public/runtime/Errors.cpp @@ -354,7 +354,7 @@ void swift::swift_reportError(uint32_t flags, const char *message) { #if defined(__APPLE__) && NDEBUG flags &= ~FatalErrorFlags::ReportBacktrace; -#else +#elif SWIFT_ENABLE_BACKTRACING // Disable fatalError backtraces if the backtracer is enabled if (runtime::backtrace::_swift_backtrace_isEnabled()) { flags &= ~FatalErrorFlags::ReportBacktrace;