Skip to content

Commit eb6f9e0

Browse files
authored
Merge pull request #41452 from al45tair/eng/PR-89139049
[Demangler] Make Node::addChild(NULL, ...) always assert.
2 parents 67c097a + b253705 commit eb6f9e0

File tree

15 files changed

+402
-21
lines changed

15 files changed

+402
-21
lines changed

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,15 @@ option(SWIFT_EMBED_BITCODE_SECTION_HIDE_SYMBOLS
327327
"If non-empty, when embedding the LLVM bitcode binary sections into the relevant binaries, pass in -bitcode_hide_symbols. Does nothing if SWIFT_EMBED_BITCODE_SECTION is set to false."
328328
FALSE)
329329

330+
if("${SWIFT_HOST_VARIANT_SDK}" MATCHES "(OSX|IOS*|TVOS*|WATCHOS*)")
331+
set(SWIFT_RUNTIME_CRASH_REPORTER_CLIENT_default TRUE)
332+
else()
333+
set(SWIFT_RUNTIME_CRASH_REPORTER_CLIENT_default FALSE)
334+
endif()
335+
330336
option(SWIFT_RUNTIME_CRASH_REPORTER_CLIENT
331337
"Whether to enable CrashReporter integration"
332-
FALSE)
338+
"${SWIFT_RUNTIME_CRASH_REPORTER_CLIENT_default}")
333339

334340
set(SWIFT_DARWIN_XCRUN_TOOLCHAIN "XcodeDefault" CACHE STRING
335341
"The name of the toolchain to pass to 'xcrun'")

include/swift/Demangling/Demangle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef SWIFT_DEMANGLING_DEMANGLE_H
2020
#define SWIFT_DEMANGLING_DEMANGLE_H
2121

22+
#include "swift/Demangling/Errors.h"
2223
#include "swift/Demangling/NamespaceMacros.h"
2324
#include "llvm/ADT/StringRef.h"
2425
#include "llvm/Support/Compiler.h"

include/swift/Demangling/Errors.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===--- Errors.h - Demangling library error handling -----------*- 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+
// This file exists because not every client links to libswiftCore (the
14+
// runtime), so calling swift::fatalError() or swift::warning() from within
15+
// the demangler is not an option.
16+
//
17+
//===----------------------------------------------------------------------===//
18+
19+
#ifndef SWIFT_DEMANGLING_ERRORS_H
20+
#define SWIFT_DEMANGLING_ERRORS_H
21+
22+
#include "../../../stdlib/public/SwiftShims/Visibility.h"
23+
#include "swift/Demangling/NamespaceMacros.h"
24+
#include <inttypes.h>
25+
#include <stdarg.h>
26+
27+
#ifndef SWIFT_FORMAT
28+
// SWIFT_FORMAT(fmt,first) marks a function as taking a format string argument
29+
// at argument `fmt`, with the first argument for the format string as `first`.
30+
#if __has_attribute(format)
31+
#define SWIFT_FORMAT(fmt, first) __attribute__((format(printf, fmt, first)))
32+
#else
33+
#define SWIFT_FORMAT(fmt, first)
34+
#endif
35+
#endif
36+
37+
#ifndef SWIFT_VFORMAT
38+
// SWIFT_VFORMAT(fmt) marks a function as taking a format string argument at
39+
// argument `fmt`, with the arguments in a `va_list`.
40+
#if __has_attribute(format)
41+
#define SWIFT_VFORMAT(fmt) __attribute__((format(printf, fmt, 0)))
42+
#else
43+
#define SWIFT_VFORMAT(fmt)
44+
#endif
45+
#endif
46+
47+
namespace swift {
48+
namespace Demangle {
49+
SWIFT_BEGIN_INLINE_NAMESPACE
50+
51+
SWIFT_NORETURN SWIFT_FORMAT(2, 3) void fatal(uint32_t flags, const char *format,
52+
...);
53+
SWIFT_FORMAT(2, 3) void warn(uint32_t flags, const char *format, ...);
54+
55+
SWIFT_NORETURN SWIFT_VFORMAT(2) void fatalv(uint32_t flags, const char *format,
56+
va_list val);
57+
SWIFT_VFORMAT(2) void warnv(uint32_t flags, const char *format, va_list val);
58+
59+
SWIFT_END_INLINE_NAMESPACE
60+
} // end namespace Demangle
61+
} // end namespace swift
62+
63+
#endif // SWIFT_DEMANGLING_DEMANGLE_H

include/swift/Runtime/Debug.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ static inline void crash(const char *message) {
8282
swift_unreachable("Expected compiler to crash.");
8383
}
8484

85-
// swift::fatalError() halts with a crash log message,
85+
// swift::fatalErrorv() halts with a crash log message,
86+
// but makes no attempt to preserve register state.
87+
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
88+
SWIFT_VFORMAT(2)
89+
extern void fatalErrorv(uint32_t flags, const char *format, va_list args);
90+
91+
// swift::fatalError() halts with a crash log message,
8692
// but makes no attempt to preserve register state.
8793
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
8894
SWIFT_FORMAT(2, 3)

lib/Demangling/CMakeLists.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
set(swift_demangling_compile_flags
2+
LLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1
3+
SWIFT_SUPPORT_OLD_MANGLING=1
4+
SWIFT_STDLIB_HAS_TYPE_PRINTING=1)
5+
6+
if(SWIFT_RUNTIME_CRASH_REPORTER_CLIENT)
7+
list(APPEND swift_demangling_compile_flags
8+
"-DSWIFT_HAVE_CRASHREPORTERCLIENT=1")
9+
endif()
10+
111
add_swift_host_library(swiftDemangling STATIC
212
Demangler.cpp
313
Context.cpp
@@ -7,11 +17,10 @@ add_swift_host_library(swiftDemangling STATIC
717
OldDemangler.cpp
818
OldRemangler.cpp
919
Punycode.cpp
10-
Remangler.cpp)
20+
Remangler.cpp
21+
Errors.cpp)
1122
target_compile_definitions(swiftDemangling PRIVATE
12-
LLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1
13-
SWIFT_SUPPORT_OLD_MANGLING=1
14-
SWIFT_STDLIB_HAS_TYPE_PRINTING=1)
23+
${swift_demangling_compile_flags})
1524

1625
# NOTE: Runtime libraries that depend on swiftDemangling should define
1726
# SWIFT_INLINE_NAMESPACE to specify the identifier that will be used for an

lib/Demangling/Demangler.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,12 @@ static bool isRequirement(Node::Kind kind) {
9393

9494
void swift::Demangle::failAssert(const char *file, unsigned line,
9595
NodePointer node, const char *expr) {
96-
fprintf(stderr, "%s:%u: assertion failed for Node %p: %s", file, line, node,
97-
expr);
98-
abort();
96+
std::string treeStr = getNodeTreeAsString(node);
97+
98+
fatal(0,
99+
"%s:%u: assertion failed for Node %p: %s\n"
100+
"%s:%u: Node %p is:\n%s\n",
101+
file, line, node, expr, file, line, node, treeStr.c_str());
99102
}
100103

101104
bool swift::Demangle::isContext(Node::Kind kind) {
@@ -338,7 +341,7 @@ Node::iterator Node::end() const {
338341
}
339342

340343
void Node::addChild(NodePointer Child, NodeFactory &Factory) {
341-
assert(Child);
344+
DEMANGLER_ALWAYS_ASSERT(Child, this);
342345
switch (NodePayloadKind) {
343346
case PayloadKind::None:
344347
InlineChildren[0] = Child;
@@ -748,7 +751,7 @@ NodePointer Demangler::demangleSymbolicReference(unsigned char rawKind) {
748751
NodePointer resolved = nullptr;
749752
if (SymbolicReferenceResolver)
750753
resolved = SymbolicReferenceResolver(kind, direct, value, at);
751-
754+
752755
// With no resolver, or a resolver that failed, refuse to demangle further.
753756
if (!resolved)
754757
return nullptr;

lib/Demangling/DemanglerAssert.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242

4343
#endif // SWIFT_RUNTIME || defined(NDEBUG)
4444

45+
// DEMANGLER_ALWAYS_ASSERT() *always* fails the program, even in the runtime
46+
#define DEMANGLER_ALWAYS_ASSERT(expr, node) \
47+
do { \
48+
if (!(expr)) \
49+
swift::Demangle::failAssert(__FILE__, __LINE__, node, #expr); \
50+
} while (0)
51+
4552
namespace swift {
4653
namespace Demangle {
4754
SWIFT_BEGIN_INLINE_NAMESPACE

0 commit comments

Comments
 (0)