Skip to content

Commit 3f49b8b

Browse files
committed
[Demangling] Fix duplicate _gCRAnnotations symbol problems.
Moved the _gCRAnnotations declarations to their own object module, which will help to avoid duplicate symbol problems (at least with .a files). Also tweaked things to make it so that the demangler and runtime versions of the message setting code will interoperate (and so that they'll interoperate better with other implementations that might creep in from somewhere, like the one in LLVMSupport). rdar://91095592
1 parent 66999ce commit 3f49b8b

File tree

12 files changed

+192
-121
lines changed

12 files changed

+192
-121
lines changed

Diff for: lib/Demangling/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ add_swift_host_library(swiftDemangling STATIC
1818
OldRemangler.cpp
1919
Punycode.cpp
2020
Remangler.cpp
21-
Errors.cpp)
21+
Errors.cpp
22+
CrashReporter.cpp)
2223
target_compile_definitions(swiftDemangling PRIVATE
2324
${swift_demangling_compile_flags})
2425

Diff for: lib/Demangling/CrashReporter.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- CrashReporter.cpp - Crash Reporter integration ---------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 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+
// Declares gCRAnnotations. This lets us link with other static libraries
14+
// that also declare gCRAnnotations, because we'll pull in their copy
15+
// (assuming they're linked first).
16+
//
17+
//===----------------------------------------------------------------------===//
18+
19+
#include "CrashReporter.h"
20+
21+
#if SWIFT_HAVE_CRASHREPORTERCLIENT
22+
23+
// Instead of linking to CrashReporterClient.a (because it complicates the
24+
// build system), define the only symbol from that static archive ourselves.
25+
//
26+
// The layout of this struct is CrashReporter ABI, so there are no ABI concerns
27+
// here.
28+
extern "C" {
29+
SWIFT_LIBRARY_VISIBILITY
30+
struct crashreporter_annotations_t gCRAnnotations __attribute__((
31+
__section__("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) = {
32+
CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0, 0};
33+
}
34+
35+
#endif

Diff for: lib/Demangling/CrashReporter.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===--- CrashReporter.h - Crash Reporter integration -----------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 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+
// Declares gCRAnnotations. This lets us link with other static libraries
14+
// that also declare gCRAnnotations, because we'll pull in their copy
15+
// (assuming they're linked first).
16+
//
17+
//===----------------------------------------------------------------------===//
18+
19+
#ifndef SWIFT_DEMANGLING_CRASHREPORTER_H
20+
#define SWIFT_DEMANGLING_CRASHREPORTER_H
21+
22+
#if SWIFT_HAVE_CRASHREPORTERCLIENT
23+
24+
// For SWIFT_LIBRARY_VISIBILITY
25+
#include "../../stdlib/public/SwiftShims/Visibility.h"
26+
27+
#include <inttypes.h>
28+
29+
#define CRASHREPORTER_ANNOTATIONS_VERSION 5
30+
#define CRASHREPORTER_ANNOTATIONS_SECTION "__crash_info"
31+
32+
struct crashreporter_annotations_t {
33+
uint64_t version; // unsigned long
34+
uint64_t message; // char *
35+
uint64_t signature_string; // char *
36+
uint64_t backtrace; // char *
37+
uint64_t message2; // char *
38+
uint64_t thread; // uint64_t
39+
uint64_t dialog_mode; // unsigned int
40+
uint64_t abort_cause; // unsigned int
41+
};
42+
43+
extern "C" {
44+
SWIFT_LIBRARY_VISIBILITY
45+
extern struct crashreporter_annotations_t gCRAnnotations;
46+
}
47+
48+
#endif // SWIFT_HAVE_CRASHREPORTERCLIENT
49+
50+
#endif // SWIFT_DEMANGLING_CRASHREPORTER_H

Diff for: lib/Demangling/Errors.cpp

+34-78
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//
13-
// This file is the public API of the demangler library.
14-
// Tools which use the demangler library (like lldb) must include this - and
15-
// only this - header file.
13+
// Provides the demangling library with its own error handling functions.
14+
// This is necessary because it isn't always linked with the runtime, so
15+
// it can't use the runtime's error handling.
1616
//
1717
//===----------------------------------------------------------------------===//
1818

@@ -33,11 +33,11 @@
3333
#include <android/log.h>
3434
#endif
3535

36-
#if SWIFT_DEMANGLE_USE_RUNTIME_ERRORS
37-
#include "swift/Runtime/Debug.h"
38-
#endif
39-
40-
#if !SWIFT_DEMANGLE_USE_RUNTIME_ERRORS
36+
#if SWIFT_HAVE_CRASHREPORTERCLIENT
37+
#include <atomic>
38+
#include <malloc/malloc.h>
39+
#include "CrashReporter.h"
40+
#endif // SWIFT_HAVE_CRASHREPORTERCLIENT
4141

4242
// -- Declarations -----------------------------------------------------------
4343

@@ -54,75 +54,41 @@ static int demangle_asprintf(char **strp, const char *format, ...);
5454
// -- Crash reporter integration ---------------------------------------------
5555

5656
#if SWIFT_HAVE_CRASHREPORTERCLIENT
57-
#include <malloc/malloc.h>
58-
#include <pthread.h>
59-
60-
#define CRASHREPORTER_ANNOTATIONS_VERSION 5
61-
#define CRASHREPORTER_ANNOTATIONS_SECTION "__crash_info"
62-
63-
struct crashreporter_annotations_t {
64-
uint64_t version; // unsigned long
65-
uint64_t message; // char *
66-
uint64_t signature_string; // char *
67-
uint64_t backtrace; // char *
68-
uint64_t message2; // char *
69-
uint64_t thread; // uint64_t
70-
uint64_t dialog_mode; // unsigned int
71-
uint64_t abort_cause; // unsigned int
72-
};
73-
74-
// Instead of linking to CrashReporterClient.a (because it complicates the
75-
// build system), define the only symbol from that static archive ourselves.
76-
//
77-
// The layout of this struct is CrashReporter ABI, so there are no ABI concerns
78-
// here.
79-
extern "C" {
80-
SWIFT_LIBRARY_VISIBILITY
81-
struct crashreporter_annotations_t gCRAnnotations __attribute__((
82-
__section__("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) = {
83-
CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0, 0};
84-
}
85-
86-
static inline void CRSetCrashLogMessage(const char *message) {
87-
gCRAnnotations.message = reinterpret_cast<uint64_t>(message);
88-
}
89-
90-
static inline const char *CRGetCrashLogMessage() {
91-
return reinterpret_cast<const char *>(gCRAnnotations.message);
92-
}
93-
9457
// Report a message to any forthcoming crash log.
9558
static void reportOnCrash(uint32_t flags, const char *message) {
96-
// We must use an "unsafe" mutex in this pathway since the normal "safe"
97-
// mutex calls fatal when an error is detected and fatal ends up
98-
// calling us. In other words we could get infinite recursion if the
99-
// mutex errors.
100-
static pthread_mutex_t crashlogLock = PTHREAD_MUTEX_INITIALIZER;
101-
102-
pthread_mutex_lock(&crashlogLock);
103-
104-
char *oldMessage = const_cast<char *>(CRGetCrashLogMessage());
105-
char *newMessage;
106-
if (oldMessage) {
107-
demangle_asprintf(&newMessage, "%s%s", oldMessage, message);
108-
if (malloc_size(oldMessage))
109-
free(oldMessage);
110-
} else {
111-
newMessage = strdup(message);
112-
}
113-
114-
CRSetCrashLogMessage(newMessage);
115-
116-
pthread_mutex_unlock(&crashlogLock);
59+
char *oldMessage = nullptr;
60+
char *newMessage = nullptr;
61+
62+
oldMessage = std::atomic_load_explicit(
63+
(volatile std::atomic<char *> *)&gCRAnnotations.message,
64+
std::memory_order_relaxed);
65+
66+
do {
67+
if (newMessage) {
68+
free(newMessage);
69+
newMessage = nullptr;
70+
}
71+
72+
if (oldMessage) {
73+
demangle_asprintf(&newMessage, "%s%s", oldMessage, message);
74+
} else {
75+
newMessage = strdup(message);
76+
}
77+
} while (!std::atomic_compare_exchange_strong_explicit(
78+
(volatile std::atomic<char *> *)&gCRAnnotations.message,
79+
&oldMessage, newMessage,
80+
std::memory_order_release,
81+
std::memory_order_relaxed));
82+
83+
if (oldMessage && malloc_size(oldMessage))
84+
free(oldMessage);
11785
}
11886

11987
#else
12088
static void
121-
12289
reportOnCrash(uint32_t flags, const char *message) {
12390
// empty
12491
}
125-
12692
#endif // SWIFT_HAVE_CRASHREPORTERCLIENT
12793

12894
// -- Utility functions ------------------------------------------------------
@@ -219,8 +185,6 @@ static void demangleWarn(uint32_t flags, const char *format, va_list val) {
219185
free(message);
220186
}
221187

222-
#endif // !SWIFT_DEMANGLE_USE_RUNTIME_ERRORS
223-
224188
// -- Public API -------------------------------------------------------------
225189

226190
namespace swift {
@@ -243,19 +207,11 @@ void warn(uint32_t flags, const char *format, ...) {
243207
}
244208

245209
SWIFT_NORETURN void fatalv(uint32_t flags, const char *format, va_list val) {
246-
#if SWIFT_DEMANGLE_USE_RUNTIME_ERRORS
247-
swift::fatalErrorv(flags, format, val);
248-
#else
249210
demangleFatal(flags, format, val);
250-
#endif
251211
}
252212

253213
void warnv(uint32_t flags, const char *format, va_list val) {
254-
#if SWIFT_DEMANGLE_USE_RUNTIME_ERRORS
255-
swift::warningv(flags, format, val);
256-
#else
257214
demangleWarn(flags, format, val);
258-
#endif
259215
}
260216

261217
SWIFT_END_INLINE_NAMESPACE

Diff for: stdlib/public/CMakeLists.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR)
7979
"${SWIFT_SOURCE_DIR}/lib/Demangling/Remangler.cpp"
8080
"${SWIFT_SOURCE_DIR}/lib/Demangling/NodeDumper.cpp"
8181
"${SWIFT_SOURCE_DIR}/lib/Demangling/Errors.cpp")
82+
set(swiftDemanglingCRSources
83+
"${SWIFT_SOURCE_DIR}/lib/Demangling/CrashReporter.cpp")
8284

8385
set(swift_demangling_cflags)
8486

@@ -109,11 +111,10 @@ if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR)
109111
${swift_demangling_cflags}
110112
INSTALL_IN_COMPONENT never_install)
111113

112-
add_swift_target_library(swiftDemanglingForCore OBJECT_LIBRARY
113-
${swiftDemanglingSources}
114+
add_swift_target_library(swiftDemanglingCR OBJECT_LIBRARY
115+
${swiftDemanglingCRSources}
114116
C_COMPILE_FLAGS
115117
-DswiftCore_EXPORTS
116-
-DSWIFT_DEMANGLE_USE_RUNTIME_ERRORS=1
117118
${swift_demangling_cflags}
118119
INSTALL_IN_COMPONENT never_install)
119120
endif()

Diff for: stdlib/public/Reflection/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ add_swift_target_library(swiftReflection STATIC
1414
C_COMPILE_FLAGS ${SWIFT_RUNTIME_CXX_FLAGS} ${swiftReflection_C_COMPILE_FLAGS}
1515
LINK_FLAGS ${SWIFT_RUNTIME_LINK_FLAGS}
1616
INCORPORATE_OBJECT_LIBRARIES
17-
swiftLLVMSupport swiftDemangling
17+
swiftLLVMSupport swiftDemangling swiftDemanglingCR
1818
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
1919
INSTALL_IN_COMPONENT dev)

Diff for: stdlib/public/core/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ endif()
295295
set(swift_core_incorporate_object_libraries)
296296
list(APPEND swift_core_incorporate_object_libraries swiftRuntime)
297297
list(APPEND swift_core_incorporate_object_libraries swiftLLVMSupport)
298-
list(APPEND swift_core_incorporate_object_libraries swiftDemanglingForCore)
298+
list(APPEND swift_core_incorporate_object_libraries swiftDemangling)
299299
list(APPEND swift_core_incorporate_object_libraries swiftStdlibStubs)
300300
if(SWIFT_STDLIB_HAS_COMMANDLINE)
301301
list(APPEND swift_core_incorporate_object_libraries swiftCommandLineSupport)

Diff for: stdlib/public/runtime/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set(swift_runtime_sources
3232
AutoDiffSupport.cpp
3333
Bincompat.cpp
3434
Casting.cpp
35+
CrashReporter.cpp
3536
CygwinPort.cpp
3637
Demangle.cpp
3738
DynamicCast.cpp

Diff for: stdlib/public/runtime/CrashReporter.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- CrashReporter.cpp - Crash Reporter integration ---------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 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+
// Declares gCRAnnotations. This lets us link with other static libraries
14+
// that also declare gCRAnnotations, because we'll pull in their copy
15+
// (assuming they're linked first).
16+
//
17+
//===----------------------------------------------------------------------===//
18+
19+
#include "swift/Runtime/Debug.h"
20+
21+
#if SWIFT_HAVE_CRASHREPORTERCLIENT
22+
23+
// Instead of linking to CrashReporterClient.a (because it complicates the
24+
// build system), define the only symbol from that static archive ourselves.
25+
//
26+
// The layout of this struct is CrashReporter ABI, so there are no ABI concerns
27+
// here.
28+
extern "C" {
29+
SWIFT_LIBRARY_VISIBILITY
30+
struct crashreporter_annotations_t gCRAnnotations __attribute__((
31+
__section__("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) = {
32+
CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0, 0};
33+
}
34+
35+
#endif

0 commit comments

Comments
 (0)