Skip to content

Commit 52ca037

Browse files
committed
Merge pull request #68670 from al45tair/eng/PR-115774613-5.9
[Linux] Force the inclusion of the backtracing code when static linking.
1 parent cc21d41 commit 52ca037

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
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/SwiftRT-ELF.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@
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+
// Drag in a symbol from the backtracer, to force the static linker to include
23+
// the code.
24+
static const void *__backtraceRef __attribute__((used))
25+
= (const void *)swift::runtime::backtrace::_swift_backtrace_isThunkFunction;
26+
2127
// Create empty sections to ensure that the start/stop symbols are synthesized
2228
// by the linker. Otherwise, we may end up with undefined symbol references as
2329
// 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: 8 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,9 @@ 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) )
560+
config.substitutions.append( ('%swift-plugin-dir', config.swift_plugin_dir) )
561+
558562
config.substitutions.append( ('%llvm_src_root', config.llvm_src_root) )
559563
config.substitutions.append( ('%swift_obj_root', config.swift_obj_root) )
560564
config.substitutions.append( ('%swift_src_root', config.swift_src_root) )
@@ -1954,6 +1958,10 @@ if run_vendor == 'apple':
19541958
config.available_features.add('back_deploy_concurrency')
19551959
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)
19561960

1961+
backtracer_path = make_path(config.swift_libexec_dir, 'swift',
1962+
config.target_sdk_name, 'swift-backtrace')
1963+
config.substitutions.append(('%backtracer', backtracer_path))
1964+
19571965
def os_stdlib_paths():
19581966
if run_vendor == 'apple':
19591967
if run_os == 'maccatalyst':

0 commit comments

Comments
 (0)