Skip to content

Conversation

bassiounix
Copy link
Contributor

@bassiounix bassiounix commented Oct 18, 2025

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

Add abort_handler_s required by Annex K interface in LLVM libc.

Copy link
Contributor Author

bassiounix commented Oct 18, 2025

@llvmbot
Copy link
Member

llvmbot commented Oct 18, 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 abort_handler_s required by Annex K interface in LLVM libc.


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

11 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/CMakeLists.txt (+1)
  • (modified) libc/include/stdlib.yaml (+13-1)
  • (modified) libc/src/__support/CMakeLists.txt (+1)
  • (added) libc/src/__support/annex_k/CMakeLists.txt (+12)
  • (added) libc/src/__support/annex_k/abort_handler_s.h (+43)
  • (modified) libc/src/stdlib/CMakeLists.txt (+10)
  • (added) libc/src/stdlib/abort_handler_s.cpp (+20)
  • (added) libc/src/stdlib/abort_handler_s.h (+22)
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 8bf6c44b1d669..79c330f94f5ea 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1076,6 +1076,7 @@ if(LLVM_LIBC_FULL_BUILD)
     # stdlib.h entrypoints
     libc.src.stdlib._Exit
     libc.src.stdlib.abort
+    libc.src.stdlib.abort_handler_s
     libc.src.stdlib.at_quick_exit
     libc.src.stdlib.atexit
     libc.src.stdlib.exit
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index dffccbab9a8e9..3d133667c31d9 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1204,6 +1204,7 @@ if(LLVM_LIBC_FULL_BUILD)
     # stdlib.h entrypoints
     libc.src.stdlib._Exit
     libc.src.stdlib.abort
+    libc.src.stdlib.abort_handler_s
     libc.src.stdlib.at_quick_exit
     libc.src.stdlib.atexit
     libc.src.stdlib.exit
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b4ab073ec912f..8f6b161b1777a 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1243,6 +1243,7 @@ if(LLVM_LIBC_FULL_BUILD)
     # stdlib.h entrypoints
     libc.src.stdlib._Exit
     libc.src.stdlib.abort
+    libc.src.stdlib.abort_handler_s
     libc.src.stdlib.at_quick_exit
     libc.src.stdlib.atexit
     libc.src.stdlib.exit
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 3266b57bc8ecd..7ef6940763519 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -368,6 +368,7 @@ add_header_macro(
     .llvm-libc-types.__search_compare_t
     .llvm-libc-types.constraint_handler_t
     .llvm-libc-types.div_t
+    .llvm-libc-types.errno_t
     .llvm-libc-types.ldiv_t
     .llvm-libc-types.lldiv_t
     .llvm-libc-types.locale_t
diff --git a/libc/include/stdlib.yaml b/libc/include/stdlib.yaml
index 29fd6bc3a1e75..050cf246decf6 100644
--- a/libc/include/stdlib.yaml
+++ b/libc/include/stdlib.yaml
@@ -5,7 +5,9 @@ standards:
 merge_yaml_files:
   - stdlib-malloc.yaml
 macros:
-  - macro_name: NULL
+  - macro_name: 'LIBC_HAS_ANNEX_K'
+    macro_header: annex-k-macros.h
+  - macro_name: 'NULL'
     macro_header: null-macro.h
 types:
   - type_name: __atexithandler_t
@@ -14,6 +16,7 @@ types:
   - type_name: __search_compare_t
   - type_name: constraint_handler_t
   - type_name: div_t
+  - type_name: errno_t
   - type_name: ldiv_t
   - type_name: lldiv_t
   - type_name: locale_t
@@ -181,6 +184,15 @@ functions:
     return_type: int
     arguments:
       - type: void
+  - name: abort_handler_s
+    standards:
+      - stdc
+    return_type: void
+    arguments:
+      - type: const char *__restrict
+      - type: void *__restrict
+      - type: errno_t
+    guard: 'LIBC_HAS_ANNEX_K'
   - name: srand
     standards:
       - stdc
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 0ef09a9b8c9d0..b71a56826156b 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -1,3 +1,4 @@
+add_subdirectory(annex_k)
 add_subdirectory(CPP)
 add_subdirectory(macros)
 
diff --git a/libc/src/__support/annex_k/CMakeLists.txt b/libc/src/__support/annex_k/CMakeLists.txt
new file mode 100644
index 0000000000000..78f5b3cddebd7
--- /dev/null
+++ b/libc/src/__support/annex_k/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_header_library(
+  abort_handler_s
+  HDRS
+    abort_handler_s.h
+  DEPENDS
+    libc.hdr.stdio_macros
+    libc.hdr.types.errno_t
+    libc.src.__support.macros.config
+    libc.src.__support.macros.attributes
+    libc.src.__support.OSUtil.osutil
+    libc.src.stdlib.abort
+)
diff --git a/libc/src/__support/annex_k/abort_handler_s.h b/libc/src/__support/annex_k/abort_handler_s.h
new file mode 100644
index 0000000000000..dca3dcc2d9825
--- /dev/null
+++ b/libc/src/__support/annex_k/abort_handler_s.h
@@ -0,0 +1,43 @@
+//===-- Implementation for abort_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_ABORT_HANDLER_S_H
+#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_ABORT_HANDLER_S_H
+
+#include "hdr/stdio_macros.h"
+#include "hdr/types/errno_t.h"
+#include "src/__support/OSUtil/io.h"
+#include "src/__support/common.h"
+#include "src/stdlib/abort.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace annex_k {
+
+LIBC_INLINE static void abort_handler_s(const char *__restrict msg,
+                                        [[maybe_unused]] void *__restrict ptr,
+                                        [[maybe_unused]] errno_t error) {
+  write_to_stderr("abort_handler_s was called in response to a "
+                  "runtime-constraint violation.\n\n");
+
+  if (msg)
+    write_to_stderr(msg);
+
+  write_to_stderr(
+      "\n\nNote to end users: This program was terminated as a result\
+of a bug present in the software. Please reach out to your\
+software's vendor to get more help.\n");
+
+  abort();
+}
+
+} // namespace annex_k
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_ABORT_HANDLER_S_H
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index c464f82dcbda7..8fd149880e2d5 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -647,6 +647,16 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.abort
 )
 
+add_entrypoint_object(
+  abort_handler_s
+  SRCS
+    abort_handler_s.cpp
+  HDRS
+    abort_handler_s.h
+  DEPENDS
+    libc.src.__support.annex_k.abort_handler_s
+)
+
 add_entrypoint_object(
   system
   ALIAS
diff --git a/libc/src/stdlib/abort_handler_s.cpp b/libc/src/stdlib/abort_handler_s.cpp
new file mode 100644
index 0000000000000..85a4cd4858315
--- /dev/null
+++ b/libc/src/stdlib/abort_handler_s.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation for abort_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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/abort_handler_s.h"
+#include "src/__support/annex_k/abort_handler_s.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, abort_handler_s,
+                   (const char *__restrict msg, void *__restrict ptr,
+                    errno_t error)) {
+  return annex_k::abort_handler_s(msg, ptr, error);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/abort_handler_s.h b/libc/src/stdlib/abort_handler_s.h
new file mode 100644
index 0000000000000..b97a081b036d2
--- /dev/null
+++ b/libc/src/stdlib/abort_handler_s.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for abort_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_ABORT_HANDLER_S_H
+#define LLVM_LIBC_SRC_STDLIB_ABORT_HANDLER_S_H
+
+#include "hdr/types/errno_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+void abort_handler_s(const char *__restrict msg, void *__restrict ptr,
+                     errno_t error);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDLIB_ABORT_HANDLER_S_H

@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-13-_libc_annex_k_add_constraint_handler_t branch from 865a661 to 696c143 Compare October 18, 2025 15:02
@bassiounix bassiounix force-pushed the users/bassiounix/spr/10-14-_libc_annex_k_add_abort_handler_s branch from 37f49e3 to 6a9bfac Compare October 18, 2025 15:02
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.

2 participants