Skip to content

Commit 0398ad1

Browse files
authored
Merge pull request #68904 from al45tair/eng/PR-116336687
[Linux] Cherry pick backtracing changes from the 5.9 branch.
2 parents cc21d41 + 9dbdd5e commit 0398ad1

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
540540
get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
541541
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
542542
set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
543+
set(swiftrt "${host_lib_dir}/${SWIFT_HOST_VARIANT_ARCH}/swiftrt.o")
543544
544545
target_link_libraries(${target} PRIVATE ${swiftrt})
545546
target_link_libraries(${target} PRIVATE "swiftCore")

stdlib/public/runtime/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ set(LLVM_OPTIONAL_SOURCES
9999
${swift_runtime_leaks_sources}
100100
${swift_runtime_backtracing_sources})
101101

102+
set(swift_enable_backtracing)
102103
if(SWIFT_ENABLE_BACKTRACING)
103104
list(APPEND swift_runtime_sources ${swift_runtime_backtracing_sources})
105+
set(swift_enable_backtracing -DSWIFT_ENABLE_BACKTRACING)
104106
endif()
105107

106108
set(swift_runtime_library_compile_flags ${swift_runtime_compile_flags})
@@ -159,7 +161,9 @@ endforeach()
159161
add_swift_target_library(swiftImageRegistrationObjectELF
160162
OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
161163
SwiftRT-ELF.cpp
162-
C_COMPILE_FLAGS ${SWIFT_RUNTIME_CORE_CXX_FLAGS}
164+
C_COMPILE_FLAGS
165+
${SWIFT_RUNTIME_CORE_CXX_FLAGS}
166+
${swift_enable_backtracing}
163167
C_COMPILE_FLAGS_LINUX -fno-lto
164168
LINK_FLAGS ${SWIFT_RUNTIME_CORE_LINK_FLAGS}
165169
TARGET_SDKS ${ELFISH_SDKS}
@@ -170,7 +174,9 @@ add_swift_target_library(swiftImageRegistrationObjectELF
170174
add_swift_target_library(swiftImageRegistrationObjectCOFF
171175
OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
172176
SwiftRT-COFF.cpp
173-
C_COMPILE_FLAGS ${SWIFT_RUNTIME_CORE_CXX_FLAGS}
177+
C_COMPILE_FLAGS
178+
${SWIFT_RUNTIME_CORE_CXX_FLAGS}
179+
${swift_enable_backtracing}
174180
LINK_FLAGS ${SWIFT_RUNTIME_CORE_LINK_FLAGS}
175181
TARGET_SDKS ${COFF_SDKS}
176182
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}

stdlib/public/runtime/SwiftRT-ELF.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@
1212

1313
#include "ImageInspectionCommon.h"
1414
#include "swift/shims/MetadataSections.h"
15+
#include "swift/Runtime/Backtrace.h"
1516

1617
#include <cstddef>
1718
#include <new>
1819

1920
extern "C" const char __dso_handle[];
2021

22+
#if SWIFT_ENABLE_BACKTRACING
23+
// Drag in a symbol from the backtracer, to force the static linker to include
24+
// the code.
25+
static const void *__backtraceRef __attribute__((used))
26+
= (const void *)swift::runtime::backtrace::_swift_backtrace_isThunkFunction;
27+
#endif
28+
2129
// Create empty sections to ensure that the start/stop symbols are synthesized
2230
// by the linker. Otherwise, we may end up with undefined symbol references as
2331
// the linker table section was never constructed.

test/Backtracing/CrashStatic.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -parse-as-library %import-static-libdispatch -Onone -static-stdlib -g -o %t/CrashStatic
3+
// RUN: %target-codesign %t/CrashStatic
4+
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no,swift-backtrace=%backtracer %target-run %t/CrashStatic 2>&1 || true) | %FileCheck %s
5+
6+
// UNSUPPORTED: use_os_stdlib
7+
// UNSUPPORTED: back_deployment_runtime
8+
// UNSUPPORTED: asan
9+
// REQUIRES: executable_test
10+
// REQUIRES: backtracing
11+
// REQUIRES: static_stdlib
12+
// REQUIRES: OS=linux-gnu
13+
14+
func level1() {
15+
level2()
16+
}
17+
18+
func level2() {
19+
level3()
20+
}
21+
22+
func level3() {
23+
level4()
24+
}
25+
26+
func level4() {
27+
level5()
28+
}
29+
30+
func level5() {
31+
print("About to crash")
32+
let ptr = UnsafeMutablePointer<Int>(bitPattern: 4)!
33+
ptr.pointee = 42
34+
}
35+
36+
@main
37+
struct CrashStatic {
38+
static func main() {
39+
level1()
40+
}
41+
}
42+
43+
// CHECK: *** Program crashed: Bad pointer dereference at 0x{{0+}}4 ***
44+
45+
// CHECK: Thread 0 {{(".*" )?}}crashed:
46+
47+
// CHECK: 0 0x{{[0-9a-f]+}} level5() + {{[0-9]+}} in CrashStatic at {{.*}}/CrashStatic.swift:33:15
48+
// CHECK-NEXT: 1 [ra] 0x{{[0-9a-f]+}} level4() + {{[0-9]+}} in CrashStatic at {{.*}}/CrashStatic.swift:27:3
49+
// CHECK-NEXT: 2 [ra] 0x{{[0-9a-f]+}} level3() + {{[0-9]+}} in CrashStatic at {{.*}}/CrashStatic.swift:23:3
50+
// CHECK-NEXT: 3 [ra] 0x{{[0-9a-f]+}} level2() + {{[0-9]+}} in CrashStatic at {{.*}}/CrashStatic.swift:19:3
51+
// CHECK-NEXT: 4 [ra] 0x{{[0-9a-f]+}} level1() + {{[0-9]+}} in CrashStatic at {{.*}}/CrashStatic.swift:15:3
52+
// CHECK-NEXT: 5 [ra] 0x{{[0-9a-f]+}} static CrashStatic.main() + {{[0-9]+}} in CrashStatic at {{.*}}/CrashStatic.swift:39:5
53+
// CHECK-NEXT: 6 [ra] [system] 0x{{[0-9a-f]+}} static CrashStatic.$main() + {{[0-9]+}} in CrashStatic at {{.*}}/<compiler-generated>
54+
// CHECK-NEXT: 7 [ra] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in CrashStatic at {{.*}}/CrashStatic.swift
55+
56+
// CHECK: Registers:
57+
58+
// CHECK: Images ({{[0-9]+}} omitted):
59+
60+
// CHECK: {{0x[0-9a-f]+}}–{{0x[0-9a-f]+}}{{ +}}{{([0-9a-f]+|<no build ID>)}}{{ +}}CrashStatic{{ +}}{{.*}}/CrashStatic

test/lit.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ config.rth = make_path(config.swift_utils, 'rth') # Resilience test helper
352352
config.scale_test = make_path(config.swift_utils, 'scale-test')
353353
config.PathSanitizingFileCheck = make_path(config.swift_utils, 'PathSanitizingFileCheck')
354354
config.swift_lib_dir = make_path(config.swift, '..', '..', 'lib')
355+
config.swift_libexec_dir = make_path(config.swift, '..', '..', 'libexec')
355356
config.round_trip_syntax_test = make_path(config.swift_utils, 'round-trip-syntax-test')
356357
config.refactor_check_compiles = make_path(config.swift_utils, 'refactor-check-compiles.py')
357358

@@ -555,6 +556,7 @@ else:
555556
config.substitutions.append( ('%llvm_obj_root', config.llvm_obj_root) )
556557
config.substitutions.append( ('%swift-lib-dir', config.swift_lib_dir) )
557558
config.substitutions.append( ('%swift-host-lib-dir', config.swift_host_lib_dir))
559+
config.substitutions.append( ('%swift-libexec-dir', config.swift_lib_dir) )
558560
config.substitutions.append( ('%llvm_src_root', config.llvm_src_root) )
559561
config.substitutions.append( ('%swift_obj_root', config.swift_obj_root) )
560562
config.substitutions.append( ('%swift_src_root', config.swift_src_root) )
@@ -1954,6 +1956,10 @@ if run_vendor == 'apple':
19541956
config.available_features.add('back_deploy_concurrency')
19551957
concurrency_back_deploy_path = os.path.join(os.path.dirname(swift_obj_root), os.path.basename(swift_obj_root).replace("swift-", "backdeployconcurrency-"), 'lib', 'swift-5.5', xcrun_sdk_name)
19561958

1959+
backtracer_path = make_path(config.swift_libexec_dir, 'swift',
1960+
config.target_sdk_name, 'swift-backtrace')
1961+
config.substitutions.append(('%backtracer', backtracer_path))
1962+
19571963
def os_stdlib_paths():
19581964
if run_vendor == 'apple':
19591965
if run_os == 'maccatalyst':

0 commit comments

Comments
 (0)