Skip to content

Commit b3b080e

Browse files
authored
Merge pull request #42109 from al45tair/eng/PR-91095592
[Demangling] Fix duplicate _gCRAnnotations symbol problems.
2 parents 2d370fb + 7c48b83 commit b3b080e

File tree

12 files changed

+198
-126
lines changed

12 files changed

+198
-126
lines changed

lib/Demangling/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
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

lib/Demangling/CrashReporter.cpp

Lines changed: 35 additions & 0 deletions
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

lib/Demangling/CrashReporter.h

Lines changed: 50 additions & 0 deletions
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

lib/Demangling/Errors.cpp

Lines changed: 38 additions & 83 deletions
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,14 @@
3333
#include <android/log.h>
3434
#endif
3535

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

40-
#if !SWIFT_DEMANGLE_USE_RUNTIME_ERRORS
42+
#include "CrashReporter.h"
43+
#endif // SWIFT_HAVE_CRASHREPORTERCLIENT
4144

4245
// -- Declarations -----------------------------------------------------------
4346

@@ -53,77 +56,39 @@ static int demangle_asprintf(char **strp, const char *format, ...);
5356

5457
// -- Crash reporter integration ---------------------------------------------
5558

56-
#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-
9459
// Report a message to any forthcoming crash log.
9560
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);
117-
}
118-
61+
#if SWIFT_HAVE_CRASHREPORTERCLIENT
62+
char *oldMessage = nullptr;
63+
char *newMessage = nullptr;
64+
65+
oldMessage = std::atomic_load_explicit(
66+
(volatile std::atomic<char *> *)&gCRAnnotations.message,
67+
SWIFT_MEMORY_ORDER_CONSUME);
68+
69+
do {
70+
if (newMessage) {
71+
free(newMessage);
72+
newMessage = nullptr;
73+
}
74+
75+
if (oldMessage) {
76+
demangle_asprintf(&newMessage, "%s%s", oldMessage, message);
77+
} else {
78+
newMessage = strdup(message);
79+
}
80+
} while (!std::atomic_compare_exchange_strong_explicit(
81+
(volatile std::atomic<char *> *)&gCRAnnotations.message,
82+
&oldMessage, newMessage,
83+
std::memory_order_release,
84+
SWIFT_MEMORY_ORDER_CONSUME));
85+
86+
if (oldMessage && malloc_size(oldMessage))
87+
free(oldMessage);
11988
#else
120-
static void
121-
122-
reportOnCrash(uint32_t flags, const char *message) {
12389
// empty
124-
}
125-
12690
#endif // SWIFT_HAVE_CRASHREPORTERCLIENT
91+
}
12792

12893
// -- Utility functions ------------------------------------------------------
12994

@@ -219,8 +184,6 @@ static void demangleWarn(uint32_t flags, const char *format, va_list val) {
219184
free(message);
220185
}
221186

222-
#endif // !SWIFT_DEMANGLE_USE_RUNTIME_ERRORS
223-
224187
// -- Public API -------------------------------------------------------------
225188

226189
namespace swift {
@@ -243,19 +206,11 @@ void warn(uint32_t flags, const char *format, ...) {
243206
}
244207

245208
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
249209
demangleFatal(flags, format, val);
250-
#endif
251210
}
252211

253212
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
257213
demangleWarn(flags, format, val);
258-
#endif
259214
}
260215

261216
SWIFT_END_INLINE_NAMESPACE

stdlib/public/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
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()

stdlib/public/Reflection/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
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)

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
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)

stdlib/public/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
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
Lines changed: 35 additions & 0 deletions
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)