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 set_constraint_handler_s 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-backend-risc-v

@llvm/pr-subscribers-libc

Author: Muhammad Bassiouni (bassiounix)

Changes

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

Add set_constraint_handler_s required by Annex K interface in LLVM libc.


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

9 Files Affected:

  • (modified) libc/config/linux/aarch64/entrypoints.txt (+1)
  • (modified) libc/config/linux/riscv/entrypoints.txt (+1)
  • (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
  • (modified) libc/include/stdlib.yaml (+7)
  • (modified) libc/src/__support/annex_k/CMakeLists.txt (+9)
  • (added) libc/src/__support/annex_k/set_constraint_handler_s.h (+36)
  • (modified) libc/src/stdlib/CMakeLists.txt (+10)
  • (added) libc/src/stdlib/set_constraint_handler_s.cpp (+19)
  • (added) libc/src/stdlib/set_constraint_handler_s.h (+21)
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 48b7ca45157a6..12a108134a531 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1082,6 +1082,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdlib.getenv
     libc.src.stdlib.ignore_handler_s
     libc.src.stdlib.quick_exit
+    libc.src.stdlib.set_constraint_handler_s
 
     # signal.h entrypoints
     libc.src.signal.kill
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 0d3ff77d29b74..985d7fed577e9 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1209,6 +1209,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdlib.getenv
     libc.src.stdlib.ignore_handler_s
     libc.src.stdlib.quick_exit
+    libc.src.stdlib.set_constraint_handler_s
 
     # signal.h entrypoints
     libc.src.signal.kill
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index dd94ffb30fa9a..f02f4ab80d952 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1248,6 +1248,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdlib.getenv
     libc.src.stdlib.ignore_handler_s
     libc.src.stdlib.quick_exit
+    libc.src.stdlib.set_constraint_handler_s
 
     # signal.h entrypoints
     libc.src.signal.kill
diff --git a/libc/include/stdlib.yaml b/libc/include/stdlib.yaml
index 7c3b113a62415..49d45f105fe7a 100644
--- a/libc/include/stdlib.yaml
+++ b/libc/include/stdlib.yaml
@@ -202,6 +202,13 @@ functions:
       - type: void *__restrict
       - type: errno_t
     guard: 'LIBC_HAS_ANNEX_K'
+  - name: set_constraint_handler_s
+    standards:
+      - stdc
+    return_type: constraint_handler_t
+    arguments:
+      - type: constraint_handler_t
+    guard: 'LIBC_HAS_ANNEX_K'
   - name: srand
     standards:
       - stdc
diff --git a/libc/src/__support/annex_k/CMakeLists.txt b/libc/src/__support/annex_k/CMakeLists.txt
index 4ef4e32ebdd85..3b2d80130afc0 100644
--- a/libc/src/__support/annex_k/CMakeLists.txt
+++ b/libc/src/__support/annex_k/CMakeLists.txt
@@ -28,3 +28,12 @@ add_header_library(
     .abort_handler_s
     libc.hdr.types.constraint_handler_t
 )
+
+add_header_library(
+  set_constraint_handler_s
+  HDRS
+    set_constraint_handler_s.h
+  DEPENDS
+    .abort_handler_s
+    .libc_constraint_handler
+)
diff --git a/libc/src/__support/annex_k/set_constraint_handler_s.h b/libc/src/__support/annex_k/set_constraint_handler_s.h
new file mode 100644
index 0000000000000..d0be7d81200f6
--- /dev/null
+++ b/libc/src/__support/annex_k/set_constraint_handler_s.h
@@ -0,0 +1,36 @@
+//===-- Implementation for set_constraint_handler_s -------------*- 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_SET_CONSTRAINT_HANDLER_S_H
+#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_SET_CONSTRAINT_HANDLER_S_H
+
+#include "abort_handler_s.h"
+#include "libc_constraint_handler.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace annex_k {
+
+LIBC_INLINE static constraint_handler_t
+set_constraint_handler_s(constraint_handler_t handler) {
+  constraint_handler_t previous_handler = libc_constraint_handler;
+
+  if (!handler) {
+    libc_constraint_handler = &abort_handler_s;
+  } else {
+    libc_constraint_handler = handler;
+  }
+
+  return previous_handler;
+}
+
+} // namespace annex_k
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_SET_CONSTRAINT_HANDLER_S_H
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 5efd7f13ff3ee..03f5287cc0563 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -664,6 +664,16 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.system
 )
 
+add_entrypoint_object(
+  set_constraint_handler_s
+  SRCS
+    set_constraint_handler_s.cpp
+  HDRS
+    set_constraint_handler_s.h
+  DEPENDS
+    libc.src.__support.annex_k.set_constraint_handler_s
+)
+
 add_entrypoint_object(
   ignore_handler_s
   HDRS
diff --git a/libc/src/stdlib/set_constraint_handler_s.cpp b/libc/src/stdlib/set_constraint_handler_s.cpp
new file mode 100644
index 0000000000000..9e9d3b76c3bf1
--- /dev/null
+++ b/libc/src/stdlib/set_constraint_handler_s.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of set_constraint_handler_s ------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "set_constraint_handler_s.h"
+#include "src/__support/annex_k/set_constraint_handler_s.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(constraint_handler_t, set_constraint_handler_s,
+                   (constraint_handler_t handler)) {
+  return annex_k::set_constraint_handler_s(handler);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/set_constraint_handler_s.h b/libc/src/stdlib/set_constraint_handler_s.h
new file mode 100644
index 0000000000000..f5c6e01712ef0
--- /dev/null
+++ b/libc/src/stdlib/set_constraint_handler_s.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for set_constraint_handler_s ------*- 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_STDLIB_SET_CONSTRAINT_HANDLER_S_H
+#define LLVM_LIBC_SRC_STDLIB_SET_CONSTRAINT_HANDLER_S_H
+
+#include "hdr/types/constraint_handler_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+constraint_handler_t set_constraint_handler_s(constraint_handler_t handler);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDLIB_SET_CONSTRAINT_HANDLER_S_H

@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_stdlib_annex_k_add_set_constraint_handler_s branch from 861c120 to ab602a1 Compare October 14, 2025 04:29
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_stdlib_annex_k_add_set_constraint_handler_s branch from ab602a1 to f428171 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 8c1ce5e to 8694e32 Compare October 16, 2025 21:54
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_stdlib_annex_k_add_set_constraint_handler_s branch from f428171 to bcdb65f Compare October 16, 2025 21:58
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_libc_constraint_handler_macros branch 2 times, most recently from edaf560 to c6bdbcd Compare October 16, 2025 22:02
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_stdlib_annex_k_add_set_constraint_handler_s branch 2 times, most recently from 13af6bb to d04c9a3 Compare October 16, 2025 22:05
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_stdlib_annex_k_add_set_constraint_handler_s branch 2 times, most recently from 80068e5 to c46a87b 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 from c6bdbcd to d66c46e Compare October 16, 2025 22:17
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_stdlib_annex_k_add_set_constraint_handler_s branch from c46a87b to ebeefe4 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 d66c46e to cbc97af Compare October 16, 2025 23:01
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_stdlib_annex_k_add_set_constraint_handler_s branch from ebeefe4 to 72604c2 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 cbc97af to 71bcc2a Compare October 16, 2025 23:06
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_stdlib_annex_k_add_set_constraint_handler_s branch from 72604c2 to b959287 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 from 71bcc2a to ef18847 Compare October 17, 2025 13:13
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_stdlib_annex_k_add_set_constraint_handler_s branch from b959287 to 82751b1 Compare October 17, 2025 13:27
@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_stdlib_annex_k_add_set_constraint_handler_s branch from 82751b1 to a22a08e 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_stdlib_annex_k_add_set_constraint_handler_s branch from a22a08e to d112c94 Compare October 18, 2025 13:50
@bassiounix bassiounix closed this Oct 18, 2025
@bassiounix bassiounix deleted the users/bassiounix/spr/10-14-_libc_stdlib_annex_k_add_set_constraint_handler_s branch October 18, 2025 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants