ams: revamp assertion system

This commit is contained in:
Michael Scire
2020-02-22 23:05:14 -08:00
parent 9572fb2ce3
commit 40400aee1f
168 changed files with 1014 additions and 696 deletions

View File

@@ -15,8 +15,8 @@
*/
#pragma once
#include <vapours/includes.hpp>
#include <vapours/defines.hpp>
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/literals.hpp>
#include <vapours/timespan.hpp>

View File

@@ -0,0 +1,76 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours/common.hpp>
namespace ams::impl {
template<typename... ArgTypes>
ALWAYS_INLINE void UnusedImpl(ArgTypes... args) {
(static_cast<void>(args), ...);
}
}
namespace ams::diag {
NORETURN NOINLINE void AssertionFailureImpl(const char *file, int line, const char *func, const char *expr, u64 value, const char *format, ...) __attribute__((format(printf, 6, 7)));
NORETURN NOINLINE void AssertionFailureImpl(const char *file, int line, const char *func, const char *expr, u64 value);
NORETURN NOINLINE void AbortImpl(const char *file, int line, const char *func, const char *expr, u64 value, const char *format, ...) __attribute__((format(printf, 6, 7)));
NORETURN NOINLINE void AbortImpl(const char *file, int line, const char *func, const char *expr, u64 value);
}
#define AMS_UNUSED(...) ::ams::impl::UnusedImpl(__VA_ARGS__)
#ifdef AMS_ENABLE_DEBUG_PRINT
#define AMS_CALL_ASSERT_FAIL_IMPL(cond, ...) ::ams::diag::AssertionFailureImpl(__FILE__, __LINE__, __PRETTY_FUNCTION__, cond, 0, ## __VA_ARGS__)
#define AMS_CALL_ABORT_IMPL(cond, ...) ::ams::diag::AbortImpl(__FILE__, __LINE__, __PRETTY_FUNCTION__, cond, 0, ## __VA_ARGS__)
#else
#define AMS_CALL_ASSERT_FAIL_IMPL(cond, ...) ::ams::diag::AssertionFailureImpl("", 0, "", "", 0)
#define AMS_CALL_ABORT_IMPL(cond, ...) ::ams::diag::AbortImpl("", 0, "", "", 0)
#endif
#ifdef AMS_ENABLE_ASSERTIONS
#define AMS_ASSERT_IMPL(expr, ...) \
({ \
if (const bool __tmp_ams_assert_val = (expr); AMS_UNLIKELY(!__tmp_ams_assert_val)) { \
AMS_CALL_ASSERT_FAIL_IMPL(#expr, ## __VA_ARGS__); \
} \
})
#else
#define AMS_ASSERT_IMPL(expr, ...) AMS_UNUSED(expr, ## __VA_ARGS__)
#endif
#define AMS_ASSERT(expr, ...) AMS_ASSERT_IMPL(expr, ## __VA_ARGS__)
#define AMS_UNREACHABLE_DEFAULT_CASE() default: AMS_CALL_ABORT_IMPL("Unreachable default case entered")
#ifdef AMS_BUILD_FOR_AUDITING
#define AMS_AUDIT(expr, ...) AMS_ASSERT(expr, ## __VA_ARGS__)
#else
#define AMS_AUDIT(expr, ...) AMS_UNUSED(expr, ## __VA_ARGS__)
#endif
#define AMS_ABORT(...) AMS_CALL_ABORT_IMPL("", ## __VA_ARGS__)
#define AMS_ABORT_UNLESS(expr, ...) \
({ \
if (const bool __tmp_ams_assert_val = (expr); AMS_UNLIKELY(!__tmp_ams_assert_val)) { \
AMS_CALL_ABORT_IMPL(#expr, ##__VA_ARGS__); \
} \
})

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours/includes.hpp>
#include <vapours/defines.hpp>
#if 0
#define AMS_BUILD_FOR_AUDITING
#endif
#ifdef AMS_BUILD_FOR_AUDITING
#define AMS_BUILD_FOR_DEBUGGING
#endif
#ifdef AMS_BUILD_FOR_DEBUGGING
#define AMS_ENABLE_ASSERTIONS
#define AMS_ENABLE_DEBUG_PRINT
#endif

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include <vapours/defines.hpp>
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util.hpp>
#ifdef ATMOSPHERE_ARCH_ARM64
@@ -30,7 +31,7 @@
namespace ams::crypto {
bool IsSameBytes(const void *lhs, const void *rhs, size_t size) {
inline bool IsSameBytes(const void *lhs, const void *rhs, size_t size) {
return impl::IsSameBytes(lhs, rhs, size);
}

View File

@@ -20,7 +20,7 @@
namespace ams::crypto::impl {
bool IsSameBytes(const void *lhs, const void *rhs, size_t size) {
inline bool IsSameBytes(const void *lhs, const void *rhs, size_t size) {
bool result;
u8 xor_acc, ltmp, rtmp;
size_t index;

View File

@@ -18,10 +18,6 @@
/* Any broadly useful language defines should go here. */
#define AMS_ASSERT(expr) do { if (!(expr)) { std::abort(); } } while (0)
#define AMS_UNREACHABLE_DEFAULT_CASE() default: std::abort()
#define NON_COPYABLE(cls) \
cls(const cls&) = delete; \
cls& operator=(const cls&) = delete

View File

@@ -14,7 +14,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours/defines.hpp>
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams { inline namespace literals {

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include <vapours/defines.hpp>
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util.hpp>
/* Utilities. */

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include <vapours/defines.hpp>
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams {
@@ -31,7 +32,7 @@ namespace ams {
static constexpr BaseType ReservedBits = 10;
static_assert(ModuleBits + DescriptionBits + ReservedBits == sizeof(BaseType) * CHAR_BIT, "ModuleBits + DescriptionBits + ReservedBits == sizeof(BaseType) * CHAR_BIT");
public:
NX_CONSTEXPR BaseType MakeValue(BaseType module, BaseType description) {
static constexpr ALWAYS_INLINE BaseType MakeValue(BaseType module, BaseType description) {
return (module) | (description << ModuleBits);
}
@@ -41,11 +42,11 @@ namespace ams {
static_assert(description < (1 << DescriptionBits), "Invalid Description");
};
NX_CONSTEXPR BaseType GetModuleFromValue(BaseType value) {
static constexpr ALWAYS_INLINE BaseType GetModuleFromValue(BaseType value) {
return value & ~(~BaseType() << ModuleBits);
}
NX_CONSTEXPR BaseType GetDescriptionFromValue(BaseType value) {
static constexpr ALWAYS_INLINE BaseType GetDescriptionFromValue(BaseType value) {
return ((value >> ModuleBits) & ~(~BaseType() << DescriptionBits));
}
};
@@ -126,13 +127,16 @@ namespace ams {
namespace result::impl {
NORETURN void OnResultAssertion(Result result);
NORETURN NOINLINE void OnResultAssertion(const char *file, int line, const char *func, const char *expr, Result result);
NORETURN NOINLINE void OnResultAssertion(Result result);
NORETURN NOINLINE void OnResultAbort(const char *file, int line, const char *func, const char *expr, Result result);
NORETURN NOINLINE void OnResultAbort(Result result);
}
constexpr ALWAYS_INLINE Result::operator ResultSuccess() const {
if (!ResultSuccess::CanAccept(*this)) {
result::impl::OnResultAssertion(*this);
result::impl::OnResultAbort(*this);
}
return ResultSuccess();
}
@@ -149,7 +153,7 @@ namespace ams {
static_assert(Value != Base::SuccessValue, "Value != Base::SuccessValue");
public:
constexpr operator Result() const { return MakeResult(Value); }
constexpr operator ResultSuccess() const { OnResultAssertion(Value); }
constexpr operator ResultSuccess() const { OnResultAbort(Value); }
constexpr ALWAYS_INLINE bool IsSuccess() const { return false; }
constexpr ALWAYS_INLINE bool IsFailure() const { return !this->IsSuccess(); }
@@ -216,18 +220,39 @@ namespace ams {
/// Evaluates an expression that returns a result, and returns the result if it would fail.
#define R_TRY(res_expr) \
({ \
const auto _tmp_r_try_rc = res_expr; \
const auto _tmp_r_try_rc = (res_expr); \
if (R_FAILED(_tmp_r_try_rc)) { \
return _tmp_r_try_rc; \
} \
})
/// Evaluates an expression that returns a result, and fatals the result if it would fail.
#ifdef AMS_ENABLE_DEBUG_PRINT
#define AMS_CALL_ON_RESULT_ASSERTION_IMPL(cond, val) ::ams::result::impl::OnResultAssertion(__FILE__, __LINE__, __PRETTY_FUNCTION__, cond, val)
#define AMS_CALL_ON_RESULT_ABORT_IMPL(cond, val) ::ams::result::impl::OnResultAbort(__FILE__, __LINE__, __PRETTY_FUNCTION__, cond, val)
#else
#define AMS_CALL_ON_RESULT_ASSERTION_IMPL(cond, val) ::ams::result::impl::OnResultAssertion("", 0, "", "", val)
#define AMS_CALL_ON_RESULT_ABORT_IMPL(cond, val) ::ams::result::impl::OnResultAbort("", 0, "", "", val)
#endif
/// Evaluates an expression that returns a result, and asserts the result if it would fail.
#ifdef AMS_ENABLE_ASSERTIONS
#define R_ASSERT(res_expr) \
({ \
const auto _tmp_r_assert_rc = res_expr; \
if (R_FAILED(_tmp_r_assert_rc)) { \
::ams::result::impl::OnResultAssertion(_tmp_r_assert_rc); \
const auto _tmp_r_assert_rc = (res_expr); \
if (AMS_UNLIKELY(R_FAILED(_tmp_r_assert_rc))) { \
AMS_CALL_ON_RESULT_ASSERTION_IMPL(#res_expr, _tmp_r_assert_rc); \
} \
})
#else
#define R_ASSERT(res_expr) AMS_UNUSED((res_expr));
#endif
/// Evaluates an expression that returns a result, and aborts if the result would fail.
#define R_ABORT_UNLESS(res_expr) \
({ \
const auto _tmp_r_abort_rc = (res_expr); \
if (AMS_UNLIKELY(R_FAILED(_tmp_r_abort_rc))) { \
AMS_CALL_ON_RESULT_ABORT_IMPL(#res_expr, _tmp_r_abort_rc); \
} \
})
@@ -244,14 +269,14 @@ namespace ams {
#define R_TRY_CATCH(res_expr) \
({ \
const auto R_CURRENT_RESULT = res_expr; \
const auto R_CURRENT_RESULT = (res_expr); \
if (R_FAILED(R_CURRENT_RESULT)) { \
if (false)
namespace ams::result::impl {
template<typename... Rs>
NX_CONSTEXPR bool AnyIncludes(Result result) {
constexpr ALWAYS_INLINE bool AnyIncludes(Result result) {
return (Rs::Includes(result) || ...);
}
@@ -287,3 +312,11 @@ namespace ams::result::impl {
} \
} \
})
#define R_END_TRY_CATCH_WITH_ABORT_UNLESS \
else { \
R_ABORT_UNLESS(R_CURRENT_RESULT); \
} \
} \
})

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include <vapours/defines.hpp>
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/results.hpp>
#include <vapours/svc/svc_types.hpp>

View File

@@ -14,6 +14,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/svc/svc_types.hpp>
namespace ams::svc::codegen::impl {

View File

@@ -15,6 +15,8 @@
*/
#pragma once
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/results.hpp>
namespace ams::svc {

View File

@@ -15,8 +15,8 @@
*/
#pragma once
#include "svc_common.hpp"
#include "svc_select_hardware_constants.hpp"
#include <vapours/svc/svc_common.hpp>
#include <vapours/svc/svc_select_hardware_constants.hpp>
namespace ams::svc {

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include <vapours/defines.hpp>
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <chrono>
namespace ams {

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include <vapours/defines.hpp>
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util/util_alignment.hpp>
#include <vapours/util/util_size.hpp>

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include "../defines.hpp"
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include "../defines.hpp"
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include "../defines.hpp"
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include "../defines.hpp"
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include "../defines.hpp"
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {

View File

@@ -15,7 +15,9 @@
*/
#pragma once
#include "util_parent_of_member.hpp"
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util/util_parent_of_member.hpp>
namespace ams::util {

View File

@@ -16,7 +16,9 @@
#pragma once
#include <freebsd/sys/tree.h>
#include "util_parent_of_member.hpp"
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util/util_parent_of_member.hpp>
namespace ams::util {

View File

@@ -15,8 +15,9 @@
*/
#pragma once
#include "../defines.hpp"
#include "util_typed_storage.hpp"
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util/util_typed_storage.hpp>
namespace ams::util {

View File

@@ -16,7 +16,8 @@
/* Scope guard logic lovingly taken from Andrei Alexandrescu's "Systemic Error Handling in C++" */
#pragma once
#include "../defines.hpp"
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {
@@ -54,5 +55,5 @@ namespace ams::util {
}
#define SCOPE_GUARD ::ams::util::impl::ScopeGuardOnExit() + [&]() ALWAYS_INLINE_LAMBDA
#define SCOPE_GUARD ::ams::util::impl::ScopeGuardOnExit() + [&]() ALWAYS_INLINE_LAMBDA
#define ON_SCOPE_EXIT auto ANONYMOUS_VARIABLE(SCOPE_EXIT_STATE_) = SCOPE_GUARD

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include "../defines.hpp"
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include "../defines.hpp"
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include <vapours.hpp>
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {

View File

@@ -15,7 +15,8 @@
*/
#pragma once
#include "../defines.hpp"
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {