Skip to content

Commit 1847503

Browse files
committed
[libc++abi] Avoid raw calls to assert() in libc++abi
The runtimes now have a principled way of doing assertions in relation to hardening, so we should use that instead of raw calls to assert() inside libc++abi. This patch aims to maintain the behavior of the demangler code when it is used from within LLVM by introducing a simple DEMANGLE_ASSERT(...) macro that is then defined to the appropriate assertion mechanism.
1 parent 9824040 commit 1847503

File tree

8 files changed

+45
-36
lines changed

8 files changed

+45
-36
lines changed

libcxxabi/src/cxa_demangle.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
// file does not yet support:
1111
// - C++ modules TS
1212

13+
#include <__assert>
14+
#define DEMANGLE_ASSERT(expr, msg) _LIBCPP_ASSERT_UNCATEGORIZED(expr, msg)
15+
1316
#include "demangle/DemangleConfig.h"
1417
#include "demangle/ItaniumDemangle.h"
1518
#include "__cxxabi_config.h"
16-
#include <cassert>
1719
#include <cctype>
1820
#include <cstdio>
1921
#include <cstdlib>
@@ -395,7 +397,7 @@ __cxa_demangle(const char *MangledName, char *Buf, size_t *N, int *Status) {
395397
InternalStatus = demangle_invalid_mangled_name;
396398
else {
397399
OutputBuffer O(Buf, N);
398-
assert(Parser.ForwardTemplateRefs.empty());
400+
DEMANGLE_ASSERT(Parser.ForwardTemplateRefs.empty(), "");
399401
AST->print(O);
400402
O += '\0';
401403
if (N != nullptr)

libcxxabi/src/demangle/DemangleConfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@
9999
#define DEMANGLE_FALLTHROUGH
100100
#endif
101101

102+
#ifndef DEMANGLE_ASSERT
103+
#include <cassert>
104+
#define DEMANGLE_ASSERT(__expr, __msg) assert((__expr) && (__msg))
105+
#endif
106+
102107
#define DEMANGLE_NAMESPACE_BEGIN namespace { namespace itanium_demangle {
103108
#define DEMANGLE_NAMESPACE_END } }
104109

libcxxabi/src/demangle/ItaniumDemangle.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "Utility.h"
2222
#include <__cxxabi_config.h>
2323
#include <algorithm>
24-
#include <cassert>
2524
#include <cctype>
2625
#include <cstdio>
2726
#include <cstdlib>
@@ -129,12 +128,12 @@ template <class T, size_t N> class PODSmallVector {
129128

130129
// NOLINTNEXTLINE(readability-identifier-naming)
131130
void pop_back() {
132-
assert(Last != First && "Popping empty vector!");
131+
DEMANGLE_ASSERT(Last != First, "Popping empty vector!");
133132
--Last;
134133
}
135134

136135
void shrinkToSize(size_t Index) {
137-
assert(Index <= size() && "shrinkToSize() can't expand!");
136+
DEMANGLE_ASSERT(Index <= size(), "shrinkToSize() can't expand!");
138137
Last = First + Index;
139138
}
140139

@@ -144,11 +143,11 @@ template <class T, size_t N> class PODSmallVector {
144143
bool empty() const { return First == Last; }
145144
size_t size() const { return static_cast<size_t>(Last - First); }
146145
T &back() {
147-
assert(Last != First && "Calling back() on empty vector!");
146+
DEMANGLE_ASSERT(Last != First, "Calling back() on empty vector!");
148147
return *(Last - 1);
149148
}
150149
T &operator[](size_t Index) {
151-
assert(Index < size() && "Invalid access!");
150+
DEMANGLE_ASSERT(Index < size(), "Invalid access!");
152151
return *(begin() + Index);
153152
}
154153
void clear() { Last = First; }
@@ -1678,7 +1677,7 @@ class SpecialSubstitution final : public ExpandedSpecialSubstitution {
16781677
std::string_view SV = ExpandedSpecialSubstitution::getBaseName();
16791678
if (isInstantiation()) {
16801679
// The instantiations are typedefs that drop the "basic_" prefix.
1681-
assert(starts_with(SV, "basic_"));
1680+
DEMANGLE_ASSERT(starts_with(SV, "basic_"), "");
16821681
SV.remove_prefix(sizeof("basic_") - 1);
16831682
}
16841683
return SV;
@@ -2569,7 +2568,7 @@ void Node::visit(Fn F) const {
25692568
return F(static_cast<const X *>(this));
25702569
#include "ItaniumNodes.def"
25712570
}
2572-
assert(0 && "unknown mangling node kind");
2571+
DEMANGLE_ASSERT(0, "unknown mangling node kind");
25732572
}
25742573

25752574
/// Determine the kind of a node from its type.
@@ -2611,7 +2610,7 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
26112610
Parser->TemplateParams.push_back(&Params);
26122611
}
26132612
~ScopedTemplateParamList() {
2614-
assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2613+
DEMANGLE_ASSERT(Parser->TemplateParams.size() >= OldNumTemplateParamLists, "");
26152614
Parser->TemplateParams.shrinkToSize(OldNumTemplateParamLists);
26162615
}
26172616
TemplateParamList *params() { return &Params; }
@@ -2692,7 +2691,7 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
26922691
}
26932692

26942693
NodeArray popTrailingNodeArray(size_t FromPosition) {
2695-
assert(FromPosition <= Names.size());
2694+
DEMANGLE_ASSERT(FromPosition <= Names.size(), "");
26962695
NodeArray res =
26972696
makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
26982697
Names.shrinkToSize(FromPosition);
@@ -2859,7 +2858,7 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
28592858
std::string_view getSymbol() const {
28602859
std::string_view Res = Name;
28612860
if (Kind < Unnameable) {
2862-
assert(starts_with(Res, "operator") &&
2861+
DEMANGLE_ASSERT(starts_with(Res, "operator"),
28632862
"operator name does not start with 'operator'");
28642863
Res.remove_prefix(sizeof("operator") - 1);
28652864
if (starts_with(Res, ' '))
@@ -3694,7 +3693,7 @@ Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) {
36943693
}
36953694
}
36963695

3697-
assert(SoFar != nullptr);
3696+
DEMANGLE_ASSERT(SoFar != nullptr, "");
36983697

36993698
Node *Base = getDerived().parseBaseUnresolvedName();
37003699
if (Base == nullptr)
@@ -5635,7 +5634,7 @@ Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
56355634
Node *ForwardRef = make<ForwardTemplateReference>(Index);
56365635
if (!ForwardRef)
56375636
return nullptr;
5638-
assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5637+
DEMANGLE_ASSERT(ForwardRef->getKind() == Node::KForwardTemplateReference, "");
56395638
ForwardTemplateRefs.push_back(
56405639
static_cast<ForwardTemplateReference *>(ForwardRef));
56415640
return ForwardRef;

libcxxabi/src/demangle/Utility.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "DemangleConfig.h"
2020

2121
#include <array>
22-
#include <cassert>
2322
#include <cstdint>
2423
#include <cstdlib>
2524
#include <cstring>
@@ -159,7 +158,7 @@ class OutputBuffer {
159158
}
160159

161160
void insert(size_t Pos, const char *S, size_t N) {
162-
assert(Pos <= CurrentPosition);
161+
DEMANGLE_ASSERT(Pos <= CurrentPosition, "");
163162
if (N == 0)
164163
return;
165164
grow(N);
@@ -172,7 +171,7 @@ class OutputBuffer {
172171
void setCurrentPosition(size_t NewPos) { CurrentPosition = NewPos; }
173172

174173
char back() const {
175-
assert(CurrentPosition);
174+
DEMANGLE_ASSERT(CurrentPosition, "");
176175
return Buffer[CurrentPosition - 1];
177176
}
178177

libcxxabi/src/fallback_malloc.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#endif
1717

1818
#include <__memory/aligned_alloc.h>
19-
#include <assert.h>
19+
#include <__assert>
2020
#include <stdlib.h> // for malloc, calloc, free
2121
#include <string.h> // for memset
2222

@@ -142,7 +142,7 @@ void* fallback_malloc(size_t len) {
142142

143143
// Check the invariant that all heap_nodes pointers 'p' are aligned
144144
// so that 'p + 1' has an alignment of at least RequiredAlignment
145-
assert(reinterpret_cast<size_t>(p + 1) % RequiredAlignment == 0);
145+
_LIBCPP_ASSERT_UNCATEGORIZED(reinterpret_cast<size_t>(p + 1) % RequiredAlignment == 0, "");
146146

147147
// Calculate the number of extra padding elements needed in order
148148
// to split 'p' and create a properly aligned heap_node from the tail
@@ -163,7 +163,7 @@ void* fallback_malloc(size_t len) {
163163
q->next_node = 0;
164164
q->len = static_cast<heap_size>(aligned_nelems);
165165
void* ptr = q + 1;
166-
assert(reinterpret_cast<size_t>(ptr) % RequiredAlignment == 0);
166+
_LIBCPP_ASSERT_UNCATEGORIZED(reinterpret_cast<size_t>(ptr) % RequiredAlignment == 0, "");
167167
return ptr;
168168
}
169169

@@ -176,7 +176,7 @@ void* fallback_malloc(size_t len) {
176176
prev->next_node = p->next_node;
177177
p->next_node = 0;
178178
void* ptr = p + 1;
179-
assert(reinterpret_cast<size_t>(ptr) % RequiredAlignment == 0);
179+
_LIBCPP_ASSERT_UNCATEGORIZED(reinterpret_cast<size_t>(ptr) % RequiredAlignment == 0, "");
180180
return ptr;
181181
}
182182
}

llvm/include/llvm/Demangle/DemangleConfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
#define DEMANGLE_FALLTHROUGH
8787
#endif
8888

89+
#ifndef DEMANGLE_ASSERT
90+
#include <cassert>
91+
#define DEMANGLE_ASSERT(__expr, __msg) assert((__expr) && (__msg))
92+
#endif
93+
8994
#define DEMANGLE_NAMESPACE_BEGIN namespace llvm { namespace itanium_demangle {
9095
#define DEMANGLE_NAMESPACE_END } }
9196

llvm/include/llvm/Demangle/ItaniumDemangle.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "DemangleConfig.h"
2020
#include "StringViewExtras.h"
2121
#include "Utility.h"
22+
#include <__cxxabi_config.h>
2223
#include <algorithm>
23-
#include <cassert>
2424
#include <cctype>
2525
#include <cstdio>
2626
#include <cstdlib>
@@ -128,12 +128,12 @@ template <class T, size_t N> class PODSmallVector {
128128

129129
// NOLINTNEXTLINE(readability-identifier-naming)
130130
void pop_back() {
131-
assert(Last != First && "Popping empty vector!");
131+
DEMANGLE_ASSERT(Last != First, "Popping empty vector!");
132132
--Last;
133133
}
134134

135135
void shrinkToSize(size_t Index) {
136-
assert(Index <= size() && "shrinkToSize() can't expand!");
136+
DEMANGLE_ASSERT(Index <= size(), "shrinkToSize() can't expand!");
137137
Last = First + Index;
138138
}
139139

@@ -143,11 +143,11 @@ template <class T, size_t N> class PODSmallVector {
143143
bool empty() const { return First == Last; }
144144
size_t size() const { return static_cast<size_t>(Last - First); }
145145
T &back() {
146-
assert(Last != First && "Calling back() on empty vector!");
146+
DEMANGLE_ASSERT(Last != First, "Calling back() on empty vector!");
147147
return *(Last - 1);
148148
}
149149
T &operator[](size_t Index) {
150-
assert(Index < size() && "Invalid access!");
150+
DEMANGLE_ASSERT(Index < size(), "Invalid access!");
151151
return *(begin() + Index);
152152
}
153153
void clear() { Last = First; }
@@ -1677,7 +1677,7 @@ class SpecialSubstitution final : public ExpandedSpecialSubstitution {
16771677
std::string_view SV = ExpandedSpecialSubstitution::getBaseName();
16781678
if (isInstantiation()) {
16791679
// The instantiations are typedefs that drop the "basic_" prefix.
1680-
assert(starts_with(SV, "basic_"));
1680+
DEMANGLE_ASSERT(starts_with(SV, "basic_"), "");
16811681
SV.remove_prefix(sizeof("basic_") - 1);
16821682
}
16831683
return SV;
@@ -2568,7 +2568,7 @@ void Node::visit(Fn F) const {
25682568
return F(static_cast<const X *>(this));
25692569
#include "ItaniumNodes.def"
25702570
}
2571-
assert(0 && "unknown mangling node kind");
2571+
DEMANGLE_ASSERT(0, "unknown mangling node kind");
25722572
}
25732573

25742574
/// Determine the kind of a node from its type.
@@ -2610,7 +2610,7 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
26102610
Parser->TemplateParams.push_back(&Params);
26112611
}
26122612
~ScopedTemplateParamList() {
2613-
assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2613+
DEMANGLE_ASSERT(Parser->TemplateParams.size() >= OldNumTemplateParamLists, "");
26142614
Parser->TemplateParams.shrinkToSize(OldNumTemplateParamLists);
26152615
}
26162616
TemplateParamList *params() { return &Params; }
@@ -2691,7 +2691,7 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
26912691
}
26922692

26932693
NodeArray popTrailingNodeArray(size_t FromPosition) {
2694-
assert(FromPosition <= Names.size());
2694+
DEMANGLE_ASSERT(FromPosition <= Names.size(), "");
26952695
NodeArray res =
26962696
makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
26972697
Names.shrinkToSize(FromPosition);
@@ -2858,7 +2858,7 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
28582858
std::string_view getSymbol() const {
28592859
std::string_view Res = Name;
28602860
if (Kind < Unnameable) {
2861-
assert(starts_with(Res, "operator") &&
2861+
DEMANGLE_ASSERT(starts_with(Res, "operator"),
28622862
"operator name does not start with 'operator'");
28632863
Res.remove_prefix(sizeof("operator") - 1);
28642864
if (starts_with(Res, ' '))
@@ -3693,7 +3693,7 @@ Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) {
36933693
}
36943694
}
36953695

3696-
assert(SoFar != nullptr);
3696+
DEMANGLE_ASSERT(SoFar != nullptr, "");
36973697

36983698
Node *Base = getDerived().parseBaseUnresolvedName();
36993699
if (Base == nullptr)
@@ -5634,7 +5634,7 @@ Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
56345634
Node *ForwardRef = make<ForwardTemplateReference>(Index);
56355635
if (!ForwardRef)
56365636
return nullptr;
5637-
assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5637+
DEMANGLE_ASSERT(ForwardRef->getKind() == Node::KForwardTemplateReference, "");
56385638
ForwardTemplateRefs.push_back(
56395639
static_cast<ForwardTemplateReference *>(ForwardRef));
56405640
return ForwardRef;

llvm/include/llvm/Demangle/Utility.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "DemangleConfig.h"
2020

2121
#include <array>
22-
#include <cassert>
2322
#include <cstdint>
2423
#include <cstdlib>
2524
#include <cstring>
@@ -159,7 +158,7 @@ class OutputBuffer {
159158
}
160159

161160
void insert(size_t Pos, const char *S, size_t N) {
162-
assert(Pos <= CurrentPosition);
161+
DEMANGLE_ASSERT(Pos <= CurrentPosition, "");
163162
if (N == 0)
164163
return;
165164
grow(N);
@@ -172,7 +171,7 @@ class OutputBuffer {
172171
void setCurrentPosition(size_t NewPos) { CurrentPosition = NewPos; }
173172

174173
char back() const {
175-
assert(CurrentPosition);
174+
DEMANGLE_ASSERT(CurrentPosition, "");
176175
return Buffer[CurrentPosition - 1];
177176
}
178177

0 commit comments

Comments
 (0)