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 macros 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 macros required by Annex K interface in LLVM libc.


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

2 Files Affected:

  • (modified) libc/src/__support/annex_k/CMakeLists.txt (+9)
  • (added) libc/src/__support/annex_k/constraint_macros.h (+44)
diff --git a/libc/src/__support/annex_k/CMakeLists.txt b/libc/src/__support/annex_k/CMakeLists.txt
index 8eb65f2469b4f..4ef4e32ebdd85 100644
--- a/libc/src/__support/annex_k/CMakeLists.txt
+++ b/libc/src/__support/annex_k/CMakeLists.txt
@@ -11,6 +11,15 @@ add_header_library(
     libc.src.stdlib.abort
 )
 
+add_header_library(
+  constraint_macros
+  HDRS
+    constraint_macros.h
+  DEPENDS
+    .libc_constraint_handler
+    libc.src.__support.libc_errno
+)
+
 add_header_library(
   libc_constraint_handler
   HDRS
diff --git a/libc/src/__support/annex_k/constraint_macros.h b/libc/src/__support/annex_k/constraint_macros.h
new file mode 100644
index 0000000000000..835c161dd0d30
--- /dev/null
+++ b/libc/src/__support/annex_k/constraint_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

@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler_macros branch from 8c1ce5e to 8694e32 Compare October 16, 2025 21:54
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler_macros branch from 8694e32 to edaf560 Compare October 16, 2025 21:58
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler branch 2 times, most recently from a78a769 to 13d94e2 Compare October 16, 2025 22:02
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler_macros branch from edaf560 to c6bdbcd Compare October 16, 2025 22:02
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler branch from 13d94e2 to 6c5ed3e Compare October 16, 2025 22:17
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler_macros branch 2 times, most recently from d66c46e to cbc97af Compare October 16, 2025 23:01
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler branch from 6c5ed3e to c7bce13 Compare October 16, 2025 23:01
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler_macros branch from cbc97af to 71bcc2a Compare October 16, 2025 23:06
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler branch from c7bce13 to 387bde0 Compare October 16, 2025 23:06
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler_macros branch from 71bcc2a to ef18847 Compare October 17, 2025 13:13
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler branch from 387bde0 to 63619c4 Compare October 17, 2025 13:13
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler_macros branch 2 times, most recently from fa1dee7 to 3168fd8 Compare October 18, 2025 13:38
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler branch from 993b396 to 20f3456 Compare October 18, 2025 13:38
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler_macros branch from 3168fd8 to fb877c9 Compare October 18, 2025 13:50
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler branch from 20f3456 to 525adcc Compare October 18, 2025 13:50
@bassiounix bassiounix closed this Oct 18, 2025
@bassiounix bassiounix deleted the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler_macros branch October 18, 2025 14:12
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