Skip to content

Commit 6bc28ff

Browse files
committed
[strip -ST] Disable runtime stack trace dumping on Darwin when asserts are disabled.
This commit disables runtime stack trace dumping via dladdr on Darwin when asserts are disabled. This stack trace dumping was added as a way to improve the ability to debug the compiler for compiler developers. This is all well and good but having such a feature always enabled prevents us from reducing the size of the swift standard library by eliminating the swift nlist. rdar://31372220
1 parent fb0f6fe commit 6bc28ff

16 files changed

+218
-18
lines changed

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ include(AddSwift)
416416
include(SwiftConfigureSDK)
417417
include(SwiftComponents)
418418
include(SwiftList)
419+
include(AddSwiftRuntime)
419420

420421
# Configure swift include, install, build components.
421422
swift_configure_components()
@@ -785,6 +786,17 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
785786
set(CMAKE_OSX_DEPLOYMENT_TARGET "")
786787
endif()
787788

789+
swift_runtime_enable_backtrace_reporting(SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING_default)
790+
set(SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING
791+
${SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING_default}
792+
CACHE BOOL "Enable simple backtrace printing using dladdr")
793+
set(SWIFT_RUNTIME_DLADDR_ALLOWED
794+
${SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING_default}
795+
CACHE BOOL "Is dladdr allowed to be used in the code base. Must be TRUE if SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING is set to TRUE")
796+
if ("${SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING}" AND NOT "${SWIFT_RUNTIME_DLADDR_ALLOWED}")
797+
message(FATAL "Can not enable backtrace reporting without allowing dladdr to be used by the runtime")
798+
endif()
799+
788800
message(STATUS "Building host Swift tools for ${SWIFT_HOST_VARIANT_SDK} ${SWIFT_HOST_VARIANT_ARCH}")
789801
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
790802
message(STATUS " Assertions: ${LLVM_ENABLE_ASSERTIONS}")
@@ -798,6 +810,8 @@ message(STATUS "")
798810

799811
message(STATUS "Building Swift runtime with:")
800812
message(STATUS " Leak Detection Checker Entrypoints: ${SWIFT_RUNTIME_ENABLE_LEAK_CHECKER}")
813+
message(STATUS " Backtraces: ${SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING}")
814+
message(STATUS " DLAddr Allowed: ${SWIFT_RUNTIME_DLADDR_ALLOWED}")
801815
message(STATUS "")
802816

803817
#

cmake/modules/AddSwiftRuntime.cmake

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#===--- AddSwiftRuntime.cmake --------------------------------------------===#
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See https:#swift.org/LICENSE.txt for license information
9+
# See https:#swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
#===----------------------------------------------------------------------===#
12+
13+
include(SwiftUtils)
14+
15+
function(swift_runtime_enable_backtrace_reporting outvar)
16+
precondition(outvar "Must have an outvar set")
17+
18+
# We do not support backtraces on android...
19+
is_sdk_requested(ANDROID swift_build_android)
20+
if(${swift_build_android})
21+
set(${outvar} FALSE PARENT_SCOPE)
22+
return()
23+
endif()
24+
25+
# ... or cygwin
26+
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
27+
set(${outvar} FALSE PARENT_SCOPE)
28+
return()
29+
endif()
30+
31+
# ... or win32.
32+
if (WIN32)
33+
set(${outvar} FALSE PARENT_SCOPE)
34+
return()
35+
endif()
36+
37+
# If we are not Darwin, but are cygwin, win32, or android we *do* support
38+
# runtime backtraces. This is just preserving the current existing
39+
# behavior. Arguably this should be an opt in feature always.
40+
if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL Darwin)
41+
set(${outvar} TRUE PARENT_SCOPE)
42+
return()
43+
endif()
44+
45+
# Otherwise, we have a Darwin build. Enable runtime backtrace reporting only
46+
# when compiler assertions are enabled.
47+
if ("${SWIFT_STDLIB_ASSERTIONS}")
48+
set(${outvar} TRUE PARENT_SCOPE)
49+
else()
50+
set(${outvar} FALSE PARENT_SCOPE)
51+
endif()
52+
endfunction()

stdlib/public/runtime/CMakeLists.txt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ if(SWIFT_RUNTIME_ENABLE_COW_EXISTENTIALS)
2323
"-DSWIFT_RUNTIME_ENABLE_COW_EXISTENTIALS=1")
2424
endif()
2525

26+
if(SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING)
27+
list(APPEND swift_runtime_compile_flags
28+
"-DSWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING=1")
29+
else()
30+
list(APPEND swift_runtime_compile_flags
31+
"-DSWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING=0")
32+
endif()
33+
34+
if(SWIFT_RUNTIME_DLADDR_ALLOWED)
35+
list(APPEND swift_runtime_compile_flags
36+
"-DSWIFT_RUNTIME_DLADDR_ALLOW=1")
37+
else()
38+
list(APPEND swift_runtime_compile_flags
39+
"-DSWIFT_RUNTIME_DLADDR_ALLOW=0")
40+
endif()
41+
2642
set(section_magic_compile_flags ${swift_runtime_compile_flags})
2743

2844
list(APPEND swift_runtime_compile_flags
@@ -93,13 +109,18 @@ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
93109
string(TOLOWER "${sdk}" lowercase_sdk)
94110

95111
# These two libraries are only used with the static swiftcore
96-
add_library(swiftImageInspectionStatic STATIC
97-
ImageInspectionStatic.cpp
98-
StaticBinaryELF.cpp)
112+
add_swift_library(swiftImageInspectionStatic STATIC
113+
ImageInspectionStatic.cpp
114+
StaticBinaryELF.cpp
115+
C_COMPILE_FLAGS ${swift_runtime_library_compile_flags}
116+
LINK_FLAGS ${swift_runtime_linker_flags})
99117
set_target_properties(swiftImageInspectionStatic PROPERTIES
100118
ARCHIVE_OUTPUT_DIRECTORY "${SWIFTSTATICLIB_DIR}/${lowercase_sdk}")
101119

102-
add_library(swiftImageInspectionShared STATIC ImageInspectionELF.cpp)
120+
add_swift_library(swiftImageInspectionShared STATIC
121+
ImageInspectionELF.cpp
122+
C_COMPILE_FLAGS ${swift_runtime_library_compile_flags}
123+
LINK_FLAGS ${swift_runtime_linker_flags})
103124
set_target_properties(swiftImageInspectionShared PROPERTIES
104125
ARCHIVE_OUTPUT_DIRECTORY "${SWIFTSTATICLIB_DIR}/${lowercase_sdk}")
105126

stdlib/public/runtime/Errors.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(_WIN32)
18-
# define SWIFT_SUPPORTS_BACKTRACE_REPORTING 0
19-
#else
20-
# define SWIFT_SUPPORTS_BACKTRACE_REPORTING 1
21-
#endif
22-
2317
#include <stdio.h>
2418
#include <stdlib.h>
2519
#include <string.h>
@@ -40,7 +34,11 @@
4034
#include <cxxabi.h>
4135
#endif
4236

43-
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
37+
#ifndef SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING
38+
#error "SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING must be defined"
39+
#endif
40+
41+
#if SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING
4442
// execinfo.h is not available on Android. Checks in this file ensure that
4543
// fatalError behaves as expected, but without stack traces.
4644
#include <execinfo.h>
@@ -58,7 +56,7 @@ enum: uint32_t {
5856

5957
using namespace swift;
6058

61-
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
59+
#if SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING
6260

6361
static bool getSymbolNameAddr(llvm::StringRef libraryName, SymbolInfo syminfo,
6462
std::string &symbolName, uintptr_t &addrOut) {
@@ -207,7 +205,7 @@ reportNow(uint32_t flags, const char *message)
207205
#ifdef __APPLE__
208206
asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "%s", message);
209207
#endif
210-
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
208+
#if SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING
211209
if (flags & FatalErrorFlags::ReportBacktrace) {
212210
fputs("Current stack trace:\n", stderr);
213211
constexpr unsigned maxSupportedStackDepth = 128;

stdlib/public/runtime/ImageInspectionELF.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#include <link.h>
2828
#include <string.h>
2929

30+
#ifndef SWIFT_RUNTIME_DLADDR_ALLOW
31+
#error "SWIFT_RUNTIME_DLADDR_ALLOW must be defined!"
32+
#endif
33+
3034
using namespace swift;
3135

3236
/// The symbol name in the image that identifies the beginning of the
@@ -158,6 +162,7 @@ void swift_addNewDSOImage(const void *addr) {
158162
}
159163

160164
int swift::lookupSymbol(const void *address, SymbolInfo *info) {
165+
#if SWIFT_RUNTIME_DLADDR_ALLOW
161166
Dl_info dlinfo;
162167
if (dladdr(address, &dlinfo) == 0) {
163168
return 0;
@@ -168,6 +173,9 @@ int swift::lookupSymbol(const void *address, SymbolInfo *info) {
168173
info->symbolName = dlinfo.dli_sname;
169174
info->symbolAddress = dlinfo.dli_saddr;
170175
return 1;
176+
#else
177+
return 0;
178+
#endif
171179
}
172180

173181
#endif // defined(__ELF__) || defined(__ANDROID__)

stdlib/public/runtime/ImageInspectionMachO.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#include <assert.h>
2727
#include <dlfcn.h>
2828

29+
#ifndef SWIFT_RUNTIME_DLADDR_ALLOW
30+
#error "SWIFT_RUNTIME_DLADDR_ALLOW must be defined"
31+
#endif
32+
2933
using namespace swift;
3034

3135
namespace {
@@ -74,6 +78,7 @@ void swift::initializeTypeMetadataRecordLookup() {
7478
}
7579

7680
int swift::lookupSymbol(const void *address, SymbolInfo *info) {
81+
#if SWIFT_RUNTIME_DLADDR_ALLOW
7782
Dl_info dlinfo;
7883
if (dladdr(address, &dlinfo) == 0) {
7984
return 0;
@@ -84,6 +89,9 @@ int swift::lookupSymbol(const void *address, SymbolInfo *info) {
8489
info->symbolName = dlinfo.dli_sname;
8590
info->symbolAddress = dlinfo.dli_saddr;
8691
return 1;
92+
#else
93+
return 0;
94+
#endif
8795
}
8896

8997
#endif // defined(__APPLE__) && defined(__MACH__)

stdlib/public/runtime/ImageInspectionWin32.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#include <dlfcn.h>
3434
#endif
3535

36+
#ifndef SWIFT_RUNTIME_DLADDR_ALLOW
37+
#error "SWIFT_RUNTIME_DLADDR_ALLOW must be defined!"
38+
#endif
39+
3640
using namespace swift;
3741

3842
/// PE section name for the section that contains protocol conformance records.
@@ -219,7 +223,7 @@ void swift::initializeTypeMetadataRecordLookup() {
219223

220224

221225
int swift::lookupSymbol(const void *address, SymbolInfo *info) {
222-
#if defined(__CYGWIN__)
226+
#if defined(__CYGWIN__) || SWIFT_RUNTIME_DLADDR_ALLOW
223227
Dl_info dlinfo;
224228
if (dladdr(address, &dlinfo) == 0) {
225229
return 0;

test/Runtime/backtrace.swift renamed to test/Runtime/crash_with_backtrace.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
// UNSUPPORTED: OS=ios
99
// UNSUPPORTED: OS=tvos
1010

11+
// REQUIRES: runtime-dladdr-backtraces
1112
// REQUIRES: executable_test
1213

1314
// Backtraces are not emitted when optimizations are enabled. This test can not
1415
// run when optimizations are enabled.
1516
// REQUIRES: swift_test_mode_optimize_none
1617

18+
// This file just causes a crash in the runtime to check whether or not a stack
19+
// trace is produced from the runtime.
20+
1721
func main() {
1822
let x = UnsafePointer<Int>(bitPattern: 0)!
1923
print("\(x.pointee)")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir %t
3+
// RUN: %target-build-swift %s -o %t/out
4+
// RUN: not --crash %t/out 2>&1 | %FileCheck %s
5+
6+
// UNSUPPORTED: OS=watchos
7+
// UNSUPPORTED: OS=ios
8+
// UNSUPPORTED: OS=tvos
9+
// UNSUPPORTED: runtime-dladdr-backtraces
10+
11+
// This file just causes a crash in the runtime to check whether or not a stack
12+
// trace is produced from the runtime.
13+
14+
// CHECK-NOT: Current stack trace:
15+
16+
import Swift
17+
18+
func foo() -> Int {
19+
return UnsafePointer<Int>(bitPattern: 0)!.pointee
20+
}
21+
22+
foo()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir %t
3+
// RUN: %target-build-swift -O %s -o %t/out
4+
// RUN: not --crash %t/out 2>&1 | %FileCheck %s
5+
6+
// UNSUPPORTED: OS=watchos
7+
// UNSUPPORTED: OS=ios
8+
// UNSUPPORTED: OS=tvos
9+
10+
// This file just causes a crash in the runtime to check whether or not a stack
11+
// trace is produced from the runtime.
12+
//
13+
// It checks that no matter what when we compile with optimization, we do not
14+
// emit backtraces.
15+
16+
// CHECK-NOT: Current stack trace:
17+
18+
import Swift
19+
20+
func foo() -> Int {
21+
return UnsafePointer<Int>(bitPattern: 0)!.pointee
22+
}
23+
24+
foo()

test/Runtime/linux-fatal-backtrace.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// REQUIRES: executable_test
66
// REQUIRES: OS=linux-gnu
77
// REQUIRES: lldb
8+
// REQUIRES: runtime-dladdr-backtraces
89

910
// Backtraces are not emitted when optimizations are enabled. This test can not
1011
// run when optimizations are enabled.

test/lit.site.cfg.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ if "@CMAKE_GENERATOR@" == "Xcode":
8383

8484
config.available_features.add("CMAKE_GENERATOR=@CMAKE_GENERATOR@")
8585

86+
if "@SWIFT_RUNTIME_ENABLE_BACKTRACE_REPORTING@" == "TRUE":
87+
config.available_features.add('runtime-dladdr-backtraces')
88+
if "@SWIFT_RUNTIME_DLADDR_ALLOWED@" == "TRUE":
89+
config.available_features.add('runtime-dladdr')
90+
8691
if "@SWIFT_ENABLE_SOURCEKIT_TESTS@" == "TRUE":
8792
config.available_features.add('sourcekit')
8893

unittests/runtime/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
22
("${SWIFT_HOST_VARIANT_ARCH}" STREQUAL "${SWIFT_PRIMARY_VARIANT_ARCH}"))
33

4+
set(swift_runtime_test_extra_libraries)
45
if(SWIFT_BUILD_STATIC_STDLIB AND "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "LINUX")
5-
set(swift_runtime_test_extra_sources
6-
"${CMAKE_CURRENT_SOURCE_DIR}/../../stdlib/public/runtime/ImageInspectionELF.cpp")
6+
list(APPEND swift_runtime_test_extra_libraries swiftImageInspectionShared)
77
endif()
88

99
add_subdirectory(LongTests)
@@ -43,13 +43,13 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
4343
# from the swiftCore dylib, so we need to link to both the runtime archive
4444
# and the stdlib.
4545
$<TARGET_OBJECTS:swiftRuntime${SWIFT_PRIMARY_VARIANT_SUFFIX}>
46-
${swift_runtime_test_extra_sources}
4746
)
4847

4948
# FIXME: cross-compile for all variants.
5049
target_link_libraries(SwiftRuntimeTests
5150
swiftCore${SWIFT_PRIMARY_VARIANT_SUFFIX}
5251
${PLATFORM_TARGET_LINK_LIBRARIES}
52+
${swift_runtime_test_extra_libraries}
5353
)
5454
endif()
5555

unittests/runtime/LongTests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
3636
# from the swiftCore dylib, so we need to link to both the runtime archive
3737
# and the stdlib.
3838
$<TARGET_OBJECTS:swiftRuntime${SWIFT_PRIMARY_VARIANT_SUFFIX}>
39-
${swift_runtime_test_extra_sources}
4039
)
4140

4241
# FIXME: cross-compile for all variants.
4342
target_link_libraries(SwiftRuntimeLongTests
4443
swiftCore${SWIFT_PRIMARY_VARIANT_SUFFIX}
4544
${PLATFORM_TARGET_LINK_LIBRARIES}
45+
${swift_runtime_test_extra_libraries}
4646
)
4747
endif()
4848

0 commit comments

Comments
 (0)