Skip to content

[libc++abi] Avoid raw calls to assert() in libc++abi #71121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions libcxxabi/src/abort_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
extern "C" _LIBCXXABI_HIDDEN _LIBCXXABI_NORETURN void
abort_message(const char *format, ...) __attribute__((format(printf, 1, 2)));

#ifndef _LIBCXXABI_ASSERT
# define _LIBCXXABI_ASSERT(expr, msg) \
do { \
if (!(expr)) { \
char const* __msg = (msg); \
::abort_message("%s:%d: %s", __FILE__, __LINE__, __msg); \
} \
} while (false)

#endif

#endif // __ABORT_MESSAGE_H_
6 changes: 4 additions & 2 deletions libcxxabi/src/cxa_demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
// file does not yet support:
// - C++ modules TS

#include "abort_message.h"
#define DEMANGLE_ASSERT(expr, msg) _LIBCXXABI_ASSERT(expr, msg)

#include "demangle/DemangleConfig.h"
#include "demangle/ItaniumDemangle.h"
#include "__cxxabi_config.h"
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
Expand Down Expand Up @@ -395,7 +397,7 @@ __cxa_demangle(const char *MangledName, char *Buf, size_t *N, int *Status) {
InternalStatus = demangle_invalid_mangled_name;
else {
OutputBuffer O(Buf, N);
assert(Parser.ForwardTemplateRefs.empty());
DEMANGLE_ASSERT(Parser.ForwardTemplateRefs.empty(), "");
AST->print(O);
O += '\0';
if (N != nullptr)
Expand Down
5 changes: 5 additions & 0 deletions libcxxabi/src/demangle/DemangleConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
#define DEMANGLE_FALLTHROUGH
#endif

#ifndef DEMANGLE_ASSERT
#include <cassert>
#define DEMANGLE_ASSERT(__expr, __msg) assert((__expr) && (__msg))
#endif

#define DEMANGLE_NAMESPACE_BEGIN namespace { namespace itanium_demangle {
#define DEMANGLE_NAMESPACE_END } }

Expand Down
27 changes: 14 additions & 13 deletions libcxxabi/src/demangle/ItaniumDemangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "Utility.h"
#include <__cxxabi_config.h>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
Expand Down Expand Up @@ -129,12 +128,12 @@ template <class T, size_t N> class PODSmallVector {

// NOLINTNEXTLINE(readability-identifier-naming)
void pop_back() {
assert(Last != First && "Popping empty vector!");
DEMANGLE_ASSERT(Last != First, "Popping empty vector!");
--Last;
}

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

Expand All @@ -144,11 +143,11 @@ template <class T, size_t N> class PODSmallVector {
bool empty() const { return First == Last; }
size_t size() const { return static_cast<size_t>(Last - First); }
T &back() {
assert(Last != First && "Calling back() on empty vector!");
DEMANGLE_ASSERT(Last != First, "Calling back() on empty vector!");
return *(Last - 1);
}
T &operator[](size_t Index) {
assert(Index < size() && "Invalid access!");
DEMANGLE_ASSERT(Index < size(), "Invalid access!");
return *(begin() + Index);
}
void clear() { Last = First; }
Expand Down Expand Up @@ -1678,7 +1677,7 @@ class SpecialSubstitution final : public ExpandedSpecialSubstitution {
std::string_view SV = ExpandedSpecialSubstitution::getBaseName();
if (isInstantiation()) {
// The instantiations are typedefs that drop the "basic_" prefix.
assert(starts_with(SV, "basic_"));
DEMANGLE_ASSERT(starts_with(SV, "basic_"), "");
SV.remove_prefix(sizeof("basic_") - 1);
}
return SV;
Expand Down Expand Up @@ -2569,7 +2568,7 @@ void Node::visit(Fn F) const {
return F(static_cast<const X *>(this));
#include "ItaniumNodes.def"
}
assert(0 && "unknown mangling node kind");
DEMANGLE_ASSERT(0, "unknown mangling node kind");
}

/// Determine the kind of a node from its type.
Expand Down Expand Up @@ -2611,7 +2610,8 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
Parser->TemplateParams.push_back(&Params);
}
~ScopedTemplateParamList() {
assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
DEMANGLE_ASSERT(Parser->TemplateParams.size() >= OldNumTemplateParamLists,
"");
Parser->TemplateParams.shrinkToSize(OldNumTemplateParamLists);
}
TemplateParamList *params() { return &Params; }
Expand Down Expand Up @@ -2692,7 +2692,7 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
}

NodeArray popTrailingNodeArray(size_t FromPosition) {
assert(FromPosition <= Names.size());
DEMANGLE_ASSERT(FromPosition <= Names.size(), "");
NodeArray res =
makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
Names.shrinkToSize(FromPosition);
Expand Down Expand Up @@ -2859,8 +2859,8 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
std::string_view getSymbol() const {
std::string_view Res = Name;
if (Kind < Unnameable) {
assert(starts_with(Res, "operator") &&
"operator name does not start with 'operator'");
DEMANGLE_ASSERT(starts_with(Res, "operator"),
"operator name does not start with 'operator'");
Res.remove_prefix(sizeof("operator") - 1);
if (starts_with(Res, ' '))
Res.remove_prefix(1);
Expand Down Expand Up @@ -3694,7 +3694,7 @@ Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) {
}
}

assert(SoFar != nullptr);
DEMANGLE_ASSERT(SoFar != nullptr, "");

Node *Base = getDerived().parseBaseUnresolvedName();
if (Base == nullptr)
Expand Down Expand Up @@ -5635,7 +5635,8 @@ Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Node *ForwardRef = make<ForwardTemplateReference>(Index);
if (!ForwardRef)
return nullptr;
assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
DEMANGLE_ASSERT(ForwardRef->getKind() == Node::KForwardTemplateReference,
"");
ForwardTemplateRefs.push_back(
static_cast<ForwardTemplateReference *>(ForwardRef));
return ForwardRef;
Expand Down
5 changes: 2 additions & 3 deletions libcxxabi/src/demangle/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "DemangleConfig.h"

#include <array>
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -159,7 +158,7 @@ class OutputBuffer {
}

void insert(size_t Pos, const char *S, size_t N) {
assert(Pos <= CurrentPosition);
DEMANGLE_ASSERT(Pos <= CurrentPosition, "");
if (N == 0)
return;
grow(N);
Expand All @@ -172,7 +171,7 @@ class OutputBuffer {
void setCurrentPosition(size_t NewPos) { CurrentPosition = NewPos; }

char back() const {
assert(CurrentPosition);
DEMANGLE_ASSERT(CurrentPosition, "");
return Buffer[CurrentPosition - 1];
}

Expand Down
9 changes: 5 additions & 4 deletions libcxxabi/src/fallback_malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "fallback_malloc.h"
#include "abort_message.h"

#include <__threading_support>
#ifndef _LIBCXXABI_HAS_NO_THREADS
Expand All @@ -16,7 +17,7 @@
#endif

#include <__memory/aligned_alloc.h>
#include <assert.h>
#include <__assert>
#include <stdlib.h> // for malloc, calloc, free
#include <string.h> // for memset

Expand Down Expand Up @@ -142,7 +143,7 @@ void* fallback_malloc(size_t len) {

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

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

Expand All @@ -176,7 +177,7 @@ void* fallback_malloc(size_t len) {
prev->next_node = p->next_node;
p->next_node = 0;
void* ptr = p + 1;
assert(reinterpret_cast<size_t>(ptr) % RequiredAlignment == 0);
_LIBCXXABI_ASSERT(reinterpret_cast<size_t>(ptr) % RequiredAlignment == 0, "");
return ptr;
}
}
Expand Down
2 changes: 2 additions & 0 deletions libcxxabi/test/test_fallback_malloc.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ typedef std::deque<void *> container;

TEST_DIAGNOSTIC_PUSH
TEST_CLANG_DIAGNOSTIC_IGNORED("-Wprivate-header")
#define _LIBCXXABI_ASSERT(expr, msg) assert((expr) && (msg))

// #define DEBUG_FALLBACK_MALLOC
#define INSTRUMENT_FALLBACK_MALLOC
#include "../src/fallback_malloc.cpp"
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/Demangle/DemangleConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
#define DEMANGLE_FALLTHROUGH
#endif

#ifndef DEMANGLE_ASSERT
#include <cassert>
#define DEMANGLE_ASSERT(__expr, __msg) assert((__expr) && (__msg))
#endif

#define DEMANGLE_NAMESPACE_BEGIN namespace llvm { namespace itanium_demangle {
#define DEMANGLE_NAMESPACE_END } }

Expand Down
27 changes: 14 additions & 13 deletions llvm/include/llvm/Demangle/ItaniumDemangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "StringViewExtras.h"
#include "Utility.h"
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
Expand Down Expand Up @@ -128,12 +127,12 @@ template <class T, size_t N> class PODSmallVector {

// NOLINTNEXTLINE(readability-identifier-naming)
void pop_back() {
assert(Last != First && "Popping empty vector!");
DEMANGLE_ASSERT(Last != First, "Popping empty vector!");
--Last;
}

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

Expand All @@ -143,11 +142,11 @@ template <class T, size_t N> class PODSmallVector {
bool empty() const { return First == Last; }
size_t size() const { return static_cast<size_t>(Last - First); }
T &back() {
assert(Last != First && "Calling back() on empty vector!");
DEMANGLE_ASSERT(Last != First, "Calling back() on empty vector!");
return *(Last - 1);
}
T &operator[](size_t Index) {
assert(Index < size() && "Invalid access!");
DEMANGLE_ASSERT(Index < size(), "Invalid access!");
return *(begin() + Index);
}
void clear() { Last = First; }
Expand Down Expand Up @@ -1677,7 +1676,7 @@ class SpecialSubstitution final : public ExpandedSpecialSubstitution {
std::string_view SV = ExpandedSpecialSubstitution::getBaseName();
if (isInstantiation()) {
// The instantiations are typedefs that drop the "basic_" prefix.
assert(starts_with(SV, "basic_"));
DEMANGLE_ASSERT(starts_with(SV, "basic_"), "");
SV.remove_prefix(sizeof("basic_") - 1);
}
return SV;
Expand Down Expand Up @@ -2568,7 +2567,7 @@ void Node::visit(Fn F) const {
return F(static_cast<const X *>(this));
#include "ItaniumNodes.def"
}
assert(0 && "unknown mangling node kind");
DEMANGLE_ASSERT(0, "unknown mangling node kind");
}

/// Determine the kind of a node from its type.
Expand Down Expand Up @@ -2610,7 +2609,8 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
Parser->TemplateParams.push_back(&Params);
}
~ScopedTemplateParamList() {
assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
DEMANGLE_ASSERT(Parser->TemplateParams.size() >= OldNumTemplateParamLists,
"");
Parser->TemplateParams.shrinkToSize(OldNumTemplateParamLists);
}
TemplateParamList *params() { return &Params; }
Expand Down Expand Up @@ -2691,7 +2691,7 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
}

NodeArray popTrailingNodeArray(size_t FromPosition) {
assert(FromPosition <= Names.size());
DEMANGLE_ASSERT(FromPosition <= Names.size(), "");
NodeArray res =
makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
Names.shrinkToSize(FromPosition);
Expand Down Expand Up @@ -2858,8 +2858,8 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
std::string_view getSymbol() const {
std::string_view Res = Name;
if (Kind < Unnameable) {
assert(starts_with(Res, "operator") &&
"operator name does not start with 'operator'");
DEMANGLE_ASSERT(starts_with(Res, "operator"),
"operator name does not start with 'operator'");
Res.remove_prefix(sizeof("operator") - 1);
if (starts_with(Res, ' '))
Res.remove_prefix(1);
Expand Down Expand Up @@ -3693,7 +3693,7 @@ Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) {
}
}

assert(SoFar != nullptr);
DEMANGLE_ASSERT(SoFar != nullptr, "");

Node *Base = getDerived().parseBaseUnresolvedName();
if (Base == nullptr)
Expand Down Expand Up @@ -5634,7 +5634,8 @@ Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Node *ForwardRef = make<ForwardTemplateReference>(Index);
if (!ForwardRef)
return nullptr;
assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
DEMANGLE_ASSERT(ForwardRef->getKind() == Node::KForwardTemplateReference,
"");
ForwardTemplateRefs.push_back(
static_cast<ForwardTemplateReference *>(ForwardRef));
return ForwardRef;
Expand Down
5 changes: 2 additions & 3 deletions llvm/include/llvm/Demangle/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "DemangleConfig.h"

#include <array>
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -159,7 +158,7 @@ class OutputBuffer {
}

void insert(size_t Pos, const char *S, size_t N) {
assert(Pos <= CurrentPosition);
DEMANGLE_ASSERT(Pos <= CurrentPosition, "");
if (N == 0)
return;
grow(N);
Expand All @@ -172,7 +171,7 @@ class OutputBuffer {
void setCurrentPosition(size_t NewPos) { CurrentPosition = NewPos; }

char back() const {
assert(CurrentPosition);
DEMANGLE_ASSERT(CurrentPosition, "");
return Buffer[CurrentPosition - 1];
}

Expand Down