[libcxx-commits] [libcxx] [libcxxabi] [libc++][hardening] Implement support for assertion semantics. (PR #148172)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jul 11 10:02:30 PDT 2025
================
@@ -0,0 +1,45 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___LOG_HARDENING_FAILURE
+#define _LIBCPP___LOG_HARDENING_FAILURE
+
+#include <__config>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+// This function should never be called directly from the code -- it should only be called through the
+// `_LIBCPP_LOG_HARDENING_FAILURE` macro.
+_LIBCPP_AVAILABILITY_LOG_HARDENING_FAILURE _LIBCPP_OVERRIDABLE_FUNC_VIS void
+__libcpp_log_hardening_failure(const char* message) _NOEXCEPT;
+
+// _LIBCPP_LOG_HARDENING_FAILURE(message)
+//
+// This macro is used to log a hardening failure without terminating the program (as is the case if the `observe`
+// assertion semantic is used). Where possible, it logs in a way that indicates a fatal error (which might include
+// capturing the stack trace).
+#if !defined(_LIBCPP_LOG_HARDENING_FAILURE)
+
+# if !_LIBCPP_AVAILABILITY_HAS_LOG_HARDENING_FAILURE
+// The decltype is there to suppress -Wunused warnings in this configuration.
+void __use(const char*);
+# define _LIBCPP_LOG_HARDENING_FAILURE(message) (decltype(::std::__use(message))())
----------------
ldionne wrote:
We might want to slightly generalize this into `_LIBCPP_LOG_FATAL_ERROR`? And we could also accept an `enum` with a category so we can extend the reasons for logging later on. Alternatively, we might want to make this just `_LIBCPP_LOG_ERROR` since we're accepting a reason, so the reason could be `HARDENING`, `FATAL` or whatever we have a use case for.
https://github.com/llvm/llvm-project/pull/148172
More information about the libcxx-commits
mailing list