[libcxx-commits] [libcxx] [libc++][hardening] Add an experimental function to log hardening errors (PR #149452)

Konstantin Varlamov via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jul 23 02:15:44 PDT 2025


================
@@ -0,0 +1,40 @@
+// -*- 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
+
+#if _LIBCPP_HAS_EXPERIMENTAL_HARDENING_OBSERVE_SEMANTIC
+
+_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_EXPORTED_FROM_ABI void __log_hardening_failure(const char* __message) _NOEXCEPT;
----------------
var-const wrote:

> if most system API calls (IMO unfortunately) only provide an unsized version

FWIW, I fully agree that null-terminated strings are an unfortunate design decision; I don't know if it made more sense back in the day, but it is definitely part of the legacy burden on C++ nowadays.

> Going the other way, do we expect this to be so rarely called that it should be considered cold?

I don't have any objection to this -- logically, it should be. At the very least, it makes sense to me to prioritize performance of valid code (which will start applying to the previously invalid code as well once the bugs are fixed).

We don't have a precedent for it, however (or at least I couldn't find it with simple grepping). Do you think this patch warrants introducing a cold attribute (which would have to work with MSVC, etc.)?

https://github.com/llvm/llvm-project/pull/149452


More information about the libcxx-commits mailing list