2949 Commits

Author SHA1 Message Date
Jan Patrick Lehr
ccbf908b08
[libc] Fix GPU test build error (#92235)
This fixes a build error on the AMDGPU buildbot introduced in PR
https://github.com/llvm/llvm-project/pull/92172
2024-05-15 07:06:40 -05:00
Robin Caloudis
0980f715cf
[libc][errno] Remove previously added errno numbers (#92163)
Introduced in https://github.com/llvm/llvm-project/pull/91150. Not
needed anymore as https://github.com/llvm/llvm-project/pull/92041 fixed
the root cause. `ENAMETOOLONG` and `EOVERFLOW` are well defined in
`<linux/errno.h>`.

Post mortem: Due to the previously missing inclusion of
`<linux/errno.h>` (fixed with
https://github.com/llvm/llvm-project/pull/92041), I misinterpreted an
undefined macro issue during the development of
https://github.com/llvm/llvm-project/pull/91150 as being caused by a
missing definition rather than by the missing inclusion of the linux
header. I realized too late that `ENAMETOOLONG` and `EOVERFLOW` were
correctly defined in `<linux/errno.h>` and that it was my missing
inclusion that caused the problem.
2024-05-15 01:26:23 -04:00
Cyuria
d3455f4ddd
[libc][docs] Fix outdated code review section, as per #91934 (#92051)
As in the title, fixes #91934
2024-05-15 00:13:05 -04:00
Robin Caloudis
a71e2b9d0f
[libc][errno] Remove non asm generic error number (#92172)
The following small thing caught my eye:

1) `EILSEQ` is not part of the generic asm error number macros. See the
[full list of generic asm errno
codes](4b95dc8736/include/uapi/asm-generic/errno-base.h).
AFAIK the generic asm errno numbers are common between different
operating systems and architectures. `EILSEQ` is not part of this common
set of errno's.

2) `EILSEQ`'s value is wrong. During the addition of `EILSEQ` in
https://reviews.llvm.org/D151129, the value `35` was probably chosen as
its the consecutive number. This is not correct. The actual values can
be looked up for example here:
* [For Linux
kernel](https://github.com/search?q=repo%3Atorvalds%2Flinux+EILSEQ&type=code&p=1):
`EILSEQ = 84` (uapi; i.e. x86_64), `EILSEQ = 88` (mips), `EILSEQ = 47`
(parisc)
* [For Darwin
kernel](https://github.com/apple-oss-distributions/xnu/blob/main/bsd/sys/errno.h#L237):
`EILSEQ = 92`
2024-05-15 00:02:46 -04:00
Guillaume Chatelet
292b300c51
[libc][bug] Fix out of bound write in memcpy w/ software prefetching (#90591)
This patch adds tests for `memcpy` and `memset` making sure that we
don't access buffers out of bounds. It relies on POSIX `mmap` /
`mprotect` and works only when FULL_BUILD_MODE is disabled.

The bug showed up while enabling software prefetching.
`loop_and_tail_offset` is always running at least one iteration but in
some configurations loop unrolled prefetching was actually needing only
the tail operation and no loop iterations at all.
2024-05-14 13:55:24 +02:00
Robin Caloudis
cd45bb2e43
[libc][errno] Remove unnecessary include (#92063)
Since https://github.com/llvm/llvm-project/pull/91150, a proxy header
for the errno macros is available and gets included in `libc_errno.h`
since then.

As `libc_errno.cpp` includes `libc_errno.h`, which already includes the
proxy header `hdr/errno_macros.h`, there's no need to include it in
`libc_errno.cpp` if we are in overlay mode, because the proxy header
takes care to either include our header from libc/include/ (fullbuild)
or the corresponding underlying system header (overlay).
2024-05-14 00:58:13 -04:00
Schrodinger ZHU Yifan
96c23af8b3
[libc] fix 32bit arm build (casting time_t) (#92065) 2024-05-14 00:46:17 -04:00
Schrodinger ZHU Yifan
b342d18a8f
[libc] add timeout and clock conversion utilities (#91905)
This PR:

- Make `clock_gettime` a header-only library
- Add `clock_conversion` header library to allow conversion between
clocks relative to the time of call
- Add `timeout` header library to manage the absolute timeout used in
POSIX's timed locking/waiting APIs
2024-05-13 19:00:19 -04:00
lntue
8960078765
[libc][errno] Include <linux/errno.h> for Linux in full build mode. (#92041) 2024-05-13 18:31:35 -04:00
Robin Caloudis
561c42df57
[libc][errno] Use macro instead of system header (#91150)
## Why
Currently, the system header `errno.h` is included in `libc_errno.h`,
which is supposed to be consumed by internal implementations only. As
unit and hermetic tests should never use `#include <errno.h>` but
instead use `#include "src/errno/libc_errno.h"`, we do not want to
implicitly include `errno.h`. In order to have a clear seperation
between those two, we want to pull out the definitions of errno numbers
from `errno.h`.

## What
* Extract the definitions of errno numbers from
[include/errno.h.def](https://github.com/llvm/llvm-project/pull/91150/files#diff-ed38ed463ed50571b498a5b69039cab58dc9d145da7f751a24da9d77f07781cd)
and place it under
[include/llvm-libc-macros/linux/error-number-macros.h](https://github.com/llvm/llvm-project/pull/91150/files#diff-d6192866629690ebb7cefa1f0a90b6675073e9642f3279df08a04dcdb05fd892)
* Provide mips-specific errno numbers in
[include/llvm-libc-macros/linux/mips/error-number-macros.h](https://github.com/llvm/llvm-project/pull/91150/files#diff-3fd35a4c94e0cc359933e497b10311d857857b2e173e8afebc421b04b7527743)
* Find definition of mips errno numbers in glibc
[here](ea73eb5f58/sysdeps/unix/sysv/linux/mips/bits/errno.h (L32-L50))
(equally defined in the Linux kernel)
* Provide sparc-specific errno numbers in
[include/llvm-libc-macros/linux/sparc/error-number-macros.h](https://github.com/llvm/llvm-project/pull/91150/files#diff-5f16ffb2a51a6f72ebd4403aca7e1edea48289c99dd5978a1c84385bec4f226b)
* Find definition of sparc errno numbers in glibc
[here](ea73eb5f58/sysdeps/unix/sysv/linux/sparc/bits/errno.h (L33-L51))
(equally defined in the Linux kernel)
* Include proxy header `errno_macros.h` instead of the system header
`errno.h` in `libc_errno.h`/`libc_errno.cpp`

Closes https://github.com/llvm/llvm-project/issues/80172
2024-05-13 17:56:01 -04:00
Schrodinger ZHU Yifan
27595c4bef
Revert "[libc][POSIX][pthreads] implemented missing pthread_rwlockattr functions" (#91966)
Reverts llvm/llvm-project#90249

Fullbuild is broken:
https://lab.llvm.org/buildbot/#/builders/163/builds/56501
2024-05-13 09:43:14 -04:00
Hendrik Hübner
d2676a7333
[libc][POSIX][pthreads] implemented missing pthread_rwlockattr functions (#90249)
Closes #89443

I added the two missing functions and respective test cases. Let me know
if anything needs changing.
2024-05-13 09:35:17 -04:00
Joseph Huber
fb3f4b013c
[libc] Add memory fence utility to the GPU utilities (#91756)
Summary:
GPUs like to execute instructions in the background until something
excplitely consumes them. We are working on adding some
microbenchmarking code, which requires flushing the pending memory
operations beforehand. This patch simply adds these utility functions
that will be used in the near future.
2024-05-10 16:38:13 -05:00
Schrodinger ZHU Yifan
d8e73752a5
Reland "[libc][NFC] adjust time related implementations"" (#91687)
Reverts llvm/llvm-project#91657 and Relands #91485
2024-05-10 15:32:04 -04:00
Schrodinger ZHU Yifan
b8f4f39d3d
[libc] avoid cmpxchg on the fastpath of callonce (#91748)
Avoid `cmpxchg` operation if the function has already been called.
The destination operand of `cmpxchg` may receive a write cycle without
regard to the result of the comparison
2024-05-10 11:55:02 -04:00
lntue
92dfe20acd
[libc][math] Fix exact cases for powf. (#91488)
It was reported from the CORE-MATH project that the `powf`
implementation did not round correctly when `x^y` is either exact for
exactly half-way.

This PR deals with the potential exact cases when `y` is an integer `2 <
y < 25`. In such cases, the results of `x^y` is exactly representable in
double precision.
2024-05-10 11:07:41 -04:00
Schrodinger ZHU Yifan
5a0e0b659f
Revert "[libc][NFC] adjust time related implementations" (#91657)
Reverts llvm/llvm-project#91485. It breaks GPU and fuchisa.
2024-05-09 17:27:59 -04:00
Schrodinger ZHU Yifan
8ac928fea8
[libc][NFC] adjust time related implementations (#91485) 2024-05-09 16:34:45 -04:00
lntue
e1f279e92d
[libc] Use __builtin_fma(f) by default if LIBC_TARGET_CPU_HAS_FMA is defined. (#91535) 2024-05-09 13:10:43 -04:00
lntue
6f1013a5b3
[libc] Add template deduction guide for cpp::lock_guard. (#91589)
Fix ctad-maybe-unsupported warnings for `cpp::lock_guard`.
2024-05-09 09:31:06 -04:00
Joseph Huber
a7ee81e827 [libc] Remove unused variable causing build errors 2024-05-09 07:00:25 -05:00
Vlad Mishel
c4a3d184db
[libc] Replace MutexLock with cpp::lock_guard (#89340)
This PR address issue #89002.

#### Changes in this PR

* Added a simple implementation of `cpp::lock_guard` (an equivalent of
`std::lock_guard`) in libc/src/__support/CPP inspired by the libstdc++
implementation
* Added tests for `cpp::lock_guard` in
/libc/test/src/__support/CPP/mutex_test.cpp
* Replaced all references to `MutexLock` with `cpp::lock_guard`

---------

Co-authored-by: Guillaume Chatelet <gchatelet@google.com>
2024-05-09 09:06:18 +02:00
lntue
e37bd6c68b
[libc][fenv] Add missing FE_* definitions for some environment. (#91519) 2024-05-08 17:32:05 -04:00
Schrodinger ZHU Yifan
a5044e6d50
[libc] fix typo due to futex renaming (#91379) 2024-05-07 17:36:58 -04:00
Joseph Huber
873431a68a
[libc] Add __FE_DENORM to the fenv macros (#91353)
Summary:
Some targets support denormals as floating point exceptions. This is
provided as an extension in the GNU headers as __FE_DENORM.

This provides it in our headers, however I'm unsure if we should make it
internal or external. I do not think it should be in all exception as it
doesn't represent an exceptional behavior as far as the standard is
concerned, but I'm not an expert.
2024-05-07 12:57:54 -05:00
Schrodinger ZHU Yifan
ab3a9e724d
[libc] clean up futex usage (#91163)
# Motivation

Futex syscalls are widely used in our codebase as synchronization
mechanism. Hence, it may be worthy to abstract out the most common
routines (wait and wake). On the other hand, C++20 also provides
`std::atomic_notify_one/std::atomic_wait/std::atomic_notify_all` which
align with such functionalities. This PR introduces `Futex` as a subtype
of `cpp::Atomic<FutexWordType>` with additional
`notify_one/notify_all/wait` operations.

Providing such wrappers also make future porting easier. For example,
FreeBSD's `_umtx_op` and Darwin's `ulock` can be wrapped in a similar
manner.

### Similar Examples

1. [bionic
futex](https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/bionic/bionic_futex.cpp)
2. [futex in Rust's
std](8cef37dbb6/library/std/src/sys/pal/unix/futex.rs (L21))
2024-05-07 10:47:41 -04:00
Benjamin Kramer
1d87465a0a [libc][math] fmod: clear exceptions before the test instead of after
The test has no control over the CPU state before the test runs.

This test checks whether no exception flags are set, which may not be
true at the start of the test. This used to be not a problem because the
check was broken but that was fixed in ecfb5d9951554d8bdb6a499c958f48cc35f78a88
2024-05-07 15:27:55 +02:00
Schrodinger ZHU Yifan
096f85e827
[libc] add more APIs of cmgxchg variants (#91208)
Such APIs are useful in lock implementations
2024-05-06 18:55:18 -04:00
Michael Flanders
ecfb5d9951
[libc][math] fix loose except check in {EXPECT,ASSERT}_FP_EXCEPTION macros (#88816)
Adds more FP test macros for the upcoming test adds for #61092 and the
issues opened from it: #88768, #88769, #88770, #88771, #88772.

Fix bug in `{EXPECT,ASSERT}_FP_EXCEPTION`. `EXPECT_FP_EXCEPTION(0)`
seems to be used to test that an exception did not happen, but it always
does `EXPECT_GE(... & 0, 0)` which never fails.

Update and refactor tests that break after the above bug fix. An
interesting way things broke after the above change is that
`ForceRoundingMode` and `quick_get_round()` were raising the inexact
exception, breaking a lot of the `atan*` tests.

The changes for all files other than `FPMatcher.h` and
`libc/test/src/math/smoke/RoundToIntegerTest.h` should have the same
semantics as before. For `RoundToIntegerTest.h`, lines 56-58 before the
changes do not always hold since this test is used for functions with
different exception and errno behavior like `lrint` and `lround`. I've
deleted those lines for now, but tests for those cases should be added
for the different nearest int functions to account for this.

Adding @nickdesaulniers for review.
2024-05-06 09:05:22 -04:00
luolent
a98a6e95be
Add clarifying parenthesis around non-trivial conditions in ternary expressions. (#90391)
Fixes [#85868](https://github.com/llvm/llvm-project/issues/85868)

Parenthesis are added as requested on ternary operators with non trivial conditions.

I used this [precedence table](https://en.cppreference.com/w/cpp/language/operator_precedence) for reference, to make sure we get the expected behavior on each change.
2024-05-04 18:38:45 +01:00
Joseph Huber
1022636b0c
[libc] Fix assert dependency on macro header (#91036)
Summary:
This file was missing a dependency so it wasn't being installed.
2024-05-03 20:57:34 -05:00
Schrodinger ZHU Yifan
0e5ff6251f
[libc] add hashtable fuzzing (#87949) 2024-05-02 15:36:10 -04:00
Vinayak Dev
aca511734f
[libc] Implement fcntl() function (#89507)
Fixes #84968. 

Implements the `fcntl()` function defined in the `fcntl.h` header.
2024-05-01 11:18:44 -07:00
Fabian Keßler
cd7a7a56fc
Add basic char*_t support for libc (partial WG14 N2653) (#90360)
This PR implements a part of WG14 N2653:
 - Define C23 char8_t
 - Define C11 char16_t
 - Define C11 char32_t
 
 Missing goals are:
- The type of UTF-8 character literals is changed from unsigned char to
char8_t. (Since UTF-8 character literals already have type unsigned
char, this is not a semantic change).
- New mbrtoc8() and c8rtomb() functions declared in <uchar.h> enable
conversions between multibyte characters and UTF-8.
    - A new ATOMIC_CHAR8_T_LOCK_FREE macro.
    - A new atomic_char8_t typedef name.
2024-04-30 15:08:38 -07:00
lntue
7dd4ce484c
[libc][stdfix] Fix overflow problem for fixed point sqrt when the inputs are close to max. (#90558)
Fixes https://github.com/llvm/llvm-project/issues/89668
2024-04-30 13:22:27 -04:00
Michael Flanders
5e9937d1b3
[libc][math] Adds entrypoint and tests for nearbyintf128,scalbnf128 (#88443)
Closes #84689.

Adding @lntue for review.

I was curious about the implementation of
`round_using_current_rounding_mode` used for the `nearbyint` functions.
It has one of the rounding modes as unreachable
([here](https://github.com/llvm/llvm-project/blob/main/libc/src/__support/FPUtil/NearestIntegerOperations.h#L243)),
and I was wondering if this was okay for the `nearbyint` functions.

---------

Co-authored-by: Michael Flanders <mkf727@cs.washington.edu>
2024-04-29 19:25:45 -04:00
aniplcc
11bd19a7a2
[libc][assert] define __STDC_VERSION_ASSERT_H__ (#87592)
Fixes #87561
2024-04-24 13:01:16 -07:00
David CARLIER
418212089e
[libc] adding linux SYS_fchmodat2 syscall. (#89819) 2024-04-23 22:58:00 +01:00
Roland McGrath
859de94536
[libc] Fix aarch64 build error in FEnvSafeTest change (#89826) 2024-04-23 13:50:55 -07:00
Roland McGrath
837dab96d6
[libc] Make fenv and math tests preserve fenv_t state (#89658)
This adds a new test fixture class FEnvSafeTest (usable as a base
class for other fixtures) that ensures each test doesn't perturb
the `fenv_t` state that the next test will start with.  It also
provides types and methods tests can use to explicitly wrap code
under test either to check that it doesn't perturb the state or
to save and restore the state around particular test code.

All the fenv and math tests are updated to use this so that none
can affect another.  Expectations that code under test and/or
tests themselves don't perturb state can be added later.
2024-04-23 13:21:25 -07:00
Rajveer Singh Bharadwaj
3ae10fde39
[libc] Generate docs for setjmp.h (#89542)
Resolves #88065

Added macros and functions.
2024-04-23 11:28:09 -07:00
Roland McGrath
d2be9826dd
[libc] Clean up alternate test framework support (#89659)
This replaces the old macros LIBC_COPT_TEST_USE_FUCHSIA and
LIBC_COPT_TEST_USE_PIGWEED with LIBC_COPT_TEST_ZXTEST and
LIBC_COPT_TEST_GTEST, respectively.  These are really not about
whether the code is in the Fuchsia build or in the Pigweed build,
but just about what test framework is being used.  The gtest
framework can be used in many contexts, and the zxtest framework
is not always what's used in the Fuchsia build.

The test/UnitTest/Test.h wrapper header now provides the macro
LIBC_TEST_HAS_MATCHERS() for use in `#if` conditionals on use of
gmock-style matchers, replacing `#if` conditionals that test the
framework selection macros directly.
2024-04-22 15:18:02 -07:00
Nick Desaulniers
dd7963239e
[libc][POSIX][pthreads] implement pthread_rwlockattr_t functions (#89322)
Implement:
- pthread_rwlockattr_destroy
- pthread_rwlockattr_getpshared
- pthread_rwlockattr_init
- pthread_rwlockattr_setpshared
2024-04-22 12:03:27 -07:00
Nick Desaulniers
0336116ed4
[libc][docs] codify Policy on Assembler Sources (#88185)
It would be helpful in future code reviews to document a policy with
regards to
where and when Assembler sources are appropriate. That way when
reviewers point
out infractions, they can point to this written policy, which may help
contributors understand that it's not solely the personal preferences of
individual reviewers but instead rather a previously agreed upon rule by
maintainers.

Link: https://github.com/llvm/llvm-project/pull/87837
Link: https://github.com/llvm/llvm-project/pull/88157
Link:
https://discourse.llvm.org/t/hand-written-in-assembly-in-libc-setjmp-longjmp/73249/12
2024-04-22 11:57:28 -07:00
Nick Desaulniers
43c26bbc42
[libc] don't over include stdlib in the hdr declaring bsearch (#89471)
When building overlay mode with GCC in release mode, glibc's stdlib.h
contains
an extern inline declaration of bsearch. This breaks our use of the
gnu::alias
function attribute in LLVM_LIBC_FUNCTION with GCC because GCC checks
that the
aliasee is defined in the same TU (clang does not).

We're looking at also potentially updating our definition of
LLVM_LIBC_FUNCTION
from libc/src/__support/common.h. Upon testing, I was able to get
-Wnonnull-compare diagnostics from GCC in our definition of bsearch
because
glibc declares bsearch with the fugly nonnull function attribute.

There's more we can do here though to improve our implementation of
bsearch.
7.24.5.1 says:

    Pointer arguments on such a call shall still have valid values, as
    described in 7.1.4.

We could also use either function attributes or parameter attributes to
denote
these should not be null (for users/callers) and perhaps still check for
non-null explicitly under some yet to be discussed hardening
configurations in
the future.

Link: #60481
Link:
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-nonnull-function-attribute
2024-04-22 11:50:59 -07:00
Nick Desaulniers
854cc89866
[libc] fixup nascent pthread_condattr_test (#89308)
- use namespaced identifiers
- add corresponding headers for namespaced declarations
- replace time.h and errno.h with finer grain includes
- update cmake

Fixes: #88987
Fixes: #89261
Link: #88997
Link: #89262
2024-04-19 09:15:47 -07:00
Michael Jones
55d4a89297
[libc] fix FEnvImpl not using the proxy header (#89303)
In the recent fenv.h header cleanup, two FEnvImpl.h files were missed,
causing build issues. This patch fixes that.
2024-04-18 16:19:12 -07:00
Nick Desaulniers
7aad1ee70f
[libc][docs] link to good beginner bugs (#89297)
Tagging bugs as "good first issue" and taking the time to work with new
contributors pays dividends.
2024-04-18 13:38:00 -07:00
Michael Jones
b8de7cf658
[libc] unpoison memory returned by pipe syscall (#88942)
The memory sanitizer doesn't recognize the results of the pipe syscall
as being initialized. This patch manually unpoisons that memory.
2024-04-18 13:36:18 -07:00
Michael Flanders
67669eada3
[libc][docs] Updates implementation status for some preexisting docgen json files (#89281)
Moving towards displaying impl status of standard header macros. The macros
aren't handled by docgen yet, so I haven't included updated rst files.
2024-04-18 13:20:32 -07:00