Skip to content

Conversation

bassiounix
Copy link
Contributor

@bassiounix bassiounix commented Oct 14, 2025

RFC https://discourse.llvm.org/t/rfc-bounds-checking-interfaces-for-llvm-libc/87685

Add internal libc_constraint_handler required by Annex K interface in LLVM libc.

Copy link
Contributor Author

bassiounix commented Oct 14, 2025

@llvmbot
Copy link
Member

llvmbot commented Oct 14, 2025

@llvm/pr-subscribers-libc

Author: Muhammad Bassiouni (bassiounix)

Changes

RFC https://discourse.llvm.org/t/rfc-bounds-checking-interfaces-for-llvm-libc/87685

Add internal libc_constraint_handler required by Annex K interface in LLVM libc.


Full diff: https://github.com/llvm/llvm-project/pull/163315.diff

3 Files Affected:

  • (modified) libc/src/__support/annex_k/CMakeLists.txt (+9)
  • (added) libc/src/__support/annex_k/libc_constraint_handler.h (+26)
  • (added) libc/src/__support/annex_k/macros.h (+44)
diff --git a/libc/src/__support/annex_k/CMakeLists.txt b/libc/src/__support/annex_k/CMakeLists.txt
index 78f5b3cddebd7..8eb65f2469b4f 100644
--- a/libc/src/__support/annex_k/CMakeLists.txt
+++ b/libc/src/__support/annex_k/CMakeLists.txt
@@ -10,3 +10,12 @@ add_header_library(
     libc.src.__support.OSUtil.osutil
     libc.src.stdlib.abort
 )
+
+add_header_library(
+  libc_constraint_handler
+  HDRS
+    libc_constraint_handler.h
+  DEPENDS
+    .abort_handler_s
+    libc.hdr.types.constraint_handler_t
+)
diff --git a/libc/src/__support/annex_k/libc_constraint_handler.h b/libc/src/__support/annex_k/libc_constraint_handler.h
new file mode 100644
index 0000000000000..db01c8dd940a0
--- /dev/null
+++ b/libc/src/__support/annex_k/libc_constraint_handler.h
@@ -0,0 +1,26 @@
+//===-- Static header for libc_constraint_handler ---------------*- 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 LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDLER_H
+#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDLER_H
+
+#include "abort_handler_s.h"
+#include "hdr/types/constraint_handler_t.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace annex_k {
+
+LIBC_INLINE static constraint_handler_t libc_constraint_handler =
+    &abort_handler_s;
+
+} // namespace annex_k
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif  // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDLER_H
diff --git a/libc/src/__support/annex_k/macros.h b/libc/src/__support/annex_k/macros.h
new file mode 100644
index 0000000000000..835c161dd0d30
--- /dev/null
+++ b/libc/src/__support/annex_k/macros.h
@@ -0,0 +1,44 @@
+//===-- Helper macros header for constraint violations ----------*- 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 LLVM_LIBC_SRC___SUPPORT_ANNEX_K_MACROS_H
+#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_MACROS_H
+
+#include "libc_constraint_handler.h"
+#include "src/__support/libc_errno.h"
+
+#define _CONSTRAINT_VIOLATION(msg, error_code, ret_code)                       \
+  {                                                                            \
+    libc_errno = error_code;                                                   \
+    libc_constraint_handler(msg, nullptr, error_code);                         \
+    return ret_code;                                                           \
+  }
+
+#define _CONSTRAINT_VIOLATION_IF(expr, error_code, return_code)                \
+  {                                                                            \
+    auto expr_val = expr;                                                      \
+    if (expr_val) {                                                            \
+      libc_errno = error_code;                                                 \
+      libc_constraint_handler(nullptr, nullptr, error_code);                   \
+      return return_code;                                                      \
+    }                                                                          \
+  }
+
+#define _CONSTRAINT_VIOLATION_CLEANUP_IF(expr, cleanup, error_code,            \
+                                         return_code)                          \
+  {                                                                            \
+    auto expr_val = expr;                                                      \
+    if (expr_val) {                                                            \
+      cleanup;                                                                 \
+      libc_errno = error_code;                                                 \
+      libc_constraint_handler(nullptr, nullptr, error_code);                   \
+      return return_code;                                                      \
+    }                                                                          \
+  }
+
+#endif // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_MACROS_H

Copy link

github-actions bot commented Oct 14, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions h -- libc/src/__support/annex_k/libc_constraint_handler.h

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/libc/src/__support/annex_k/libc_constraint_handler.h b/libc/src/__support/annex_k/libc_constraint_handler.h
index db01c8dd9..9b0a45d09 100644
--- a/libc/src/__support/annex_k/libc_constraint_handler.h
+++ b/libc/src/__support/annex_k/libc_constraint_handler.h
@@ -23,4 +23,4 @@ LIBC_INLINE static constraint_handler_t libc_constraint_handler =
 
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif  // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDLER_H
+#endif // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDLER_H

@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler branch from cec1eb5 to bdb084e Compare October 14, 2025 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants