From 8e062105593bee87b89ca99778b30e5695d398d7 Mon Sep 17 00:00:00 2001 From: Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com> Date: Wed, 18 Mar 2026 23:58:27 +0200 Subject: [PATCH] [libc][annex_k] Add errno_t (#163094) RFC https://discourse.llvm.org/t/rfc-bounds-checking-interfaces-for-llvm-libc/87685 Add `errno_t` type required by Annex K interface in LLVM libc. --- libc/hdr/types/CMakeLists.txt | 8 ++++++ libc/hdr/types/errno_t.h | 30 +++++++++++++++++++++ libc/include/CMakeLists.txt | 1 + libc/include/errno.yaml | 3 ++- libc/include/llvm-libc-types/CMakeLists.txt | 1 + libc/include/llvm-libc-types/errno_t.h | 26 ++++++++++++++++++ 6 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 libc/hdr/types/errno_t.h create mode 100644 libc/include/llvm-libc-types/errno_t.h diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt index dec73b0665f0..ba99a02a1011 100644 --- a/libc/hdr/types/CMakeLists.txt +++ b/libc/hdr/types/CMakeLists.txt @@ -162,6 +162,14 @@ 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 +) + add_proxy_header_library( fenv_t HDRS diff --git a/libc/hdr/types/errno_t.h b/libc/hdr/types/errno_t.h new file mode 100644 index 000000000000..d83692a6585d --- /dev/null +++ b/libc/hdr/types/errno_t.h @@ -0,0 +1,30 @@ +//===-- 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 + +#ifndef __STDC_WANT_LIB_EXT1__ +#define __STDC_WANT_LIB_EXT1__ 1 +#endif + +#ifdef LIBC_FULL_BUILD + +#include "include/llvm-libc-types/errno_t.h" + +#else // Overlay mode + +#include + +#endif // LIBC_FULL_BUILD + +#ifdef __STDC_WANT_LIB_EXT1__ +#undef __STDC_WANT_LIB_EXT1__ +#endif + +#endif // LLVM_LIBC_HDR_TYPES_ERRNO_T_H diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt index b2ebd035872a..9eae20c820e9 100644 --- a/libc/include/CMakeLists.txt +++ b/libc/include/CMakeLists.txt @@ -318,6 +318,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( diff --git a/libc/include/errno.yaml b/libc/include/errno.yaml index 188a9fa1211a..86afd2f03b20 100644 --- a/libc/include/errno.yaml +++ b/libc/include/errno.yaml @@ -5,7 +5,8 @@ standards: - Linux - POSIX macros: [] -types: [] +types: + - type_name: errno_t enums: [] objects: [] functions: [] diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt index 5d82660fd676..09f9d759f0dd 100644 --- a/libc/include/llvm-libc-types/CMakeLists.txt +++ b/libc/include/llvm-libc-types/CMakeLists.txt @@ -29,6 +29,7 @@ add_header(double_t HDR double_t.h) add_header(DIR HDR DIR.h) add_header(dev_t HDR dev_t.h) add_header(div_t HDR div_t.h) +add_header(errno_t HDR errno_t.h DEPENDS libc.include.llvm-libc-macros.annex_k_macros) add_header(ldiv_t HDR ldiv_t.h) add_header(lldiv_t HDR lldiv_t.h) add_header(FILE HDR FILE.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 000000000000..4ce694959ef2 --- /dev/null +++ b/libc/include/llvm-libc-types/errno_t.h @@ -0,0 +1,26 @@ +//===-- 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 + +#include "../llvm-libc-macros/annex-k-macros.h" + +// LIBC_HAS_ANNEX_K is used to check whether C11 Annex K (the optional +// “Bounds-checking interfaces”) is enabled in this libc implementation. Annex K +// introduces additional types and functions, including `errno_t` (a typedef +// used by the *_s functions). Since `errno_t` is *not defined* in the standard +// library unless Annex K is enabled, we must guard any code that uses it with +// LIBC_HAS_ANNEX_K. +#ifdef LIBC_HAS_ANNEX_K + +typedef int errno_t; + +#endif // LIBC_HAS_ANNEX_K + +#endif // LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H