Skip to content

Commit

Permalink
[Demangling] Improve DEMANGLE_ASSERT, enable some disabled tests.
Browse files Browse the repository at this point in the history
Defining DEMANGLE_ASSERT as assert() is simple but messes up the file/line
information from assert().  Make a dedicated assertion failure function and use
that instead to fix.

Also enable some tests that were disabled because they triggered abort()
calls in the remanglers.

rdar://79725187
  • Loading branch information
al45tair committed Sep 7, 2021
1 parent 22f8e13 commit 59cfe35
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
13 changes: 12 additions & 1 deletion lib/Demangling/Demangler.cpp
Expand Up @@ -15,10 +15,13 @@
//===----------------------------------------------------------------------===//

#include "swift/Demangling/Demangler.h"
#include "swift/Demangling/ManglingUtils.h"
#include "DemanglerAssert.h"
#include "swift/Demangling/ManglingMacros.h"
#include "swift/Demangling/ManglingUtils.h"
#include "swift/Demangling/Punycode.h"
#include "swift/Strings.h"
#include <stdio.h>
#include <stdlib.h>

using namespace swift;
using namespace Mangle;
Expand Down Expand Up @@ -87,6 +90,14 @@ static bool isRequirement(Node::Kind kind) {
// Public utility functions //
//////////////////////////////////

SWIFT_NORETURN void swift::Demangle::failAssert(const char *file, unsigned line,
NodePointer node,
const char *expr) {
fprintf(stderr, "%s:%u: assertion failed for Node %p: %s", file, line, node,
expr);
abort();
}

bool swift::Demangle::isContext(Node::Kind kind) {
switch (kind) {
#define NODE(ID)
Expand Down
35 changes: 32 additions & 3 deletions lib/Demangling/DemanglerAssert.h
@@ -1,4 +1,4 @@
//===--- DemanglerAssert.h - Assertions for de/re-mangling ----------------===//
//===--- DemanglerAssert.h - Assertions for de/re-mangling ------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
Expand All @@ -18,14 +18,43 @@
#ifndef SWIFT_DEMANGLING_ASSERT_H
#define SWIFT_DEMANGLING_ASSERT_H

#include "swift/Demangling/Demangle.h"
#include "swift/Demangling/NamespaceMacros.h"

#if SWIFT_RUNTIME

// In the runtime, DEMANGLER_ASSERT() returns an error
#define DEMANGLER_ASSERT(expr, node) \
do { \
if (!(expr)) \
return ManglingError(ManglingError::AssertionFailed, (node), __LINE__); \
} while (0)

#elif !defined(NDEBUG)

// If NDEBUG is not defined, DEMANGLER_ASSERT() works like assert()
#define DEMANGLER_ASSERT(expr, node) \
do { \
if (!(expr)) \
swift::Demangle::failAssert(__FILE__, __LINE__, node, #expr); \
} while (0)

#else
#define DEMANGLER_ASSERT(expr, node) assert(expr)
#endif

// Otherwise, DEMANGLER_ASSERT() does nothing
#define DEMANGLER_ASSERT(expr, node)

#endif // SWIFT_RUNTIME

namespace swift {
namespace Demangle {
SWIFT_BEGIN_INLINE_NAMESPACE

SWIFT_NORETURN void failAssert(const char *file, unsigned line,
NodePointer node, const char *expr);

SWIFT_END_INLINE_NAMESPACE
} // end namespace Demangle
} // end namespace swift

#endif // SWIFT_DEMANGLING_ASSERT_H
17 changes: 7 additions & 10 deletions test/Demangle/Inputs/objc-getclass.txt
@@ -1,16 +1,13 @@
# These were found by fuzzing getObjCClassByMangledName

# rdar://63485806
# This results in an abort(), whereas it should be an error; rdar://79725187
# covers improving error handling; until that's done, disable this test case

# 3…KySSyGSkySySSGiG3(KˇˇˇˇˇˇˇˇˇˇˇˇˇˇCwKySSiKySS
# SSmSySyySGGSGyGSyySyySySSGGSGyS78iSLccSGSyySSySSGGccLcV1yVS~^§!zzzzzzzzzzzzhzzzzzSLzSEzzzzzzzzzzzzzzzzzxxxxx8K_S0ttnIx4_
# ˇyySySyySySyGnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnSgZ1laSgSg
# SSx3…KySyySGSSG_S2ItLHPˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇMd7KLPlKSSFTc•OSgS
# 3…KySSiGSeySySSGiGSiySySSGiG3(KˇˇˇˇˇˇˇˇˇˇˇˇˇˇCwKySSiKySS
# 3…KySSiGSyySySSGiG3(KˇˇˇˇˇˇˇˇˇˇˇˇˇˇCwKySSiKySS
# SyySyySSySyyGSyySyyGGSGyGSyySySyySySSGGSGGˇˇˇˇˇS4S_SmˇˇAGmmmmmmmmmtLHPL(LHPTVdLHV
3…KySSyGSkySySSGiG3(KˇˇˇˇˇˇˇˇˇˇˇˇˇˇCwKySSiKySS
SSmSySyySGGSGyGSyySyySySSGGSGyS78iSLccSGSyySSySSGGccLcV1yVS~^§!zzzzzzzzzzzzhzzzzzSLzSEzzzzzzzzzzzzzzzzzxxxxx8K_S0ttnIx4_
ˇyySySyySySyGnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnSgZ1laSgSg
SSx3…KySyySGSSG_S2ItLHPˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇMd7KLPlKSSFTc•OSgS
3…KySSiGSeySySSGiGSiySySSGiG3(KˇˇˇˇˇˇˇˇˇˇˇˇˇˇCwKySSiKySS
3…KySSiGSyySySSGiG3(KˇˇˇˇˇˇˇˇˇˇˇˇˇˇCwKySSiKySS
SyySyySSySyyGSyySyyGGSGyGSyySySyySySSGGSGGˇˇˇˇˇS4S_SmˇˇAGmmmmmmmmmtLHPL(LHPTVdLHV

# rdar://63488139
1_SxSt_S4KSgS9OSgRSLAPALÂ
Expand Down

0 comments on commit 59cfe35

Please sign in to comment.