Skip to content

Commit 5b1daa9

Browse files
committed
Conditionally wrap (de)mangling symbols in an inline namespace.
Since libDemangling is included in the Swift standard library, ODR violations can occur on platforms that allow statically linking stdlib if Swift code is linked with other compiler libraries that also transitively pull in libDemangling, and if the stdlib version and compiler version do not match exactly (even down to commit drift between releases). This lets the runtime conditionally segregate its copies of the libDemangling symbols from those in the compiler using an inline namespace without affecting usage throughout source.
1 parent cb0cdbc commit 5b1daa9

File tree

17 files changed

+90
-2
lines changed

17 files changed

+90
-2
lines changed

include/swift/AST/ASTDemangler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
#include "llvm/ADT/StringRef.h"
2727
#include "swift/AST/Types.h"
2828
#include "swift/Demangling/Demangler.h"
29+
#include "swift/Demangling/NamespaceMacros.h"
2930
#include "swift/Demangling/TypeDecoder.h"
3031

3132
namespace swift {
3233

3334
class TypeDecl;
3435

3536
namespace Demangle {
37+
SWIFT_BEGIN_INLINE_NAMESPACE
3638

3739
Type getTypeForMangling(ASTContext &ctx,
3840
llvm::StringRef mangling);
@@ -178,6 +180,7 @@ class ASTBuilder {
178180
Demangle::Node::Kind kind);
179181
};
180182

183+
SWIFT_END_INLINE_NAMESPACE
181184
} // namespace Demangle
182185

183186
} // namespace swift

include/swift/Basic/Mangler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SWIFT_BASIC_MANGLER_H
1515

1616
#include "swift/Demangling/ManglingUtils.h"
17+
#include "swift/Demangling/NamespaceMacros.h"
1718
#include "swift/Basic/Debug.h"
1819
#include "swift/Basic/LLVM.h"
1920
#include "llvm/ADT/DenseMap.h"
@@ -24,6 +25,7 @@
2425

2526
namespace swift {
2627
namespace Mangle {
28+
SWIFT_BEGIN_INLINE_NAMESPACE
2729

2830
void printManglingStats();
2931

@@ -204,6 +206,7 @@ class Mangler {
204206
}
205207
};
206208

209+
SWIFT_END_INLINE_NAMESPACE
207210
} // end namespace Mangle
208211
} // end namespace swift
209212

include/swift/Demangling/Demangle.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
#include <cstdint>
2626
#include "llvm/ADT/StringRef.h"
2727
#include "swift/Runtime/Config.h"
28+
#include "swift/Demangling/NamespaceMacros.h"
2829

2930
namespace llvm {
3031
class raw_ostream;
3132
}
3233

3334
namespace swift {
3435
namespace Demangle {
36+
SWIFT_BEGIN_INLINE_NAMESPACE
3537

3638
enum class SymbolicReferenceKind : uint8_t;
3739

@@ -621,6 +623,7 @@ bool isFunctionAttr(Node::Kind kind);
621623
/// contain symbolic references.
622624
llvm::StringRef makeSymbolicMangledNameStringRef(const char *base);
623625

626+
SWIFT_END_INLINE_NAMESPACE
624627
} // end namespace Demangle
625628
} // end namespace swift
626629

include/swift/Demangling/Demangler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define SWIFT_DEMANGLING_DEMANGLER_H
2121

2222
#include "swift/Demangling/Demangle.h"
23+
#include "swift/Demangling/NamespaceMacros.h"
2324

2425
//#define NODE_FACTORY_DEBUGGING
2526

@@ -28,6 +29,7 @@ using llvm::StringRef;
2829

2930
namespace swift {
3031
namespace Demangle {
32+
SWIFT_BEGIN_INLINE_NAMESPACE
3133

3234
class CharVector;
3335

@@ -622,6 +624,7 @@ template <size_t Size> class StackAllocatedDemangler : public Demangler {
622624

623625
NodePointer demangleOldSymbolAsNode(StringRef MangledName,
624626
NodeFactory &Factory);
627+
SWIFT_END_INLINE_NAMESPACE
625628
} // end namespace Demangle
626629
} // end namespace swift
627630

include/swift/Demangling/ManglingUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
#define SWIFT_DEMANGLING_MANGLINGUTILS_H
1515

1616
#include "llvm/ADT/StringRef.h"
17+
#include "swift/Demangling/NamespaceMacros.h"
1718
#include "swift/Demangling/Punycode.h"
1819

1920
namespace swift {
2021
namespace Mangle {
22+
SWIFT_BEGIN_INLINE_NAMESPACE
2123

2224
using llvm::StringRef;
2325

@@ -311,6 +313,7 @@ class SubstitutionMerging {
311313
}
312314
};
313315

316+
SWIFT_END_INLINE_NAMESPACE
314317
} // end namespace Mangle
315318
} // end namespace swift
316319

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- NamespaceMacros.h - Macros for inline namespaces -------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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+
// Macros that conditionally define an inline namespace so that symbols used in
14+
// multiple places (such as in the compiler and in the runtime library) can be
15+
// given distinct mangled names in different contexts without affecting client
16+
// usage in source.
17+
//
18+
//===----------------------------------------------------------------------===//
19+
20+
#ifndef SWIFT_DEMANGLING_NAMESPACE_MACROS_H
21+
#define SWIFT_DEMANGLING_NAMESPACE_MACROS_H
22+
23+
#if defined(__cplusplus)
24+
25+
#if defined(SWIFT_INLINE_NAMESPACE)
26+
#define SWIFT_BEGIN_INLINE_NAMESPACE inline namespace SWIFT_INLINE_NAMESPACE {
27+
#define SWIFT_END_INLINE_NAMESPACE }
28+
#else
29+
#define SWIFT_BEGIN_INLINE_NAMESPACE
30+
#define SWIFT_END_INLINE_NAMESPACE
31+
#endif
32+
33+
#endif
34+
35+
#endif // SWIFT_DEMANGLING_NAMESPACE_MACROS_H

include/swift/Demangling/Punycode.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
#define SWIFT_DEMANGLING_PUNYCODE_H
2929

3030
#include "llvm/ADT/StringRef.h"
31+
#include "swift/Demangling/NamespaceMacros.h"
3132
#include <vector>
3233
#include <cstdint>
3334

3435
namespace swift {
3536
namespace Punycode {
37+
SWIFT_BEGIN_INLINE_NAMESPACE
3638

3739
using llvm::StringRef;
3840

@@ -58,6 +60,7 @@ bool encodePunycodeUTF8(StringRef InputUTF8, std::string &OutPunycode,
5860

5961
bool decodePunycodeUTF8(StringRef InputPunycode, std::string &OutUTF8);
6062

63+
SWIFT_END_INLINE_NAMESPACE
6164
} // end namespace Punycode
6265
} // end namespace swift
6366

include/swift/Demangling/TypeDecoder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "swift/ABI/MetadataValues.h"
2222
#include "swift/Demangling/Demangler.h"
23+
#include "swift/Demangling/NamespaceMacros.h"
2324
#include "swift/Basic/LLVM.h"
2425
#include "swift/Runtime/Unreachable.h"
2526
#include "swift/Strings.h"
@@ -28,6 +29,7 @@
2829

2930
namespace swift {
3031
namespace Demangle {
32+
SWIFT_BEGIN_INLINE_NAMESPACE
3133

3234
enum class ImplMetatypeRepresentation {
3335
Thin,
@@ -1152,6 +1154,7 @@ decodeMangledType(BuilderType &Builder,
11521154
}
11531155

11541156

1157+
SWIFT_END_INLINE_NAMESPACE
11551158
} // end namespace Demangle
11561159
} // end namespace swift
11571160

include/swift/SILOptimizer/Utils/SpecializationMangler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
#define SWIFT_SILOPTIMIZER_UTILS_SPECIALIZATIONMANGLER_H
1515

1616
#include "swift/Demangling/Demangler.h"
17+
#include "swift/Demangling/NamespaceMacros.h"
1718
#include "swift/Basic/NullablePtr.h"
1819
#include "swift/AST/ASTMangler.h"
1920
#include "swift/SIL/SILLinkage.h"
2021
#include "swift/SIL/SILFunction.h"
2122

2223
namespace swift {
2324
namespace Mangle {
25+
SWIFT_BEGIN_INLINE_NAMESPACE
2426

2527
enum class SpecializationKind : uint8_t {
2628
Generic,
@@ -169,6 +171,7 @@ class FunctionSignatureSpecializationMangler : public SpecializationMangler {
169171
void mangleReturnValue(ReturnValueModifierIntBase RetMod);
170172
};
171173

174+
SWIFT_END_INLINE_NAMESPACE
172175
} // end namespace Mangle
173176
} // end namespace swift
174177

lib/Demangling/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ add_swift_host_library(swiftDemangling STATIC
1111
target_compile_definitions(swiftDemangling PRIVATE
1212
LLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1)
1313

14+
# NOTE: Runtime libraries that depend on swiftDemangling should define
15+
# SWIFT_INLINE_NAMESPACE to specify the identifier that will be used for an
16+
# inline namespace that will be added around the symbols defined by this
17+
# library. This keeps the demangling symbols in those libraries distinct
18+
# from those in the compiler, which prevents ODR violations in certain
19+
# contexts; for example, on platforms that support statically linking the
20+
# Swift standard library, it allows this to happen safely when the binary
21+
# also links in compiler libraries that may not match exactly with the
22+
# runtime version.

0 commit comments

Comments
 (0)