4706 Commits

Author SHA1 Message Date
Marcell Leleszi
d8e56988f8
[libc] Add printf error handling (with fixes) (#166382)
https://github.com/llvm/llvm-project/issues/159474

Resubmitting https://github.com/llvm/llvm-project/pull/162876 with fixes
as it broke some buildbots:
 - Fix comparisons of integer expressions of different signedness
- Not check for specific errnos in tests, as they might not be available
on all platforms
2025-11-04 16:20:51 -08:00
Shubh Pachchigar
0fd029858a
[libc] Add chown and getgid implementations (#166434)
Implements chown and getgid per the POSIX specification and adds
corresponding unit tests. getgid is added as it is required by the chown
unit tests. This PR will address #165785

Co-authored-by: shubh@DOE <shubhp@mbm3a24.local>
2025-11-04 16:18:53 -08:00
Marcell Leleszi
a02e574011
[libc] Add faccessat entrypoints for aarch64 and riscv (#165869)
Add faccessat entrypoints for aarch64 and riscv linux. Entrypoints are
removed if faccessat2 syscall is not available.
2025-11-04 09:41:01 -08:00
Kewen Meng
346da3dfd3
Revert "[libc] Add printf error handling" (#166232) 2025-11-03 12:50:11 -08:00
Marcell Leleszi
0c707c9713
[libc] Add printf error handling (#162876)
[#159474](https://github.com/llvm/llvm-project/issues/159474)

- All printf variants set errno and consistently return -1 on error,
instead of returning various predefined error codes
- Return value overflow handling is added
2025-11-03 11:15:14 -08:00
Joseph Huber
9c26170dd7 [libc] Fix Linux kernel headers being included on all OS's
Summary:
The changes in
43bd7e3bb9
altered how we handled including headers, this included the system on
the GPU target which poisoned the include path that was curated to not
include any system headers. Change this to only apply is the target OS
is Linux.
2025-11-03 08:24:41 -06:00
Alexey Samsonov
315dfe5865
[libc] Templatize strtointeger implementation. (#165884)
* Removes the copy-pasta implementation of wcstointeger,
  and migrate the wcsto* family of functions to use a template
  version of strtointeger.
* Fixes the out-of-bound read in the original implementation(s)
  when the entire input string consists of whitespaces
  (then the sign check can access OOB memory)

The code is currently slightly peppered with "if constexpr" statements
to distinguish between char and wchar_t. We can probably
simplify it in subsequent changes by:
* using overrides, so that internal::isalnum() is overriden for
  both char and wchar_t (since C++ luckily allows us to reuse names).
* this wouldn't help for direct comparison with literals -
for this as a somewhat ugly workaround like is_char_literal(c, '0',
L'0')
2025-10-31 18:57:08 +00:00
Alexey Samsonov
42a8ff877d
[libc] Add "struct tm" declaration to <wchar.h> (#165795)
`<wchar.h>` should at least include the forward declaration of `struct tm`,
since it's needed for the `wcsftime` declaration (also, see
https://man7.org/linux/man-pages/man0/wchar.h.0p.html).

Even though we don't yet have `wcsftime`, some downstream users (notably
- libcxx) expects to see `struct tm` declaration there, to re-declare it under
`std` namespace:
c46bfed1a4/libcxx/include/cwchar (L135)

So, add this type declaration to llvm-libc version of `wchar.h` now.
2025-10-30 20:30:49 -07:00
Alexey Samsonov
45b1a4bb8d
Add <stdbool.h> to llvm-libc-types headers that need it. (#165798)
We need `<stdbool.h>` to support having "bool" members inside pthread
structs that may get included through `<pthread.h>` from C code prior to
C23.
2025-10-30 16:37:00 -07:00
Michael Jones
f7a21a837c
[libc] Remove optimization flags on entrypoints (#165782)
Optimization flags are now handled through a common flag. These are no
longer necessary.

Fixes #112409
2025-10-30 15:52:50 -07:00
Michael Jones
39189c3e99
[libc] Fix strftime_test (#165770)
A typo in #165711 caused sanitizer failures (the small buffer was used
for the larger test). Renamed the variables to avoid the mistake in
future.
2025-10-30 19:36:51 +00:00
Roland McGrath
a24a7548b8
[libc][hdrgen] Sort identifiers with leading underscores specially (#165745)
This makes the sorting behavior more uniform: functions and
macros are always sorted (separately), not only when merging.

This changes the sort order used for functions and other things
sorted by their symbol names.  Symbols are sorted alphabetically
without regard to leading underscores, and then for identifiers
that differ only in the number of leading underscores, the fewer
underscores the earlier in the sort order.  For the functions
declared in a generated header, adjacent names with and without
underscores will be grouped together without blank lines.

This is implemented by factoring the name field, equality, and
sorting support out of the various entity classes into a new
common superclass (hdrgen.Symbol).

This uncovered YAML's requirement to quote the string "NULL" to
avoid pyyaml parsing it as None (equivalent to Javascript null)
rather than a string.
2025-10-30 11:49:22 -07:00
Marcell Leleszi
784b74c6ef
[libc] Fix off by one error in strftime (#165711)
This patch fixes a bug in strftime's return value when the formatted
output exactly fills the buffer, not including the null terminator. The
previous check failed to account for the null terminator in this case,
incorrectly returning the written count instead of 0.
2025-10-30 10:35:42 -07:00
Marcell Leleszi
b1acd6df03
[libc] Remove faccessat entrypoint if faccessat2 syscall is not available (#164936)
[#163091](https://github.com/llvm/llvm-project/issues/163091)

Remove unistd.faccessat entrypoint for x86 linux if faccessat2 syscall
is not available. Tested with non existent symbol and exclusion works.
2025-10-30 10:35:20 -07:00
Roland McGrath
22079e3f36
[libc][hdrgen] Add extra_standards and license_text (#165459)
This adds a few new features to hdrgen, all meant to facilitate
using it with inputs and outputs that are outside the llvm-libc
source tree.

The new `extra_standards` field is a dictionary to augment the
set of names that can be used in `standards` lists.  The keys are
the identifiers used in YAML ("stdc") and the values are the
pretty names generated in the header comments ("Standard C").
This lets a libc project that's leveraging the llvm-libc sources
along with its own code define new APIs outside the formal and de
facto standards that llvm-libc draws its supported APIs from.

The new `license_text` field is a list of lines of license text
that replaces the standard LLVM license text used at the top of
each generated header.  This lets other projects use hdrgen with
their own inputs to produce generated headers that are not tied
to the LLVM project.

Finally, for any function attributes that are not in a canonical
list known to be provided by __llvm-libc-common.h, an include
will be generated for "llvm-libc-macros/{attribute name}.h",
expecting that file to define the "attribute" name as a macro.

All this can be used immediately by builds that drive hdrgen and
build libc code outside the LLVM CMake build.  Future changes
could add CMake plumbing to facilitate augmenting the LLVM CMake
build of libc with outside sources via overlays and cache files.
2025-10-30 10:00:51 -07:00
Roland McGrath
10afda0796
[libc][hdrgen] Fix includes sorting in JSON emission (#165460)
The JSON output support in hdrgen had a bug that tripped when
used with headers that use special-case headers like <stdint.h>
to supply some times, as well as llvm-libc-types/*.h headers.
2025-10-28 13:16:20 -07:00
Sterling-Augustine
7d1538cd3d
Move LIBC_CONF_STRING_UNSAFE_WIDE_READ to top-level libc-configuration (#165046)
This options sets a compile option when building sources inside the
string directory, and this option affects string_utils.h. But
string_utils.h is #included from more places than just the string
directory (such as from __support/CPP/string.h), leading to both
narrow-reads in those cases, but more seriously, ODR violations when the
two different string_length implementations are included int he same
program.

Having this option at the top level avoids this problem.
2025-10-27 14:39:46 -07:00
Shreeyash Pandey
51427079f7
[libc] fix architecture guarding for 32bit sigsetjmp (#164923)
Fixes: https://github.com/llvm/llvm-project/issues/164653

Signed-off-by: Shreeyash Pandey <shreeyash335@gmail.com>
2025-10-26 16:40:01 -04:00
Connector Switch
30f9ce14b4
[libc] add SIG_HOLD for linux/gpu (#165007) 2025-10-25 12:34:03 +08:00
Shreeyash Pandey
a1dc546f7f
[libc] add missing headers in stdfix (#162078)
Fixes https://github.com/llvm/llvm-project/issues/129361

@michaelrj-google @PiJoules

---------

Signed-off-by: Shreeyash Pandey <shreeyash335@gmail.com>
Co-authored-by: Michael Jones <michaelrj@google.com>
2025-10-24 10:42:31 -07:00
lntue
c597a4f476
Revert "[libc] Add -Werror for libc tests" (#164684)
Reverts llvm/llvm-project#160413
2025-10-22 19:05:10 +00:00
Vinay Deshmukh
5d6c00cd0f
[libc] Add -Werror for libc tests (#160413)
Relates to https://github.com/llvm/llvm-project/issues/119281

Note:

1) As this PR enables `-Werror` for `libc` tests, it's very likely some
downstream CI's may fail / start failing, so it's very likely this PR
may need to be reverted and re-applied.

P.S.

I do not have merge permissions, so I will need one of the reviews to
merge it for me. Thank you!
2025-10-22 14:32:21 -04:00
Muhammad Bassiouni
58d4f18067
[libc][annex_k] Add Annex K support macros. (#163100)
RFC https://discourse.llvm.org/t/rfc-bounds-checking-interfaces-for-llvm-libc/87685

Add internal support macros required by Annex K interface in LLVM libc.
2025-10-22 21:25:27 +03:00
Alexey Samsonov
9fc353ee36
[libc] Fix a couple issues in <wchar.h> header (#164666)
* Add FILE type declaration, as it should be presented in `<wchar.h>`,
as well as in `<stdio.h>`
* Fix argument type in `wcsrtombs` / `wcsnrtombs` function - it should
be restrict pointer to `mbstate_t`. Add restrict qualifier to internal
implementation as well.

This brings us closer to being able to build libcxx with wide-character
support against llvm-libc headers.
2025-10-22 11:09:42 -07:00
Alexey Samsonov
57a8228f40
[libc] Add macros definitions for <nl_types.h> (#164474)
This PR adds required macro definitions to `<nl_types.h>` header, so
that they're available to the code including it.

The header itself was added in 12abe8aed6dc724546cf93c94d7ff6abe864f28b,
together with stub definitions of the three required functions.
2025-10-22 09:41:03 -07:00
lntue
2bc4ad35e7
[libc][math] Add tolerance to math tests so that they still work when accurate path is skipped. (#164522) 2025-10-21 19:09:41 -05:00
Alexey Samsonov
12abe8aed6
[libc] Stub out message catalog functions from <nl_types.h> (#164360)
Create a POSIX `<nl_types.h>` header with `catopen`, `catclose`, and
`catgets` function declarations.
Provide the stub/placeholder implementations which always return error.
This is consistent with the way
locales are currently (un-)implemented in llvm-libc.

Notably, providing `<nl_types.h>` fixes the last remaining issue with
building libc++ against llvm-libc
(on certain configuration of x86_64 Linux) after disabling threads and
wide-characters in libc++.
2025-10-21 10:34:35 -07:00
lntue
513f9c50e7
[libc][math] Add float-only implementation for sinf / cosf. (#161680)
Based on the double precision's sin/cos fast path algorithm:

Step 1: Perform range reduction `y = x mod pi/8` with target errors <
2^-54.
This is because the worst case mod pi/8 for single precision is ~2^-31,
so to have up to 1 ULP errors from
the range reduction, the targeted errors should `be 2^(-31 - 23) =
2^-54`.

Step 2: Polynomial approximation
We use degree-5 and degree-4 polynomials to approximate sin and cos of
the reduced angle respectively.

Step 3: Combine the results using trig identities
```math
\begin{align*}
  \sin(x) &= \sin(y) \cdot \cos(k \cdot \frac{\pi}{8}) + \cos(y) \cdot \sin(k \cdot \frac{\pi}{8}) \\
  \cos(x) &= \cos(y) \cdot \cos(k \cdot \frac{\pi}{8}) - \sin(y) \cdot \sin(k \cdot \frac{\pi}{8})
\end{align*}
```

Overall errors: <= 3 ULPs for default rounding modes (tested
exhaustively).

Current limitation: large range reduction requires FMA instructions for
binary32. This restriction will be removed in the followup PR.

---------

Co-authored-by: Petr Hosek <phosek@google.com>
2025-10-21 13:33:09 -04:00
Connector Switch
5b3416caf9
[libc] implement inet_aton (#162651)
This patch adds the implementation for `inet_aton` function. Since this
function is not explicitly included in POSIX, I have marked it with
`llvm_libc_ext`. It is widely available and commonly used, and can also
be used to implement `inet_addr`, which is included in POSIX.
2025-10-21 23:47:38 +08:00
Simon Tatham
48a99ad1fa
[libc] Add llvm-libc-types header deps for <complex.h>. (#164422)
complex.yaml declares types cfloat16, cfloat128 and float128 in its
'types' section, causing the output complex.h to #include three headers
with names of the form "llvm-libc-types/cfloat16.h". But
include/CMakeLists.txt doesn't mention those types as dependencies in
the `add_header_macro` call for complex.h. As a result, cfloat16.h and
cfloat128.h are not installed by the 'install' target, but complex.h
still tries to to include them.

The third type header, float128.h, doesn't have this problem because the
`add_header_macro` math.h does mention it as a dependency. So I've added
all three headers as dependencies in complex.h, following the same
pattern, and now the install target installs the missing two headers.
2025-10-21 15:47:54 +01:00
Ren Hiyama
fedbb0f7e1
[libc] Capture 'POLY_COEFFS' by reference in asinpi_polyeval lambda. (#164404)
Closes #164403

GCC fails to compile libc currently. This PR fixes this by capturing
'POLY_COEFFS' by reference.
2025-10-21 10:40:00 -04:00
Schrodinger ZHU Yifan
2505df0a4f
Revert "[libc] add basic lifetime annotations for support data structures" (#164012)
Reverts llvm/llvm-project#145933 due to broken aarch64 buildbots.
2025-10-17 16:35:14 -04:00
Schrodinger ZHU Yifan
c0e0a33f48
[libc] add basic lifetime annotations for support data structures (#145933)
fix #145932

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-17 16:10:44 -04:00
Connector Switch
404099dcf2
[libc][NFC] Remove cpp20 requirement for arpa/inet test (#163983)
Address
https://github.com/llvm/llvm-project/pull/162651#discussion_r2417151569.
2025-10-17 15:56:11 +00:00
Connector Switch
c4ceb8d7e5
[libc] add IPV6 related macros (#162219) 2025-10-17 23:41:52 +08:00
Muhammad Bassiouni
a55c4c812e
[libc][math] Refactor exp2m1f implementation to header-only in src/__support/math folder. (#162017)
Part of #147386

in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
2025-10-17 18:27:51 +03:00
Petr Hosek
08c115d1f8
[libc] Enable intermediate computation in float for baremetal (#163829)
This can improve performance on 32-bit baremetal targets.
2025-10-16 11:57:00 -07:00
Petr Hosek
543932f034
[libc] Make LIBC_MATH_NO_ERRNO imply -fno-math-errno (#125794)
This partially reverts #124200. Rather than using a CMake option to
control whether to enable `-fno-math-errno`, use LIBC_MATH_NO_ERRNO
configuration option. While there might be other cases when we want to
set `-fno-math-errno`, having LIBC_MATH_NO_ERRNO imply it should be
always safe and represents a reasonable starting point.
2025-10-16 10:00:45 -07:00
lntue
e6afe2a854
[libc] Fix temporary types for fputil::split template. (#163813)
Fix #163711.
2025-10-16 12:56:09 -04:00
Petr Hosek
d50423e182
Revert "[libc] Enable intermediate computation in float for baremetal" (#163712)
Reverts llvm/llvm-project#163622, see #163711 for details.
2025-10-15 23:46:53 -07:00
Alexey Samsonov
36f7f3442c
[libc] Update generated libc header to include missing functions/files. (#163658)
* Add a missing dependency to install struct_itimerval.h
* Add fseeko/ftello declarations to stdio.h
2025-10-15 18:06:13 -07:00
Petr Hosek
49f03eed05
[libc] Enable intermediate computation in float for baremetal (#163622)
This can improve performance on 32-bit baremetal targets.
2025-10-15 16:33:55 -07:00
Peter Collingbourne
72c6e4b230
libc: Introduce calls to sysconf to get page size.
sysconf(_SC_PAGESIZE) is implemented now.

Reviewers: michaelrj-google, aaronmondal, rupprecht, keith

Reviewed By: michaelrj-google

Pull Request: https://github.com/llvm/llvm-project/pull/163462
2025-10-15 12:31:26 -07:00
Petr Hosek
24a4ad8941
[libc] Disable null checks on baremetal platforms (#163515)
On baremetal platforms, 0 is often a valid address and the null checks
can break otherwise correct code.
2025-10-15 10:06:24 -07:00
Guillaume Chatelet
57726bdca2
Revert "[libc] Implement branchless head-tail comparison for bcmp" (#162859)
Reverts llvm/llvm-project#107540

This PR demonstrated improvements on micro-benchmarks but the gains did
not seem to materialize in production. We are reverting this change for
now to get more data. This PR might be reintegrated later once we're
more confident in its effects.
2025-10-13 23:25:42 +02:00
Peter Collingbourne
7905ec387e
[libc] Use UMAXV.4S to reduce bcmp result.
We can use UMAXV.4S to reduce the comparison result in a single
instruction. This improves performance by roughly 4% on Apple M1:

Summary
  bin/libc.src.string.bcmp_benchmark3 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10 ran
    1.01 ± 0.02 times faster than bin/libc.src.string.bcmp_benchmark3 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.01 ± 0.03 times faster than bin/libc.src.string.bcmp_benchmark3 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.01 ± 0.03 times faster than bin/libc.src.string.bcmp_benchmark3 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.01 ± 0.02 times faster than bin/libc.src.string.bcmp_benchmark2 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.02 ± 0.03 times faster than bin/libc.src.string.bcmp_benchmark2 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.03 ± 0.03 times faster than bin/libc.src.string.bcmp_benchmark2 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.03 ± 0.03 times faster than bin/libc.src.string.bcmp_benchmark2 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.05 ± 0.02 times faster than bin/libc.src.string.bcmp_benchmark1 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.05 ± 0.02 times faster than bin/libc.src.string.bcmp_benchmark1 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.05 ± 0.03 times faster than bin/libc.src.string.bcmp_benchmark1 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.05 ± 0.02 times faster than bin/libc.src.string.bcmp_benchmark1 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10

(1 = original, 2 = a variant of this patch that uses UMAXV.16B, 3 = this patch)

Reviewers: michaelrj-google, gchatelet, overmighty, SchrodingerZhu

Pull Request: https://github.com/llvm/llvm-project/pull/99260
2025-10-13 11:21:48 -07:00
Muhammad Bassiouni
878527651a
[libc][math] Refactor exp2f16 implementation to header-only in src/__support/math folder. (#161993)
Part of #147386

in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
2025-10-11 23:27:00 +03:00
Joseph Huber
c18de24d9d
[libc] Add a config option to disable slab reclaiming (#151599)
Summary:
Without slab reclaiming this interface is much simpler and it can speed
up cases with a lot of churn. Basically, wastes memory for performance.
2025-10-10 21:05:51 -05:00
Schrodinger ZHU Yifan
e07914b563
[libc] fix sysconf test for rv32 (#162685) 2025-10-09 14:26:07 -04:00
Jeff Bailey
42d26a185f
[libc][docs] Update VSCode instructions for full host build (#162626)
The VSCode instructions were stale from the transition to the runtimes
directory. This updates will all the options give on the Full Host Build
page.

Tested:
Built libc target.
2025-10-09 17:10:47 +00:00