[libc-commits] [libc] [libc][stdio] Add fopen_s and bootstrap annex k. (PR #152248)
Muhammad Bassiouni via libc-commits
libc-commits at lists.llvm.org
Sat Aug 9 07:23:29 PDT 2025
https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/152248
>From 18c2a2dc9c232c072a0c109c4e6710bf919c8596 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 6 Aug 2025 07:49:12 +0300
Subject: [PATCH 01/16] [libc] Add fopen_s.
---
libc/hdr/types/CMakeLists.txt | 18 ++++++++
libc/hdr/types/constraint_handler_t.h | 14 ++++++
libc/hdr/types/errno_t.h | 14 ++++++
libc/include/CMakeLists.txt | 3 ++
libc/include/errno.h.def | 2 +
libc/include/llvm-libc-types/CMakeLists.txt | 4 ++
.../llvm-libc-types/constraint_handler_t.h | 17 +++++++
libc/include/llvm-libc-types/errno_t.h | 14 ++++++
libc/include/stdio.yaml | 9 ++++
libc/include/stdlib.yaml | 8 ++++
libc/src/__support/CMakeLists.txt | 2 +
libc/src/__support/annex_k/CMakeLists.txt | 40 +++++++++++++++++
libc/src/__support/annex_k/abort_handler_s.h | 41 +++++++++++++++++
libc/src/__support/annex_k/helper_macros.h | 43 ++++++++++++++++++
libc/src/__support/annex_k/ignore_handler_s.h | 23 ++++++++++
.../annex_k/libc_constraint_hander.h | 22 ++++++++++
libc/src/__support/stdio/CMakeLists.txt | 9 ++++
libc/src/__support/stdio/fopen.h | 34 ++++++++++++++
libc/src/stdio/CMakeLists.txt | 8 ++++
libc/src/stdio/fopen_s.h | 23 ++++++++++
libc/src/stdio/generic/CMakeLists.txt | 16 +++++--
libc/src/stdio/generic/fopen.cpp | 12 +----
libc/src/stdio/generic/fopen_s.cpp | 44 +++++++++++++++++++
libc/src/stdlib/CMakeLists.txt | 12 +++++
libc/src/stdlib/set_constraint_handler_s.cpp | 29 ++++++++++++
libc/src/stdlib/set_constraint_handler_s.h | 21 +++++++++
26 files changed, 469 insertions(+), 13 deletions(-)
create mode 100644 libc/hdr/types/constraint_handler_t.h
create mode 100644 libc/hdr/types/errno_t.h
create mode 100644 libc/include/llvm-libc-types/constraint_handler_t.h
create mode 100644 libc/include/llvm-libc-types/errno_t.h
create mode 100644 libc/src/__support/annex_k/CMakeLists.txt
create mode 100644 libc/src/__support/annex_k/abort_handler_s.h
create mode 100644 libc/src/__support/annex_k/helper_macros.h
create mode 100644 libc/src/__support/annex_k/ignore_handler_s.h
create mode 100644 libc/src/__support/annex_k/libc_constraint_hander.h
create mode 100644 libc/src/__support/stdio/CMakeLists.txt
create mode 100644 libc/src/__support/stdio/fopen.h
create mode 100644 libc/src/stdio/fopen_s.h
create mode 100644 libc/src/stdio/generic/fopen_s.cpp
create mode 100644 libc/src/stdlib/set_constraint_handler_s.cpp
create mode 100644 libc/src/stdlib/set_constraint_handler_s.h
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 02fe1adc264fb..6cd39d0dca020 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -162,6 +162,24 @@ add_proxy_header_library(
libc.include.fcntl
)
+add_proxy_header_library(
+ errno_t
+ HDRS
+ errno_t.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.errno_t
+ libc.include.errno
+)
+
+add_proxy_header_library(
+ constraint_handler_t
+ HDRS
+ constraint_handler_t.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.constraint_handler_t
+ libc.include.stdlib
+)
+
add_proxy_header_library(
fenv_t
HDRS
diff --git a/libc/hdr/types/constraint_handler_t.h b/libc/hdr/types/constraint_handler_t.h
new file mode 100644
index 0000000000000..df7eaa7abf48b
--- /dev/null
+++ b/libc/hdr/types/constraint_handler_t.h
@@ -0,0 +1,14 @@
+//===-- Proxy for constraint_handler_t ------------------------------------===//
+//
+// 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_HDR_TYPES_CONSTRAINT_HANDLER_T_H
+#define LLVM_LIBC_HDR_TYPES_CONSTRAINT_HANDLER_T_H
+
+#include "include/llvm-libc-types/constraint_handler_t.h"
+
+#endif // LLVM_LIBC_HDR_TYPES_CONSTRAINT_HANDLER_T_H
diff --git a/libc/hdr/types/errno_t.h b/libc/hdr/types/errno_t.h
new file mode 100644
index 0000000000000..8769819d81474
--- /dev/null
+++ b/libc/hdr/types/errno_t.h
@@ -0,0 +1,14 @@
+//===-- Proxy for errno_t -------------------------------------------------===//
+//
+// 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_HDR_TYPES_ERRNO_T_H
+#define LLVM_LIBC_HDR_TYPES_ERRNO_T_H
+
+#include "include/llvm-libc-types/errno_t.h"
+
+#endif // LLVM_LIBC_HDR_TYPES_ERRNO_T_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 74fcea0f15e2f..d33ba79765846 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -290,6 +290,7 @@ add_header_macro(
DEPENDS
.llvm-libc-macros.generic_error_number_macros
.llvm-libc-macros.error_number_macros
+ .llvm-libc-types.errno_t
)
add_header_macro(
@@ -339,6 +340,7 @@ add_header_macro(
.llvm-libc-types.off_t
.llvm-libc-types.size_t
.llvm-libc-types.ssize_t
+ .llvm-libc-types.errno_t
.llvm_libc_common_h
)
@@ -358,6 +360,7 @@ add_header_macro(
.llvm-libc-types.lldiv_t
.llvm-libc-types.locale_t
.llvm-libc-types.size_t
+ .llvm-libc-types.constraint_handler_t
.llvm_libc_common_h
)
diff --git a/libc/include/errno.h.def b/libc/include/errno.h.def
index aa1f6c9e48444..35341c60e38b2 100644
--- a/libc/include/errno.h.def
+++ b/libc/include/errno.h.def
@@ -33,4 +33,6 @@ __END_C_DECLS
#define errno (*__llvm_libc_errno())
+#include "llvm-libc-types/errno_t.h"
+
#endif // LLVM_LIBC_ERRNO_H
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 451beae6f1e6f..dc0650b2bd039 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -289,3 +289,7 @@ add_header(EFI_SYSTEM_TABLE
.EFI_TABLE_HEADER
.char16_t
)
+
+add_header(errno_t HDR errno_t.h)
+
+add_header(constraint_handler_t HDR constraint_handler_t.h DEPENDS .errno_t)
diff --git a/libc/include/llvm-libc-types/constraint_handler_t.h b/libc/include/llvm-libc-types/constraint_handler_t.h
new file mode 100644
index 0000000000000..b3dfeb67067f1
--- /dev/null
+++ b/libc/include/llvm-libc-types/constraint_handler_t.h
@@ -0,0 +1,17 @@
+//===-- Definition of type constraint_handler_t ---------------------------===//
+//
+// 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_INCLUDE_LLVM_LIBC_TYPES_CONSTRAINT_HANDLER_T_H
+#define LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_CONSTRAINT_HANDLER_T_H
+
+#include "errno_t.h"
+
+typedef void (*constraint_handler_t)(const char *__restrict msg,
+ void *__restrict ptr, errno_t error);
+
+#endif // LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_CONSTRAINT_HANDLER_T_H
diff --git a/libc/include/llvm-libc-types/errno_t.h b/libc/include/llvm-libc-types/errno_t.h
new file mode 100644
index 0000000000000..460f072dbd680
--- /dev/null
+++ b/libc/include/llvm-libc-types/errno_t.h
@@ -0,0 +1,14 @@
+//===-- Definition of type errno_t ----------------------------------------===//
+//
+// 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_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
+#define LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
+
+typedef int errno_t;
+
+#endif // LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
diff --git a/libc/include/stdio.yaml b/libc/include/stdio.yaml
index 2a0c563709984..7674720b03b68 100644
--- a/libc/include/stdio.yaml
+++ b/libc/include/stdio.yaml
@@ -11,6 +11,7 @@ macros:
macro_value: stderr
types:
- type_name: size_t
+ - type_name: errno_t
- type_name: off_t
- type_name: cookie_io_functions_t
- type_name: FILE
@@ -125,6 +126,14 @@ functions:
arguments:
- type: const char *
- type: const char *
+ - name: fopen_s
+ standards:
+ - stdc
+ return_type: errno_t
+ arguments:
+ - type: FILE *__restrict *__restrict
+ - type: const char *__restrict
+ - type: const char *__restrict
- name: fopencookie
standards:
- GNUExtensions
diff --git a/libc/include/stdlib.yaml b/libc/include/stdlib.yaml
index 3b2ff13c684b1..1e6824950ffd8 100644
--- a/libc/include/stdlib.yaml
+++ b/libc/include/stdlib.yaml
@@ -17,6 +17,8 @@ types:
- type_name: lldiv_t
- type_name: locale_t
- type_name: size_t
+ - type_name: errno_t
+ - type_name: constraint_handler_t
enums: []
objects: []
functions:
@@ -180,6 +182,12 @@ functions:
return_type: int
arguments:
- type: void
+ - name: set_constraint_handler_s
+ standards:
+ - stdc
+ return_type: constraint_handler_t
+ arguments:
+ - type: constraint_handler_t
- name: srand
standards:
- stdc
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 2196d9e23bba7..f2239e7d27ade 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -1,5 +1,7 @@
+add_subdirectory(annex_k)
add_subdirectory(CPP)
add_subdirectory(macros)
+add_subdirectory(stdio)
add_header_library(
libc_errno
diff --git a/libc/src/__support/annex_k/CMakeLists.txt b/libc/src/__support/annex_k/CMakeLists.txt
new file mode 100644
index 0000000000000..b3b472a8dbd16
--- /dev/null
+++ b/libc/src/__support/annex_k/CMakeLists.txt
@@ -0,0 +1,40 @@
+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.File.file
+ libc.src.stdio.fprintf
+ libc.src.stdio.fflush
+ libc.src.stdlib.abort
+)
+
+add_header_library(
+ ignore_handler_s
+ HDRS
+ ignore_handler_s.h
+ DEPENDS
+ libc.hdr.types.errno_t
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.__support.macros.attributes
+)
+
+add_header_library(
+ helper_macros
+ HDRS
+ helper_macros.h
+)
+
+add_header_library(
+ libc_constraint_handler
+ HDRS
+ libc_constraint_handler.h
+ DEPENDS
+ libc.src.__support.annex_k.abort_handler_s
+ libc.hdr.types.errno_t
+)
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..db3512bb0bca5
--- /dev/null
+++ b/libc/src/__support/annex_k/abort_handler_s.h
@@ -0,0 +1,41 @@
+//===-- 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___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/libc_errno.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+#include "src/stdio/fflush.h"
+#include "src/stdio/fprintf.h"
+#include "src/stdlib/abort.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LIBC_INLINE static void abort_handler_s(const char *__restrict msg,
+ void *__restrict ptr, errno_t error) {
+ libc_errno = error;
+ fprintf(stderr, "abort_handler_s was called in response to a "
+ "runtime-constraint violation.\n\n");
+ if (msg)
+ fprintf(stderr, "%s\n", msg);
+ fprintf(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");
+
+ fflush(stderr);
+ abort();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_ABORT_HANDLER_S_H
diff --git a/libc/src/__support/annex_k/helper_macros.h b/libc/src/__support/annex_k/helper_macros.h
new file mode 100644
index 0000000000000..e11869549897e
--- /dev/null
+++ b/libc/src/__support/annex_k/helper_macros.h
@@ -0,0 +1,43 @@
+//===-- 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_HELPER_MACROS_H
+#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_HELPER_MACROS_H
+
+#include "libc_constraint_hander.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_HELPER_MACROS_H
diff --git a/libc/src/__support/annex_k/ignore_handler_s.h b/libc/src/__support/annex_k/ignore_handler_s.h
new file mode 100644
index 0000000000000..7690776429945
--- /dev/null
+++ b/libc/src/__support/annex_k/ignore_handler_s.h
@@ -0,0 +1,23 @@
+//===-- Implementation header for ignore_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_IGNORE_HANDLER_S_H
+#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_IGNORE_HANDLER_S_H
+
+#include "hdr/types/errno_t.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LIBC_INLINE static void ignore_handler_s(const char *__restrict msg,
+ void *__restrict ptr, errno_t error) {}
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_IGNORE_HANDLER_S_H
diff --git a/libc/src/__support/annex_k/libc_constraint_hander.h b/libc/src/__support/annex_k/libc_constraint_hander.h
new file mode 100644
index 0000000000000..a21b966133104
--- /dev/null
+++ b/libc/src/__support/annex_k/libc_constraint_hander.h
@@ -0,0 +1,22 @@
+//===-- 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_HANDER_H
+#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDER_H
+
+#include "hdr/types/constraint_handler_t.h"
+#include "src/__support/annex_k/abort_handler_s.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LIBC_INLINE static constraint_handler_t libc_constraint_handler =
+ &abort_handler_s;
+
+}
+
+#endif // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDER_H
diff --git a/libc/src/__support/stdio/CMakeLists.txt b/libc/src/__support/stdio/CMakeLists.txt
new file mode 100644
index 0000000000000..3bb93b634c1a0
--- /dev/null
+++ b/libc/src/__support/stdio/CMakeLists.txt
@@ -0,0 +1,9 @@
+add_header_library(
+ fopen
+ HDRS
+ fopen.h
+ DEPENDS
+ libc.hdr.types.FILE
+ libc.src.__support.File.file
+ libc.src.__support.libc_errno
+)
diff --git a/libc/src/__support/stdio/fopen.h b/libc/src/__support/stdio/fopen.h
new file mode 100644
index 0000000000000..738b7963db50c
--- /dev/null
+++ b/libc/src/__support/stdio/fopen.h
@@ -0,0 +1,34 @@
+//===-- Common implementation of fopen ------------------------------------===//
+//
+// 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_STDIO_FOPEN_H
+#define LLVM_LIBC_SRC___SUPPORT_STDIO_FOPEN_H
+
+#include "hdr/types/FILE.h"
+#include "src/__support/File/file.h"
+#include "src/__support/libc_errno.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace stdio_internal {
+
+LIBC_INLINE static constexpr ::FILE *fopen(const char *__restrict name,
+ const char *__restrict mode) {
+ auto result = LIBC_NAMESPACE::openfile(name, mode);
+ if (!result.has_value()) {
+ libc_errno = result.error();
+ return nullptr;
+ }
+ return reinterpret_cast<::FILE *>(result.value());
+}
+
+} // namespace stdio_internal
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_STDIO_FOPEN_H
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index b0a6ef1e291b5..32810b41b8866 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -204,6 +204,14 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.fdopen
)
+add_entrypoint_object(
+ fopen_s
+ HDRS
+ fopen_s.h
+ DEPENDS
+ .generic.fopen_s
+)
+
# These entrypoints have multiple potential implementations.
add_stdio_entrypoint_object(feof)
add_stdio_entrypoint_object(feof_unlocked)
diff --git a/libc/src/stdio/fopen_s.h b/libc/src/stdio/fopen_s.h
new file mode 100644
index 0000000000000..19f3f9cf1ee7c
--- /dev/null
+++ b/libc/src/stdio/fopen_s.h
@@ -0,0 +1,23 @@
+//===-- Implementation header of fopen_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_STDIO_FOPEN_S_H
+#define LLVM_LIBC_SRC_STDIO_FOPEN_S_H
+
+#include "hdr/types/FILE.h"
+#include "include/llvm-libc-types/errno_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+errno_t fopen_s(FILE *__restrict *__restrict streamptr,
+ const char *__restrict filename, const char *__restrict mode);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDIO_FOPEN_S_H
diff --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt
index 6361822b61999..0898ae83a76ac 100644
--- a/libc/src/stdio/generic/CMakeLists.txt
+++ b/libc/src/stdio/generic/CMakeLists.txt
@@ -162,9 +162,19 @@ add_generic_entrypoint_object(
HDRS
../fopen.h
DEPENDS
- libc.hdr.types.FILE
- libc.src.__support.File.file
- libc.src.__support.File.platform_file
+ libc.src.__support.stdio.fopen
+)
+
+add_generic_entrypoint_object(
+ fopen_s
+ SRCS
+ fopen_s.cpp
+ HDRS
+ ../fopen_s.h
+ DEPENDS
+ libc.src.__support.stdio.fopen
+ libc.src.__support.macros.config
+ libc.src.__support.annex_k.helper_macros
)
add_generic_entrypoint_object(
diff --git a/libc/src/stdio/generic/fopen.cpp b/libc/src/stdio/generic/fopen.cpp
index 57c85c2e54e16..a1db51a00bde2 100644
--- a/libc/src/stdio/generic/fopen.cpp
+++ b/libc/src/stdio/generic/fopen.cpp
@@ -7,22 +7,14 @@
//===----------------------------------------------------------------------===//
#include "src/stdio/fopen.h"
-#include "src/__support/File/file.h"
-
-#include "hdr/types/FILE.h"
-#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
+#include "src/__support/stdio/fopen.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(::FILE *, fopen,
(const char *__restrict name, const char *__restrict mode)) {
- auto result = LIBC_NAMESPACE::openfile(name, mode);
- if (!result.has_value()) {
- libc_errno = result.error();
- return nullptr;
- }
- return reinterpret_cast<::FILE *>(result.value());
+ return stdio_internal::fopen(name, mode);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/generic/fopen_s.cpp b/libc/src/stdio/generic/fopen_s.cpp
new file mode 100644
index 0000000000000..546218ff76f27
--- /dev/null
+++ b/libc/src/stdio/generic/fopen_s.cpp
@@ -0,0 +1,44 @@
+//===-- Implementation of fopen_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 "src/stdio/fopen_s.h"
+#include "src/__support/annex_k/helper_macros.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/stdio/fopen.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(errno_t, fopen_s,
+ (FILE *__restrict *__restrict streamptr,
+ const char *__restrict filename,
+ const char *__restrict mode)) {
+ _CONSTRAINT_VIOLATION_IF(streamptr == 0, EINVAL, EINVAL);
+ _CONSTRAINT_VIOLATION_CLEANUP_IF(!mode || !filename, *streamptr = nullptr,
+ EINVAL, EINVAL);
+
+ FILE *ret = nullptr;
+
+ if (mode[0] == 'u') {
+ ret = stdio_internal::fopen(filename, mode + 1);
+ if (!ret) {
+ *streamptr = nullptr;
+ return -1;
+ }
+ } else {
+ ret = stdio_internal::fopen(filename, mode);
+ if (!ret) {
+ *streamptr = nullptr;
+ return -1;
+ }
+ }
+
+ *streamptr = ret;
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index aa653c38a8c3f..0165c799b7aa8 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -652,3 +652,15 @@ add_entrypoint_object(
DEPENDS
.${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.abort_handler_s
+ libc.src.__support.annex_k.libc_constraint_handler
+ libc.src.__support.macros.config
+)
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..5d215a58461be
--- /dev/null
+++ b/libc/src/stdlib/set_constraint_handler_s.cpp
@@ -0,0 +1,29 @@
+//===-- 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/abort_handler_s.h"
+#include "src/__support/annex_k/libc_constraint_hander.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(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 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..6da7e85bd693a
--- /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_STRTOULL_H
+#define LLVM_LIBC_SRC_STDLIB_STRTOULL_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_STRTOULL_H
>From 4eaffd3a7b1eb2b5f70bb53c8fec8eb8c2d7657d Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 6 Aug 2025 11:35:03 +0300
Subject: [PATCH 02/16] add function to linux x86 entrypoint
---
libc/config/linux/x86_64/entrypoints.txt | 3 +++
libc/include/CMakeLists.txt | 1 +
libc/include/llvm-libc-types/CMakeLists.txt | 1 +
libc/include/llvm-libc-types/rsize_t.h | 14 ++++++++++++++
libc/include/stdlib.yaml | 17 +++++++++++++++++
libc/src/__support/annex_k/CMakeLists.txt | 2 +-
libc/src/__support/annex_k/abort_handler_s.h | 3 ++-
libc/src/__support/annex_k/helper_macros.h | 2 +-
...raint_hander.h => libc_constraint_handler.h} | 2 +-
libc/src/stdio/CMakeLists.txt | 9 +--------
libc/src/stdlib/set_constraint_handler_s.cpp | 2 +-
11 files changed, 43 insertions(+), 13 deletions(-)
create mode 100644 libc/include/llvm-libc-types/rsize_t.h
rename libc/src/__support/annex_k/{libc_constraint_hander.h => libc_constraint_handler.h} (93%)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 066dc21497691..e3de356c13304 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -198,6 +198,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.qsort
libc.src.stdlib.qsort_r
libc.src.stdlib.rand
+ libc.src.stdlib.set_constraint_handler_s
libc.src.stdlib.srand
libc.src.stdlib.strfromd
libc.src.stdlib.strfromf
@@ -1131,6 +1132,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.fileno
libc.src.stdio.flockfile
libc.src.stdio.fopen
+ libc.src.stdio.fopen_s
libc.src.stdio.fopencookie
libc.src.stdio.fputc
libc.src.stdio.fputs
@@ -1166,6 +1168,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdlib.exit
libc.src.stdlib.getenv
libc.src.stdlib.quick_exit
+ libc.src.stdlib.set_constraint_handler_s
# signal.h entrypoints
libc.src.signal.kill
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index d33ba79765846..7d8186e215453 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -360,6 +360,7 @@ add_header_macro(
.llvm-libc-types.lldiv_t
.llvm-libc-types.locale_t
.llvm-libc-types.size_t
+ .llvm-libc-types.rsize_t
.llvm-libc-types.constraint_handler_t
.llvm_libc_common_h
)
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index dc0650b2bd039..6480491f6ea4c 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -1,5 +1,6 @@
add_header(off64_t HDR off64_t.h)
add_header(size_t HDR size_t.h)
+add_header(rsize_t HDR rsize_t.h)
add_header(ssize_t HDR ssize_t.h)
add_header(__atfork_callback_t HDR __atfork_callback_t.h)
add_header(__search_compare_t HDR __search_compare_t.h)
diff --git a/libc/include/llvm-libc-types/rsize_t.h b/libc/include/llvm-libc-types/rsize_t.h
new file mode 100644
index 0000000000000..dca61c4f76a05
--- /dev/null
+++ b/libc/include/llvm-libc-types/rsize_t.h
@@ -0,0 +1,14 @@
+//===-- Definition of rsize_t types ---------------------------------------===//
+//
+// 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_TYPES_RSIZE_T_H
+#define LLVM_LIBC_TYPES_RSIZE_T_H
+
+typedef __SIZE_TYPE__ rsize_t;
+
+#endif // LLVM_LIBC_TYPES_RSIZE_T_H
diff --git a/libc/include/stdlib.yaml b/libc/include/stdlib.yaml
index 1e6824950ffd8..c225a53b87f39 100644
--- a/libc/include/stdlib.yaml
+++ b/libc/include/stdlib.yaml
@@ -17,6 +17,7 @@ types:
- type_name: lldiv_t
- type_name: locale_t
- type_name: size_t
+ - type_name: rsize_t
- type_name: errno_t
- type_name: constraint_handler_t
enums: []
@@ -188,6 +189,22 @@ functions:
return_type: constraint_handler_t
arguments:
- type: constraint_handler_t
+ - name: abort_handler_s
+ standards:
+ - stdc
+ return_type: void
+ arguments:
+ - type: const char *__restrict
+ - type: void *__restrict
+ - type: errno_t
+ - name: ignore_handler_s
+ standards:
+ - stdc
+ return_type: void
+ arguments:
+ - type: const char *__restrict
+ - type: void *__restrict
+ - type: errno_t
- name: srand
standards:
- stdc
diff --git a/libc/src/__support/annex_k/CMakeLists.txt b/libc/src/__support/annex_k/CMakeLists.txt
index b3b472a8dbd16..8ab4296f41fe5 100644
--- a/libc/src/__support/annex_k/CMakeLists.txt
+++ b/libc/src/__support/annex_k/CMakeLists.txt
@@ -35,6 +35,6 @@ add_header_library(
HDRS
libc_constraint_handler.h
DEPENDS
- libc.src.__support.annex_k.abort_handler_s
+ .abort_handler_s
libc.hdr.types.errno_t
)
diff --git a/libc/src/__support/annex_k/abort_handler_s.h b/libc/src/__support/annex_k/abort_handler_s.h
index db3512bb0bca5..731f859c537ec 100644
--- a/libc/src/__support/annex_k/abort_handler_s.h
+++ b/libc/src/__support/annex_k/abort_handler_s.h
@@ -21,7 +21,8 @@
namespace LIBC_NAMESPACE_DECL {
LIBC_INLINE static void abort_handler_s(const char *__restrict msg,
- void *__restrict ptr, errno_t error) {
+ [[maybe_unused]] void *__restrict ptr,
+ errno_t error) {
libc_errno = error;
fprintf(stderr, "abort_handler_s was called in response to a "
"runtime-constraint violation.\n\n");
diff --git a/libc/src/__support/annex_k/helper_macros.h b/libc/src/__support/annex_k/helper_macros.h
index e11869549897e..b359acb95c32c 100644
--- a/libc/src/__support/annex_k/helper_macros.h
+++ b/libc/src/__support/annex_k/helper_macros.h
@@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_ANNEX_K_HELPER_MACROS_H
#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_HELPER_MACROS_H
-#include "libc_constraint_hander.h"
+#include "libc_constraint_handler.h"
#define _CONSTRAINT_VIOLATION(msg, error_code, ret_code) \
{ \
diff --git a/libc/src/__support/annex_k/libc_constraint_hander.h b/libc/src/__support/annex_k/libc_constraint_handler.h
similarity index 93%
rename from libc/src/__support/annex_k/libc_constraint_hander.h
rename to libc/src/__support/annex_k/libc_constraint_handler.h
index a21b966133104..8e52f72d04277 100644
--- a/libc/src/__support/annex_k/libc_constraint_hander.h
+++ b/libc/src/__support/annex_k/libc_constraint_handler.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDER_H
#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDER_H
+#include "abort_handler_s.h"
#include "hdr/types/constraint_handler_t.h"
-#include "src/__support/annex_k/abort_handler_s.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index 32810b41b8866..9bc51c27d8d8a 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -204,14 +204,6 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.fdopen
)
-add_entrypoint_object(
- fopen_s
- HDRS
- fopen_s.h
- DEPENDS
- .generic.fopen_s
-)
-
# These entrypoints have multiple potential implementations.
add_stdio_entrypoint_object(feof)
add_stdio_entrypoint_object(feof_unlocked)
@@ -226,6 +218,7 @@ add_stdio_entrypoint_object(fflush)
add_stdio_entrypoint_object(clearerr)
add_stdio_entrypoint_object(clearerr_unlocked)
add_stdio_entrypoint_object(fopen)
+add_stdio_entrypoint_object(fopen_s)
add_stdio_entrypoint_object(fclose)
add_stdio_entrypoint_object(fread_unlocked)
add_stdio_entrypoint_object(fread)
diff --git a/libc/src/stdlib/set_constraint_handler_s.cpp b/libc/src/stdlib/set_constraint_handler_s.cpp
index 5d215a58461be..7558b6b271e20 100644
--- a/libc/src/stdlib/set_constraint_handler_s.cpp
+++ b/libc/src/stdlib/set_constraint_handler_s.cpp
@@ -8,7 +8,7 @@
#include "set_constraint_handler_s.h"
#include "src/__support/annex_k/abort_handler_s.h"
-#include "src/__support/annex_k/libc_constraint_hander.h"
+#include "src/__support/annex_k/libc_constraint_handler.h"
#include "src/__support/common.h"
namespace LIBC_NAMESPACE_DECL {
>From 2728b97393cc1711f15103c553dc6973216d0e6a Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 8 Aug 2025 06:25:58 +0300
Subject: [PATCH 03/16] make fullbuild only
---
libc/config/linux/x86_64/entrypoints.txt | 1 -
1 file changed, 1 deletion(-)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index e3de356c13304..29b7df4f9fe34 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -198,7 +198,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.qsort
libc.src.stdlib.qsort_r
libc.src.stdlib.rand
- libc.src.stdlib.set_constraint_handler_s
libc.src.stdlib.srand
libc.src.stdlib.strfromd
libc.src.stdlib.strfromf
>From 80586f1909e2f541cfc6ac5ae72b21518d438c67 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 8 Aug 2025 06:56:29 +0300
Subject: [PATCH 04/16] remove calling public functions from internal code
---
libc/src/__support/annex_k/CMakeLists.txt | 4 +---
libc/src/__support/annex_k/abort_handler_s.h | 17 +++++++++--------
2 files changed, 10 insertions(+), 11 deletions(-)
diff --git a/libc/src/__support/annex_k/CMakeLists.txt b/libc/src/__support/annex_k/CMakeLists.txt
index 8ab4296f41fe5..6cbbe88bb5424 100644
--- a/libc/src/__support/annex_k/CMakeLists.txt
+++ b/libc/src/__support/annex_k/CMakeLists.txt
@@ -8,9 +8,7 @@ add_header_library(
libc.src.__support.macros.config
libc.src.__support.macros.attributes
libc.src.__support.File.file
- libc.src.stdio.fprintf
- libc.src.stdio.fflush
- libc.src.stdlib.abort
+ libc.src.__support.OSUtil.io
)
add_header_library(
diff --git a/libc/src/__support/annex_k/abort_handler_s.h b/libc/src/__support/annex_k/abort_handler_s.h
index 731f859c537ec..e496f44d60e26 100644
--- a/libc/src/__support/annex_k/abort_handler_s.h
+++ b/libc/src/__support/annex_k/abort_handler_s.h
@@ -14,9 +14,8 @@
#include "src/__support/libc_errno.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
-#include "src/stdio/fflush.h"
-#include "src/stdio/fprintf.h"
-#include "src/stdlib/abort.h"
+#include "src/__support/OSUtil/io.h"
+#include <stdlib.h>
namespace LIBC_NAMESPACE_DECL {
@@ -24,16 +23,18 @@ LIBC_INLINE static void abort_handler_s(const char *__restrict msg,
[[maybe_unused]] void *__restrict ptr,
errno_t error) {
libc_errno = error;
- fprintf(stderr, "abort_handler_s was called in response to a "
+ write_to_stderr("abort_handler_s was called in response to a "
"runtime-constraint violation.\n\n");
- if (msg)
- fprintf(stderr, "%s\n", msg);
- fprintf(stderr,
+ if (msg) {
+ write_to_stderr(msg);
+ write_to_stderr("\n");
+ }
+
+ 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");
- fflush(stderr);
abort();
}
>From 92b18c5255d0d886ad01115b0e7bed06c04f00f8 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 8 Aug 2025 12:17:24 +0300
Subject: [PATCH 05/16] add default handlers
---
libc/config/linux/x86_64/entrypoints.txt | 2 +
libc/include/llvm-libc-macros/CMakeLists.txt | 6 +++
.../include/llvm-libc-macros/annex-k-macros.h | 19 ++++++++
libc/src/__support/annex_k/CMakeLists.txt | 24 ----------
.../annex_k/libc_constraint_handler.h | 9 ++--
libc/src/stdlib/CMakeLists.txt | 44 ++++++++++++++++++-
.../abort_handler_s.cpp} | 32 ++++++--------
libc/src/stdlib/abort_handler_s.h | 22 ++++++++++
libc/src/stdlib/ignore_handler_s.cpp | 16 +++++++
.../annex_k => stdlib}/ignore_handler_s.h | 13 +++---
libc/src/stdlib/set_constraint_handler_s.cpp | 2 +-
11 files changed, 133 insertions(+), 56 deletions(-)
create mode 100644 libc/include/llvm-libc-macros/annex-k-macros.h
rename libc/src/{__support/annex_k/abort_handler_s.h => stdlib/abort_handler_s.cpp} (51%)
create mode 100644 libc/src/stdlib/abort_handler_s.h
create mode 100644 libc/src/stdlib/ignore_handler_s.cpp
rename libc/src/{__support/annex_k => stdlib}/ignore_handler_s.h (53%)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 29b7df4f9fe34..7d63f6a5017dc 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1168,6 +1168,8 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdlib.getenv
libc.src.stdlib.quick_exit
libc.src.stdlib.set_constraint_handler_s
+ libc.src.stdlib.abort_handler_s
+ libc.src.stdlib.ignore_handler_s
# signal.h entrypoints
libc.src.signal.kill
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 7aa549ddc75d9..30699c7e43ac8 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -31,6 +31,12 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
endif()
+add_macro_header(
+ annex_k_macros
+ HDR
+ annex-k-macros.h
+)
+
add_macro_header(
assert_macros
HDR
diff --git a/libc/include/llvm-libc-macros/annex-k-macros.h b/libc/include/llvm-libc-macros/annex-k-macros.h
new file mode 100644
index 0000000000000..02c8dc60301a9
--- /dev/null
+++ b/libc/include/llvm-libc-macros/annex-k-macros.h
@@ -0,0 +1,19 @@
+//===-- Definition of macros to be used with Annex K functions ------------===//
+//
+// 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_INCLUDE_LLVM_LIBC_MACROS_ANNEX_K_MACROS_H
+#define LLVM_LIBC_INCLUDE_LLVM_LIBC_MACROS_ANNEX_K_MACROS_H
+
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && \
+ defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ == 1
+
+#define LIBC_HAS_ANNEX_K
+
+#endif
+
+#endif // LLVM_LIBC_INCLUDE_LLVM_LIBC_MACROS_ANNEX_K_MACROS_H
diff --git a/libc/src/__support/annex_k/CMakeLists.txt b/libc/src/__support/annex_k/CMakeLists.txt
index 6cbbe88bb5424..646645696b5b4 100644
--- a/libc/src/__support/annex_k/CMakeLists.txt
+++ b/libc/src/__support/annex_k/CMakeLists.txt
@@ -1,27 +1,3 @@
-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.File.file
- libc.src.__support.OSUtil.io
-)
-
-add_header_library(
- ignore_handler_s
- HDRS
- ignore_handler_s.h
- DEPENDS
- libc.hdr.types.errno_t
- libc.src.__support.libc_errno
- libc.src.__support.macros.config
- libc.src.__support.macros.attributes
-)
-
add_header_library(
helper_macros
HDRS
diff --git a/libc/src/__support/annex_k/libc_constraint_handler.h b/libc/src/__support/annex_k/libc_constraint_handler.h
index 8e52f72d04277..b1c97cc225e0f 100644
--- a/libc/src/__support/annex_k/libc_constraint_handler.h
+++ b/libc/src/__support/annex_k/libc_constraint_handler.h
@@ -6,11 +6,12 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDER_H
-#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDER_H
+#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"
+#include "src/__support/common.h"
+#include "src/stdlib/abort_handler_s.h"
namespace LIBC_NAMESPACE_DECL {
@@ -19,4 +20,4 @@ LIBC_INLINE static constraint_handler_t libc_constraint_handler =
}
-#endif // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDER_H
+#endif // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_LIBC_CONSTRAINT_HANDLER_H
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 0165c799b7aa8..5cfead6ae8020 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -660,7 +660,49 @@ add_entrypoint_object(
HDRS
set_constraint_handler_s.h
DEPENDS
- libc.src.__support.annex_k.abort_handler_s
+ .abort_handler_s
libc.src.__support.annex_k.libc_constraint_handler
libc.src.__support.macros.config
)
+
+add_entrypoint_object(
+ abort_handler_s
+ SRCS
+ abort_handler_s.cpp
+ 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.File.file
+)
+
+add_header_library(
+ libc_constraint_handler
+ HDRS
+ libc_constraint_handler.h
+ DEPENDS
+ .abort_handler_s
+ libc.hdr.types.constraint_handler_t
+)
+
+add_header_library(
+ annex_k_helper_macros
+ HDRS
+ annex_k_helper_macros.h
+)
+
+add_entrypoint_object(
+ ignore_handler_s
+ HDRS
+ ignore_handler_s.h
+ SRCS
+ ignore_handler_s.cpp
+ DEPENDS
+ libc.hdr.types.errno_t
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.__support.macros.attributes
+)
diff --git a/libc/src/__support/annex_k/abort_handler_s.h b/libc/src/stdlib/abort_handler_s.cpp
similarity index 51%
rename from libc/src/__support/annex_k/abort_handler_s.h
rename to libc/src/stdlib/abort_handler_s.cpp
index e496f44d60e26..7a82b2d073d25 100644
--- a/libc/src/__support/annex_k/abort_handler_s.h
+++ b/libc/src/stdlib/abort_handler_s.cpp
@@ -1,4 +1,4 @@
-//===-- Implementation header for abort_handler_s ---------------*- C++ -*-===//
+//===-- 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.
@@ -6,38 +6,32 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC___SUPPORT_ANNEX_K_ABORT_HANDLER_S_H
-#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_ABORT_HANDLER_S_H
-
+#include "src/stdlib/abort_handler_s.h"
#include "hdr/stdio_macros.h"
#include "hdr/types/errno_t.h"
+#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
-#include "src/__support/macros/attributes.h"
-#include "src/__support/macros/config.h"
-#include "src/__support/OSUtil/io.h"
+#include <stdio.h>
#include <stdlib.h>
namespace LIBC_NAMESPACE_DECL {
-LIBC_INLINE static void abort_handler_s(const char *__restrict msg,
- [[maybe_unused]] void *__restrict ptr,
- errno_t error) {
+LLVM_LIBC_FUNCTION(void, abort_handler_s,
+ (const char *__restrict msg,
+ [[maybe_unused]] void *__restrict ptr, errno_t error)) {
libc_errno = error;
- write_to_stderr("abort_handler_s was called in response to a "
+ fprintf(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");
- }
-
- write_to_stderr(
+ if (msg)
+ fprintf(stderr, "%s\n", msg);
+ fprintf(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");
+ fflush(stderr);
+
abort();
}
} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_ABORT_HANDLER_S_H
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
diff --git a/libc/src/stdlib/ignore_handler_s.cpp b/libc/src/stdlib/ignore_handler_s.cpp
new file mode 100644
index 0000000000000..172b1bcdb7b30
--- /dev/null
+++ b/libc/src/stdlib/ignore_handler_s.cpp
@@ -0,0 +1,16 @@
+//===-- Implementation header for ignore_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/ignore_handler_s.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, ignore_handler_s,
+ (const char *__restrict, void *__restrict, errno_t)) {}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/annex_k/ignore_handler_s.h b/libc/src/stdlib/ignore_handler_s.h
similarity index 53%
rename from libc/src/__support/annex_k/ignore_handler_s.h
rename to libc/src/stdlib/ignore_handler_s.h
index 7690776429945..07328d4be01ce 100644
--- a/libc/src/__support/annex_k/ignore_handler_s.h
+++ b/libc/src/stdlib/ignore_handler_s.h
@@ -6,18 +6,17 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC___SUPPORT_ANNEX_K_IGNORE_HANDLER_S_H
-#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_IGNORE_HANDLER_S_H
+#ifndef LLVM_LIBC_SRC_STDLIB_IGNORE_HANDLER_S_H
+#define LLVM_LIBC_SRC_STDLIB_IGNORE_HANDLER_S_H
#include "hdr/types/errno_t.h"
-#include "src/__support/macros/attributes.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/common.h"
namespace LIBC_NAMESPACE_DECL {
-LIBC_INLINE static void ignore_handler_s(const char *__restrict msg,
- void *__restrict ptr, errno_t error) {}
+void ignore_handler_s(const char *__restrict msg, void *__restrict ptr,
+ errno_t error);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_IGNORE_HANDLER_S_H
+#endif // LLVM_LIBC_SRC_STDLIB_IGNORE_HANDLER_S_H
diff --git a/libc/src/stdlib/set_constraint_handler_s.cpp b/libc/src/stdlib/set_constraint_handler_s.cpp
index 7558b6b271e20..020989ee862f9 100644
--- a/libc/src/stdlib/set_constraint_handler_s.cpp
+++ b/libc/src/stdlib/set_constraint_handler_s.cpp
@@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//
#include "set_constraint_handler_s.h"
-#include "src/__support/annex_k/abort_handler_s.h"
+#include "abort_handler_s.h"
#include "src/__support/annex_k/libc_constraint_handler.h"
#include "src/__support/common.h"
>From f9f75a29af4f12df447f8d8760aec0dff1b34f5c Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 8 Aug 2025 13:09:33 +0300
Subject: [PATCH 06/16] guard fopen_s
---
libc/include/CMakeLists.txt | 1 +
libc/include/stdio.yaml | 3 +++
2 files changed, 4 insertions(+)
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 7d8186e215453..ac7fcbdfe5854 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -332,6 +332,7 @@ add_header_macro(
../libc/include/stdio.yaml
stdio.h
DEPENDS
+ .llvm-libc-macros.annex_k_macros
.llvm-libc-macros.file_seek_macros
.llvm-libc-macros.null_macro
.llvm-libc-macros.stdio_macros
diff --git a/libc/include/stdio.yaml b/libc/include/stdio.yaml
index 7674720b03b68..1a0d2ecd9a57d 100644
--- a/libc/include/stdio.yaml
+++ b/libc/include/stdio.yaml
@@ -9,6 +9,8 @@ macros:
macro_value: stdin
- macro_name: stderr
macro_value: stderr
+ - macro_name: LIBC_HAS_ANNEX_K
+ macro_header: annex-k-macros.h
types:
- type_name: size_t
- type_name: errno_t
@@ -127,6 +129,7 @@ functions:
- type: const char *
- type: const char *
- name: fopen_s
+ guard: LIBC_HAS_ANNEX_K
standards:
- stdc
return_type: errno_t
>From 21bbcceb7927096ae50f7ef33fed440ad8bd728d Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 8 Aug 2025 15:45:02 +0300
Subject: [PATCH 07/16] guard include headers with C11 and macro flag
---
libc/hdr/types/constraint_handler_t.h | 4 ++++
libc/hdr/types/errno_t.h | 4 ++++
libc/include/llvm-libc-types/constraint_handler_t.h | 4 ++++
libc/include/llvm-libc-types/errno_t.h | 4 ++++
libc/include/llvm-libc-types/rsize_t.h | 4 ++++
libc/include/stdlib.yaml | 5 +++++
libc/src/stdio/fopen_s.h | 2 +-
7 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/libc/hdr/types/constraint_handler_t.h b/libc/hdr/types/constraint_handler_t.h
index df7eaa7abf48b..ca7798b84228e 100644
--- a/libc/hdr/types/constraint_handler_t.h
+++ b/libc/hdr/types/constraint_handler_t.h
@@ -9,6 +9,10 @@
#ifndef LLVM_LIBC_HDR_TYPES_CONSTRAINT_HANDLER_T_H
#define LLVM_LIBC_HDR_TYPES_CONSTRAINT_HANDLER_T_H
+#define LIBC_HAS_ANNEX_K
+
#include "include/llvm-libc-types/constraint_handler_t.h"
+#undef LIBC_HAS_ANNEX_K
+
#endif // LLVM_LIBC_HDR_TYPES_CONSTRAINT_HANDLER_T_H
diff --git a/libc/hdr/types/errno_t.h b/libc/hdr/types/errno_t.h
index 8769819d81474..91706b05c9155 100644
--- a/libc/hdr/types/errno_t.h
+++ b/libc/hdr/types/errno_t.h
@@ -9,6 +9,10 @@
#ifndef LLVM_LIBC_HDR_TYPES_ERRNO_T_H
#define LLVM_LIBC_HDR_TYPES_ERRNO_T_H
+#define LIBC_HAS_ANNEX_K
+
#include "include/llvm-libc-types/errno_t.h"
+#undef LIBC_HAS_ANNEX_K
+
#endif // LLVM_LIBC_HDR_TYPES_ERRNO_T_H
diff --git a/libc/include/llvm-libc-types/constraint_handler_t.h b/libc/include/llvm-libc-types/constraint_handler_t.h
index b3dfeb67067f1..2c4d54c2163f6 100644
--- a/libc/include/llvm-libc-types/constraint_handler_t.h
+++ b/libc/include/llvm-libc-types/constraint_handler_t.h
@@ -11,7 +11,11 @@
#include "errno_t.h"
+#ifdef LIBC_HAS_ANNEX_K
+
typedef void (*constraint_handler_t)(const char *__restrict msg,
void *__restrict ptr, errno_t error);
+#endif // LIBC_HAS_ANNEX_K
+
#endif // LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_CONSTRAINT_HANDLER_T_H
diff --git a/libc/include/llvm-libc-types/errno_t.h b/libc/include/llvm-libc-types/errno_t.h
index 460f072dbd680..f99fe1266ccd1 100644
--- a/libc/include/llvm-libc-types/errno_t.h
+++ b/libc/include/llvm-libc-types/errno_t.h
@@ -9,6 +9,10 @@
#ifndef LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
#define LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
+#ifdef LIBC_HAS_ANNEX_K
+
typedef int errno_t;
+#endif // LIBC_HAS_ANNEX_K
+
#endif // LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
diff --git a/libc/include/llvm-libc-types/rsize_t.h b/libc/include/llvm-libc-types/rsize_t.h
index dca61c4f76a05..52f61d4a61091 100644
--- a/libc/include/llvm-libc-types/rsize_t.h
+++ b/libc/include/llvm-libc-types/rsize_t.h
@@ -9,6 +9,10 @@
#ifndef LLVM_LIBC_TYPES_RSIZE_T_H
#define LLVM_LIBC_TYPES_RSIZE_T_H
+#ifdef LIBC_HAS_ANNEX_K
+
typedef __SIZE_TYPE__ rsize_t;
+#endif // LIBC_HAS_ANNEX_K
+
#endif // LLVM_LIBC_TYPES_RSIZE_T_H
diff --git a/libc/include/stdlib.yaml b/libc/include/stdlib.yaml
index c225a53b87f39..0f56816bb92a7 100644
--- a/libc/include/stdlib.yaml
+++ b/libc/include/stdlib.yaml
@@ -5,6 +5,8 @@ standards:
merge_yaml_files:
- stdlib-malloc.yaml
macros:
+ - macro_name: LIBC_HAS_ANNEX_K
+ macro_header: annex-k-macros.h
- macro_name: NULL
macro_header: null-macro.h
types:
@@ -189,6 +191,7 @@ functions:
return_type: constraint_handler_t
arguments:
- type: constraint_handler_t
+ guard: LIBC_HAS_ANNEX_K
- name: abort_handler_s
standards:
- stdc
@@ -197,6 +200,7 @@ functions:
- type: const char *__restrict
- type: void *__restrict
- type: errno_t
+ guard: LIBC_HAS_ANNEX_K
- name: ignore_handler_s
standards:
- stdc
@@ -205,6 +209,7 @@ functions:
- type: const char *__restrict
- type: void *__restrict
- type: errno_t
+ guard: LIBC_HAS_ANNEX_K
- name: srand
standards:
- stdc
diff --git a/libc/src/stdio/fopen_s.h b/libc/src/stdio/fopen_s.h
index 19f3f9cf1ee7c..36a4445f9309e 100644
--- a/libc/src/stdio/fopen_s.h
+++ b/libc/src/stdio/fopen_s.h
@@ -10,7 +10,7 @@
#define LLVM_LIBC_SRC_STDIO_FOPEN_S_H
#include "hdr/types/FILE.h"
-#include "include/llvm-libc-types/errno_t.h"
+#include "hdr/types/errno_t.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
>From 1e8e8dfcbd226cf6727b98941c04c3616a5fcbcb Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 8 Aug 2025 17:32:11 +0300
Subject: [PATCH 08/16] fix empty naming
---
libc/include/stdlib.yaml | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/libc/include/stdlib.yaml b/libc/include/stdlib.yaml
index 0f56816bb92a7..531d01197e656 100644
--- a/libc/include/stdlib.yaml
+++ b/libc/include/stdlib.yaml
@@ -5,9 +5,9 @@ standards:
merge_yaml_files:
- stdlib-malloc.yaml
macros:
- - macro_name: LIBC_HAS_ANNEX_K
+ - macro_name: 'LIBC_HAS_ANNEX_K'
macro_header: annex-k-macros.h
- - macro_name: NULL
+ - macro_name: 'NULL'
macro_header: null-macro.h
types:
- type_name: __atexithandler_t
@@ -191,7 +191,7 @@ functions:
return_type: constraint_handler_t
arguments:
- type: constraint_handler_t
- guard: LIBC_HAS_ANNEX_K
+ guard: 'LIBC_HAS_ANNEX_K'
- name: abort_handler_s
standards:
- stdc
@@ -200,7 +200,7 @@ functions:
- type: const char *__restrict
- type: void *__restrict
- type: errno_t
- guard: LIBC_HAS_ANNEX_K
+ guard: 'LIBC_HAS_ANNEX_K'
- name: ignore_handler_s
standards:
- stdc
@@ -209,7 +209,7 @@ functions:
- type: const char *__restrict
- type: void *__restrict
- type: errno_t
- guard: LIBC_HAS_ANNEX_K
+ guard: 'LIBC_HAS_ANNEX_K'
- name: srand
standards:
- stdc
>From 15103c2688553b375155c91491ee97f0f4621026 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 8 Aug 2025 19:46:36 +0300
Subject: [PATCH 09/16] remove C11 requirement and add stdc macro support
---
libc/include/llvm-libc-macros/annex-k-macros.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/libc/include/llvm-libc-macros/annex-k-macros.h b/libc/include/llvm-libc-macros/annex-k-macros.h
index 02c8dc60301a9..ec7dd027bc922 100644
--- a/libc/include/llvm-libc-macros/annex-k-macros.h
+++ b/libc/include/llvm-libc-macros/annex-k-macros.h
@@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_INCLUDE_LLVM_LIBC_MACROS_ANNEX_K_MACROS_H
#define LLVM_LIBC_INCLUDE_LLVM_LIBC_MACROS_ANNEX_K_MACROS_H
-#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && \
- defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ == 1
+#define __STDC_LIB_EXT1__ 200509L
+
+#if defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ == 1
#define LIBC_HAS_ANNEX_K
>From 87806b7d36bd832ce55c0a55e724f34d347df4c3 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 8 Aug 2025 20:13:46 +0300
Subject: [PATCH 10/16] add linux targets
---
libc/config/linux/aarch64/entrypoints.txt | 4 ++++
libc/config/linux/arm/entrypoints.txt | 3 +++
libc/config/linux/riscv/entrypoints.txt | 4 ++++
libc/config/linux/x86_64/entrypoints.txt | 4 ++--
4 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index fbdf8fb621b0f..39b4f9319b956 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -970,6 +970,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.fileno
libc.src.stdio.flockfile
libc.src.stdio.fopen
+ libc.src.stdio.fopen_s
libc.src.stdio.fopencookie
libc.src.stdio.fputc
libc.src.stdio.fputs
@@ -1000,11 +1001,14 @@ 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
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/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index e3f5feecc896a..2a5f2e00d7b4c 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -146,12 +146,14 @@ set(TARGET_LIBC_ENTRYPOINTS
# stdlib.h entrypoints
libc.src.stdlib.abs
+ libc.src.stdlib.abort_handler_s
libc.src.stdlib.atof
libc.src.stdlib.atoi
libc.src.stdlib.atol
libc.src.stdlib.atoll
libc.src.stdlib.bsearch
libc.src.stdlib.div
+ libc.src.stdlib.ignore_handler_s
libc.src.stdlib.labs
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
@@ -159,6 +161,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.memalignment
libc.src.stdlib.qsort
libc.src.stdlib.qsort_r
+ libc.src.stdlib.set_constraint_handler_s
libc.src.stdlib.strtod
libc.src.stdlib.strtof
libc.src.stdlib.strtol
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index ba67dddbe8b98..0210cd9ddee5e 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1096,6 +1096,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.fileno
libc.src.stdio.flockfile
libc.src.stdio.fopen
+ libc.src.stdio.fopen_s
libc.src.stdio.fopencookie
libc.src.stdio.fputc
libc.src.stdio.fputs
@@ -1126,11 +1127,14 @@ 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
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 7d63f6a5017dc..78d95a3b57708 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1162,14 +1162,14 @@ 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
libc.src.stdlib.getenv
+ libc.src.stdlib.ignore_handler_s
libc.src.stdlib.quick_exit
libc.src.stdlib.set_constraint_handler_s
- libc.src.stdlib.abort_handler_s
- libc.src.stdlib.ignore_handler_s
# signal.h entrypoints
libc.src.signal.kill
>From f231ed968948ecf6afb6742bc392cb604a0ce8c5 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 8 Aug 2025 20:31:50 +0300
Subject: [PATCH 11/16] remove targets from arm
---
libc/config/linux/arm/entrypoints.txt | 3 ---
1 file changed, 3 deletions(-)
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 2a5f2e00d7b4c..e3f5feecc896a 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -146,14 +146,12 @@ set(TARGET_LIBC_ENTRYPOINTS
# stdlib.h entrypoints
libc.src.stdlib.abs
- libc.src.stdlib.abort_handler_s
libc.src.stdlib.atof
libc.src.stdlib.atoi
libc.src.stdlib.atol
libc.src.stdlib.atoll
libc.src.stdlib.bsearch
libc.src.stdlib.div
- libc.src.stdlib.ignore_handler_s
libc.src.stdlib.labs
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
@@ -161,7 +159,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.memalignment
libc.src.stdlib.qsort
libc.src.stdlib.qsort_r
- libc.src.stdlib.set_constraint_handler_s
libc.src.stdlib.strtod
libc.src.stdlib.strtof
libc.src.stdlib.strtol
>From 4824601bec0ef990a4b16aaee8d675da1198d90c Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 8 Aug 2025 20:53:46 +0300
Subject: [PATCH 12/16] remove handler args name
---
libc/include/llvm-libc-types/constraint_handler_t.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/include/llvm-libc-types/constraint_handler_t.h b/libc/include/llvm-libc-types/constraint_handler_t.h
index 2c4d54c2163f6..efd21296cd399 100644
--- a/libc/include/llvm-libc-types/constraint_handler_t.h
+++ b/libc/include/llvm-libc-types/constraint_handler_t.h
@@ -13,8 +13,8 @@
#ifdef LIBC_HAS_ANNEX_K
-typedef void (*constraint_handler_t)(const char *__restrict msg,
- void *__restrict ptr, errno_t error);
+typedef void (*constraint_handler_t)(const char *__restrict, void *__restrict,
+ errno_t);
#endif // LIBC_HAS_ANNEX_K
>From 178df7117eeff813bc52e92aba4735552a7ca31d Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 8 Aug 2025 21:11:55 +0300
Subject: [PATCH 13/16] restore C/C++ version check
---
libc/include/llvm-libc-macros/annex-k-macros.h | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/libc/include/llvm-libc-macros/annex-k-macros.h b/libc/include/llvm-libc-macros/annex-k-macros.h
index ec7dd027bc922..75e1fd52d1257 100644
--- a/libc/include/llvm-libc-macros/annex-k-macros.h
+++ b/libc/include/llvm-libc-macros/annex-k-macros.h
@@ -9,12 +9,17 @@
#ifndef LLVM_LIBC_INCLUDE_LLVM_LIBC_MACROS_ANNEX_K_MACROS_H
#define LLVM_LIBC_INCLUDE_LLVM_LIBC_MACROS_ANNEX_K_MACROS_H
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
+ (defined(__cplusplus) && __cplusplus >= 201703L)
+
#define __STDC_LIB_EXT1__ 200509L
#if defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ == 1
#define LIBC_HAS_ANNEX_K
-#endif
+#endif // defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ == 1
+#endif // (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) ||
+ // (defined(__cplusplus) && __cplusplus >= 201703L)
#endif // LLVM_LIBC_INCLUDE_LLVM_LIBC_MACROS_ANNEX_K_MACROS_H
>From 4c4b6bcf318599592b351a01b157f4ea4228586c Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 8 Aug 2025 21:35:38 +0300
Subject: [PATCH 14/16] make gcc happy
---
libc/src/__support/stdio/fopen.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/stdio/fopen.h b/libc/src/__support/stdio/fopen.h
index 738b7963db50c..cf4ff95b0939e 100644
--- a/libc/src/__support/stdio/fopen.h
+++ b/libc/src/__support/stdio/fopen.h
@@ -17,8 +17,8 @@ namespace LIBC_NAMESPACE_DECL {
namespace stdio_internal {
-LIBC_INLINE static constexpr ::FILE *fopen(const char *__restrict name,
- const char *__restrict mode) {
+LIBC_INLINE static ::FILE *fopen(const char *__restrict name,
+ const char *__restrict mode) {
auto result = LIBC_NAMESPACE::openfile(name, mode);
if (!result.has_value()) {
libc_errno = result.error();
>From c632701109954d2a0ad90ff1c9bc1673c6a6e13b Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sat, 9 Aug 2025 16:59:14 +0300
Subject: [PATCH 15/16] modify deps of abort_handler_s
---
libc/src/stdlib/CMakeLists.txt | 3 +++
libc/src/stdlib/abort_handler_s.cpp | 5 +++--
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 5cfead6ae8020..dd60ece834d77 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -677,6 +677,9 @@ add_entrypoint_object(
libc.src.__support.macros.config
libc.src.__support.macros.attributes
libc.src.__support.File.file
+ libc.src.stdio.fflush
+ libc.src.stdio.fprintf
+ libc.src.stdlib.abort
)
add_header_library(
diff --git a/libc/src/stdlib/abort_handler_s.cpp b/libc/src/stdlib/abort_handler_s.cpp
index 7a82b2d073d25..b34aaa1197257 100644
--- a/libc/src/stdlib/abort_handler_s.cpp
+++ b/libc/src/stdlib/abort_handler_s.cpp
@@ -11,8 +11,9 @@
#include "hdr/types/errno_t.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
-#include <stdio.h>
-#include <stdlib.h>
+#include "src/stdio/fflush.h"
+#include "src/stdio/fprintf.h"
+#include "src/stdlib/abort.h"
namespace LIBC_NAMESPACE_DECL {
>From ef58e7bf99c6e500c6670fc5c5f2f16828ceaf81 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sat, 9 Aug 2025 17:23:10 +0300
Subject: [PATCH 16/16] remove unnecessary indirection
---
libc/src/__support/CMakeLists.txt | 1 -
libc/src/__support/annex_k/CMakeLists.txt | 7 ++++-
libc/src/__support/annex_k/helper_macros.h | 1 +
libc/src/__support/stdio/CMakeLists.txt | 9 ------
libc/src/__support/stdio/fopen.h | 34 ----------------------
libc/src/stdio/generic/CMakeLists.txt | 6 ++--
libc/src/stdio/generic/fopen.cpp | 12 ++++++--
libc/src/stdio/generic/fopen_s.cpp | 6 ++--
8 files changed, 24 insertions(+), 52 deletions(-)
delete mode 100644 libc/src/__support/stdio/CMakeLists.txt
delete mode 100644 libc/src/__support/stdio/fopen.h
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index f2239e7d27ade..0534a3a95a6f4 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -1,7 +1,6 @@
add_subdirectory(annex_k)
add_subdirectory(CPP)
add_subdirectory(macros)
-add_subdirectory(stdio)
add_header_library(
libc_errno
diff --git a/libc/src/__support/annex_k/CMakeLists.txt b/libc/src/__support/annex_k/CMakeLists.txt
index 646645696b5b4..f6d2427609d3f 100644
--- a/libc/src/__support/annex_k/CMakeLists.txt
+++ b/libc/src/__support/annex_k/CMakeLists.txt
@@ -2,6 +2,9 @@ add_header_library(
helper_macros
HDRS
helper_macros.h
+ DEPENDS
+ .libc_constraint_handler
+ libc.src.__support.libc_errno
)
add_header_library(
@@ -10,5 +13,7 @@ add_header_library(
libc_constraint_handler.h
DEPENDS
.abort_handler_s
- libc.hdr.types.errno_t
+ libc.hdr.types.constraint_handler_t
+ libc.src.__support.common
+ libc.src.stdlib.abort_handler_s
)
diff --git a/libc/src/__support/annex_k/helper_macros.h b/libc/src/__support/annex_k/helper_macros.h
index b359acb95c32c..d37989cceafba 100644
--- a/libc/src/__support/annex_k/helper_macros.h
+++ b/libc/src/__support/annex_k/helper_macros.h
@@ -10,6 +10,7 @@
#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_HELPER_MACROS_H
#include "libc_constraint_handler.h"
+#include "src/__support/libc_errno.h"
#define _CONSTRAINT_VIOLATION(msg, error_code, ret_code) \
{ \
diff --git a/libc/src/__support/stdio/CMakeLists.txt b/libc/src/__support/stdio/CMakeLists.txt
deleted file mode 100644
index 3bb93b634c1a0..0000000000000
--- a/libc/src/__support/stdio/CMakeLists.txt
+++ /dev/null
@@ -1,9 +0,0 @@
-add_header_library(
- fopen
- HDRS
- fopen.h
- DEPENDS
- libc.hdr.types.FILE
- libc.src.__support.File.file
- libc.src.__support.libc_errno
-)
diff --git a/libc/src/__support/stdio/fopen.h b/libc/src/__support/stdio/fopen.h
deleted file mode 100644
index cf4ff95b0939e..0000000000000
--- a/libc/src/__support/stdio/fopen.h
+++ /dev/null
@@ -1,34 +0,0 @@
-//===-- Common implementation of fopen ------------------------------------===//
-//
-// 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_STDIO_FOPEN_H
-#define LLVM_LIBC_SRC___SUPPORT_STDIO_FOPEN_H
-
-#include "hdr/types/FILE.h"
-#include "src/__support/File/file.h"
-#include "src/__support/libc_errno.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-namespace stdio_internal {
-
-LIBC_INLINE static ::FILE *fopen(const char *__restrict name,
- const char *__restrict mode) {
- auto result = LIBC_NAMESPACE::openfile(name, mode);
- if (!result.has_value()) {
- libc_errno = result.error();
- return nullptr;
- }
- return reinterpret_cast<::FILE *>(result.value());
-}
-
-} // namespace stdio_internal
-
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SRC___SUPPORT_STDIO_FOPEN_H
diff --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt
index 0898ae83a76ac..96aff17d09609 100644
--- a/libc/src/stdio/generic/CMakeLists.txt
+++ b/libc/src/stdio/generic/CMakeLists.txt
@@ -162,7 +162,9 @@ add_generic_entrypoint_object(
HDRS
../fopen.h
DEPENDS
- libc.src.__support.stdio.fopen
+ libc.hdr.types.FILE
+ libc.src.__support.File.file
+ libc.src.__support.File.platform_file
)
add_generic_entrypoint_object(
@@ -172,7 +174,7 @@ add_generic_entrypoint_object(
HDRS
../fopen_s.h
DEPENDS
- libc.src.__support.stdio.fopen
+ libc.src.stdio.fopen
libc.src.__support.macros.config
libc.src.__support.annex_k.helper_macros
)
diff --git a/libc/src/stdio/generic/fopen.cpp b/libc/src/stdio/generic/fopen.cpp
index a1db51a00bde2..57c85c2e54e16 100644
--- a/libc/src/stdio/generic/fopen.cpp
+++ b/libc/src/stdio/generic/fopen.cpp
@@ -7,14 +7,22 @@
//===----------------------------------------------------------------------===//
#include "src/stdio/fopen.h"
+#include "src/__support/File/file.h"
+
+#include "hdr/types/FILE.h"
+#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
-#include "src/__support/stdio/fopen.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(::FILE *, fopen,
(const char *__restrict name, const char *__restrict mode)) {
- return stdio_internal::fopen(name, mode);
+ auto result = LIBC_NAMESPACE::openfile(name, mode);
+ if (!result.has_value()) {
+ libc_errno = result.error();
+ return nullptr;
+ }
+ return reinterpret_cast<::FILE *>(result.value());
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/generic/fopen_s.cpp b/libc/src/stdio/generic/fopen_s.cpp
index 546218ff76f27..b7de9639eac9c 100644
--- a/libc/src/stdio/generic/fopen_s.cpp
+++ b/libc/src/stdio/generic/fopen_s.cpp
@@ -7,9 +7,9 @@
//===----------------------------------------------------------------------===//
#include "src/stdio/fopen_s.h"
+#include "src/stdio/fopen.h"
#include "src/__support/annex_k/helper_macros.h"
#include "src/__support/macros/config.h"
-#include "src/__support/stdio/fopen.h"
namespace LIBC_NAMESPACE_DECL {
@@ -24,13 +24,13 @@ LLVM_LIBC_FUNCTION(errno_t, fopen_s,
FILE *ret = nullptr;
if (mode[0] == 'u') {
- ret = stdio_internal::fopen(filename, mode + 1);
+ ret = fopen(filename, mode + 1);
if (!ret) {
*streamptr = nullptr;
return -1;
}
} else {
- ret = stdio_internal::fopen(filename, mode);
+ ret = fopen(filename, mode);
if (!ret) {
*streamptr = nullptr;
return -1;
More information about the libc-commits
mailing list