Skip to content

Commit 8c3cd2f

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 48ea4ac commit 8c3cd2f

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
@@ -92,13 +108,18 @@ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
92108
string(TOLOWER "${sdk}" lowercase_sdk)
93109

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

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

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
@@ -25,6 +25,10 @@
2525
#include <link.h>
2626
#include <string.h>
2727

28+
#ifndef SWIFT_RUNTIME_DLADDR_ALLOW
29+
#error "SWIFT_RUNTIME_DLADDR_ALLOW must be defined!"
30+
#endif
31+
2832
using namespace swift;
2933

3034
/// The symbol name in the image that identifies the beginning of the
@@ -156,6 +160,7 @@ void swift_addNewDSOImage(const void *addr) {
156160
}
157161

158162
int swift::lookupSymbol(const void *address, SymbolInfo *info) {
163+
#if SWIFT_RUNTIME_DLADDR_ALLOW
159164
Dl_info dlinfo;
160165
if (dladdr(address, &dlinfo) == 0) {
161166
return 0;
@@ -166,6 +171,9 @@ int swift::lookupSymbol(const void *address, SymbolInfo *info) {
166171
info->symbolName = dlinfo.dli_sname;
167172
info->symbolAddress = dlinfo.dli_saddr;
168173
return 1;
174+
#else
175+
return 0;
176+
#endif
169177
}
170178

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

stdlib/public/runtime/ImageInspectionMachO.cpp

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

27+
#ifndef SWIFT_RUNTIME_DLADDR_ALLOW
28+
#error "SWIFT_RUNTIME_DLADDR_ALLOW must be defined"
29+
#endif
30+
2731
using namespace swift;
2832

2933
namespace {
@@ -72,6 +76,7 @@ void swift::initializeTypeMetadataRecordLookup() {
7276
}
7377

7478
int swift::lookupSymbol(const void *address, SymbolInfo *info) {
79+
#if SWIFT_RUNTIME_DLADDR_ALLOW
7580
Dl_info dlinfo;
7681
if (dladdr(address, &dlinfo) == 0) {
7782
return 0;
@@ -82,6 +87,9 @@ int swift::lookupSymbol(const void *address, SymbolInfo *info) {
8287
info->symbolName = dlinfo.dli_sname;
8388
info->symbolAddress = dlinfo.dli_saddr;
8489
return 1;
90+
#else
91+
return 0;
92+
#endif
8593
}
8694

8795
#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)