2019-03-15 21:01:26 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
// This header allows for the use of soft-assertions. It is included before
|
|
|
|
// the standard <assert.h> and these declarations will take precedence.
|
|
|
|
//
|
|
|
|
// These declarations exist because the system declarations may have
|
|
|
|
// __attribute__((noreturn)) and our only modification here is to remove
|
|
|
|
// that. Alternative definitions to the standard library also exist in
|
|
|
|
// ircd/assert.cc
|
|
|
|
|
2019-05-29 00:53:25 +02:00
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_ASSERT_H
|
|
|
|
|
2019-08-12 04:02:50 +02:00
|
|
|
/// This file has to be included prior to the standard library assert headers
|
2019-07-09 01:49:43 +02:00
|
|
|
#if defined(RB_ASSERT) && defined(_ASSERT_H_DECLS) && defined(RB_DEBUG)
|
2019-05-29 00:53:25 +02:00
|
|
|
#error "Do not include <assert.h> or <cassert> first."
|
|
|
|
#endif
|
|
|
|
|
2019-08-12 04:02:50 +02:00
|
|
|
/// Define an indicator for whether we override standard assert() behavior in
|
|
|
|
/// this build if the conditions are appropriate.
|
2019-03-15 21:01:26 +01:00
|
|
|
#if defined(RB_ASSERT) && !defined(NDEBUG)
|
2019-08-12 04:02:50 +02:00
|
|
|
#define IRCD_ASSERT_OVERRIDE
|
|
|
|
#define _ASSERT_H_DECLS
|
|
|
|
#endif
|
2019-03-15 21:01:26 +01:00
|
|
|
|
2019-08-12 04:02:50 +02:00
|
|
|
// Our utils
|
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
void debugtrap() noexcept;
|
2019-12-26 23:38:51 +01:00
|
|
|
void always_assert(const bool &) noexcept;
|
|
|
|
void print_assertion(const char *const &, const char *const &, const unsigned &, const char *const &) noexcept;
|
2019-08-12 04:02:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Override the standard assert behavior to take one of several different
|
|
|
|
/// actions as defined in our internal assert.cc unit. When trapping assert
|
|
|
|
/// is disabled this path will be used instead.
|
|
|
|
///
|
|
|
|
#if defined(IRCD_ASSERT_OVERRIDE) && !defined(RB_ASSERT_INTRINSIC)
|
2019-03-15 21:01:26 +01:00
|
|
|
extern "C" void
|
2019-05-15 00:11:29 +02:00
|
|
|
__attribute__((visibility("default")))
|
2019-08-12 04:02:50 +02:00
|
|
|
__assert_fail(const char *__assertion,
|
|
|
|
const char *__file,
|
|
|
|
unsigned int __line,
|
|
|
|
const char *__function);
|
2019-03-15 21:01:26 +01:00
|
|
|
#endif
|
2019-08-09 01:12:52 +02:00
|
|
|
|
2020-02-02 02:20:50 +01:00
|
|
|
/// Override the standard assert behavior, if enabled, to trap into the
|
|
|
|
/// debugger as close as possible to the offending site.
|
|
|
|
///
|
|
|
|
#if defined(IRCD_ASSERT_OVERRIDE) && defined(RB_ASSERT_INTRINSIC)
|
|
|
|
extern "C" // for clang
|
|
|
|
{
|
|
|
|
extern inline void
|
|
|
|
__attribute__((flatten, always_inline, gnu_inline, artificial))
|
|
|
|
__assert_fail(const char *__assertion,
|
|
|
|
const char *__file,
|
|
|
|
unsigned int __line,
|
|
|
|
const char *__function)
|
|
|
|
{
|
|
|
|
ircd::print_assertion(__assertion, __file, __line, __function);
|
|
|
|
ircd::debugtrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-12 04:02:50 +02:00
|
|
|
/// Intrinsic to halt execution for examination by a tracing debugger without
|
|
|
|
/// aborting the program.
|
|
|
|
///
|
2019-08-09 01:12:52 +02:00
|
|
|
extern inline void
|
|
|
|
__attribute__((always_inline, gnu_inline, artificial))
|
|
|
|
ircd::debugtrap()
|
2019-08-12 04:02:50 +02:00
|
|
|
noexcept
|
2019-08-09 01:12:52 +02:00
|
|
|
{
|
|
|
|
#if defined(__clang__)
|
|
|
|
static_assert(__has_builtin(__builtin_debugtrap));
|
|
|
|
__builtin_debugtrap();
|
|
|
|
#elif defined(__x86_64__)
|
2019-09-03 04:58:04 +02:00
|
|
|
__asm__ volatile ("int $3 # IRCd ASSERTION DEBUG TRAP !!! ");
|
2019-08-09 01:12:52 +02:00
|
|
|
#else
|
|
|
|
__builtin_trap();
|
|
|
|
#endif
|
|
|
|
}
|
2019-12-26 23:38:51 +01:00
|
|
|
|
|
|
|
/// Trap on false expression whether or not NDEBUG.
|
|
|
|
extern inline void
|
|
|
|
__attribute__((always_inline, gnu_inline, artificial))
|
|
|
|
ircd::always_assert(const bool &expr)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(__builtin_expect(!expr, 0))
|
|
|
|
debugtrap();
|
|
|
|
}
|