Commit Graph

1071 Commits

Author SHA1 Message Date
Guillaume Chatelet
ba4d36951f [libc] Make BigInt bit_cast-able to compatible types (#75063)
This is a second take on #74837 to fix #74258
2023-12-21 10:51:23 +01:00
Guillaume Chatelet
3ae5a9b67f [libc][NFC] Rename MAX_EXPONENT to MAX_BIASED_EXPONENT (#75932)
As currently defined `MAX_EXPONENT` actually corresponds to the biased
exponent (i.e. an unsigned value).
2023-12-20 10:30:09 +01:00
Nick Desaulniers
22764918b5 [libc] try fixing LlvmLibcStackChkFail.Smash a third time (#75988)
Build bots are failing in post submit. Unclear why but can't reproduce
locally.
Disable this test for asan for now.
2023-12-19 16:23:29 -08:00
Nick Desaulniers
b3d024c673 [libc] try fixing LlvmLibcStackChkFail.Smash again (#75967)
Looks like adding attributes to lambdas wasn't added to ISO C++ until
C++23.
Forget lambdas and just use a static function.
2023-12-19 13:32:52 -08:00
Nick Desaulniers
edbd034248 [libc] disable asan for LlvmLibcStackChkFail.Smash (#75966)
Otherwise for ASAN configured runs of the test, the test will fail due
to the
sanitizer rather than via SIGABRT.
2023-12-19 12:39:27 -08:00
Nick Desaulniers
24d44ff473 [libc] __stack_chk_fail post submit test failures (#75962)
Use a size smaller than the smallest supported page size so that we
don't
clobber over any guard pages, which may result in a segfault before
__stack_chk_fail can be called.

Also, move __stack_chk_fail outside of our namespace.
2023-12-19 11:59:42 -08:00
Nick Desaulniers
315a5cce89 [libc] move __stack_chk_fail to src/ from startup/ (#75863)
__stack_chk_fail should be provided by libc.a, not startup files.

Add __stack_chk_fail to existing linux and arm entrypoints. On Windows
(when
not targeting MinGW), it seems that the corresponding function
identifier is
__security_check_cookie, so no entrypoint is added for Windows.
Baremetal
targets also ought to be compileable with `-fstack-protector*`

There is no common header for this prototype, since calls to
__stack_chk_fail
are meant to be inserted by the compiler upon function return when
compiled
`-fstack-protector*`.
2023-12-19 11:05:12 -08:00
Guillaume Chatelet
1b4a0794b0 [libc][NFC] Use FPBits builders instead of custom constructs (#75942) 2023-12-19 16:45:36 +01:00
Guillaume Chatelet
57fcc23896 [libc] Fix improper initialization of StorageType (#75610)
`StorageType` may be a `BigInt` under the hood. Initializing it with
`-1` does not yields the maximum value.
2023-12-15 15:51:10 +01:00
Guillaume Chatelet
3546f4da19 [libc][NFC] Rename MANTISSA_WIDTH in FRACTION_LEN (#75489)
This one might be a bit controversial since the terminology has been
introduced from the start but I think `FRACTION_LEN` is a better name
here. AFAICT it really is "the number of bits after the decimal dot when
the number is in normal form."

`MANTISSA_WIDTH` is less precise as it's unclear whether we take the
leading bit into account.
This patch also renames most of the properties to use the `_LEN` suffix
and fixes useless casts or variables.
2023-12-15 13:57:35 +01:00
Guillaume Chatelet
b4fa7f4c94 [libc][NFC] Split str_to_float tests and reduce type clutter (#75471)
This is a follow up on #75353.
2023-12-15 09:44:10 +01:00
Nick Desaulniers
7c6b4be615 [libc] fix msan failure in mempcpy_test (#75532)
Internal builds of the unittests with msan flagged mempcpy_test.

    ==6862==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x55e34d7d734a in length
llvm-project/libc/src/__support/CPP/string_view.h:41:11
#1 0x55e34d7d734a in string_view
llvm-project/libc/src/__support/CPP/string_view.h:71:24
#2 0x55e34d7d734a in
__llvm_libc_9999_0_0_git::testing::Test::testStrEq(char const*, char
const*, char const*, char const*,
__llvm_libc_9999_0_0_git::testing::internal::Location)
llvm-project/libc/test/UnitTest/LibcTest.cpp:284:13
#3 0x55e34d7d4e09 in LlvmLibcMempcpyTest_Simple::Run()
llvm-project/libc/test/src/string/mempcpy_test.cpp:20:3
#4 0x55e34d7d6dff in
__llvm_libc_9999_0_0_git::testing::Test::runTests(char const*)
llvm-project/libc/test/UnitTest/LibcTest.cpp:133:8
#5 0x55e34d7d86e0 in main
llvm-project/libc/test/UnitTest/LibcTestMain.cpp:21:10

SUMMARY: MemorySanitizer: use-of-uninitialized-value
llvm-project/libc/src/__support/CPP/string_view.h:41:11 in length

What's going on here is that mempcpy_test.cpp's Simple test is using
ASSERT_STREQ with a partially initialized char array. ASSERT_STREQ calls
Test::testStrEq which constructs a cpp:string_view. That constructor
calls the
private method cpp::string_view::length. When built with msan, the loop
is
transformed into multi-byte access, which then fails upon access.

I took a look at libc++'s __constexpr_strlen which just calls
__builtin_strlen(). Replacing the implementation of
cpp::string_view::length
with a call to __builtin_strlen() may still result in out of bounds
access when
the test is built with msan.

It's not safe to use ASSERT_STREQ with a partially initialized array.
Initialize the whole array so that the test passes.
2023-12-14 13:39:33 -08:00
Guillaume Chatelet
493cc71d72 [libc][NFC] Remove MantissaWidth traits (#75458)
Same as #75362, the traits does not bring a lot of value over
`FloatProperties::MANTISSA_WIDTH` (or `FPBits::MANTISSA_WIDTH`).
2023-12-14 15:07:09 +01:00
Nick Desaulniers
c0ad6e2fa6 [libc] fix unit tests (#75361)
Fixes #75261
2023-12-13 09:55:29 -08:00
Guillaume Chatelet
6b37d8b73f [libc][NFC] Shorten type names in tests (#75358) 2023-12-13 18:09:40 +01:00
michaelrj-google
8180ea8694 [libc] Add bind function (#74014)
This patch adds the bind function to go with the socket function. It
also cleans up a lot of socket related data structures.
2023-12-12 13:36:11 -08:00
Guillaume Chatelet
dd85e67dc4 [libc] Add mask functions to math_extras (#75169) 2023-12-12 13:23:21 +01:00
Guillaume Chatelet
b9be9f66c7 [libc] Make the bit header compatible with uint128 types (#75161) 2023-12-12 11:54:54 +01:00
Guillaume Chatelet
c703e65765 [libc][NFC] Remove custom leading_zeroes, factor in frequent typenames (#74825) 2023-12-11 17:10:42 +01:00
Guillaume Chatelet
7b387d2758 [libc][NFC] Fix mixed up biased/unbiased exponent (#75037)
According to [wikipedia](https://en.wikipedia.org/wiki/Exponent_bias)
the "biased exponent" is the encoded form that is always positive
whereas the unbiased form is the actual "real" exponent that can be
positive or negative.
`FPBits` seems to be using `unbiased_exponent` to describe the encoded
form (unsigned). This patch simply use `biased` instead of `unbiased`.
2023-12-11 17:06:48 +01:00
Guillaume Chatelet
05420a1754 Revert "[libc] Make BigInt bit_cast-able to compatible types" (#74887)
This reverts the following commits:
 - a539a09000
 - 31316b3f85

Rationale for revert:
https://github.com/llvm/llvm-project/issues/74258#issuecomment-1847836861
2023-12-08 22:10:57 +01:00
Guillaume Chatelet
31316b3f85 [reland][libc] Make BigInt bit_cast-able to compatible types (#74862)
Fix #74258
This is a reland of #74837, the error went unnoticed because it compiles
fine on
clang-16 but not on clang-12 which is the version used on the buildbots.

The fix was to explicitly initialize `BigInt` variables in `constexpr`
operations: `BigInt<Bits, Signed> result(0);` instead of `BigInt<Bits,
Signed> result;`
2023-12-08 18:03:24 +01:00
Guillaume Chatelet
d5199b43be Revert "[libc] Make BigInt bit_cast-able to compatible types" (#74856)
Reverts llvm/llvm-project#74837

Some build bot are failing because of missing constexpr.

https://lab.llvm.org/buildbot/#/builders/138/builds/56468/steps/7/logs/stdio
2023-12-08 17:15:10 +01:00
Guillaume Chatelet
a2b7ded40c [libc] Make BigInt bit_cast-able to compatible types (#74837)
Fix #74258
2023-12-08 17:11:05 +01:00
Schrodinger ZHU Yifan
86bde5adc8 [libc] implement prctl (#74386)
Implement `prctl` as specified in
https://man7.org/linux/man-pages/man2/prctl.2.html.

This patch also includes test cases covering two simple use cases:

1. `PR_GET_NAME/PR_SET_NAME`: where userspace data is passed via arg2.
2. `PR_GET_THP_DISABLE`: where return value is passed via syscal retval.
2023-12-05 12:31:00 -08:00
Schrodinger ZHU Yifan
86e99e11e5 [libc] [search] improve hsearch robustness (#73896)
Following up the discussion at
https://github.com/llvm/llvm-project/pull/73469#discussion_r1409593911
by @nickdesaulniers.

According to FreeBSD implementation
(https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/upstream-freebsd/lib/libc/stdlib/hcreate.c),
`hsearch` is able to handle the cases where the global table is not
properly initialized. To do this, FreeBSD actually allows hash table to
be dynamically resized. If the global table is uninitialized at the
first call, the table will be initialized with a minimal size; hence
subsequent insertion will be reasonable as the table grows
automatically.

This patch mimic such behaviors. More precisely, this patch introduces:

1. a full table iterator that scans each element in the table,
2. a resize routine that is automatically triggered whenever the load
factor is reached where it iterates the old table and insert the entries
into a new one,
3. more tests that cover the growth of the table.
2023-12-05 09:06:48 -08:00
Guillaume Chatelet
21b986637b [libc] Fix arm32 tests (#74457)
`ASSERT_EQ` requires that both operands have the same type but on arm32
`size_t` is `unsigned int` instead of `unsigned long`. Using `size_t`
explicitely to avoid "conflicting types for parameter 'ValType"
2023-12-05 13:53:19 +01:00
Guillaume Chatelet
1d89478830 [reland][libc][NFC] Remove __support/bit.h and use __support/CPP/bit.h instead (#73939) (#74446)
Same as #73939 but also fix `libc/src/string/memory_utils/op_aarch64.h`
that was still using `deferred_static_assert`.
2023-12-05 11:35:13 +01:00
Guillaume Chatelet
de7fdc5b54 Revert "[libc][NFC] Remove __support/bit.h and use __support/CPP/bit.h instead" (#74444)
Reverts llvm/llvm-project#73939

This broke libc-aarch64-ubuntu build bot 
https://lab.llvm.org/buildbot/#/builders/138/builds/56186
2023-12-05 11:25:39 +01:00
Guillaume Chatelet
b140948850 [libc][NFC] Remove __support/bit.h and use __support/CPP/bit.h instead (#73939) 2023-12-05 11:21:07 +01:00
Schrodinger ZHU Yifan
6fd1c1b8ef [libc] fix HashTable warnings and build problems (#74371)
According to https://lab.llvm.org/buildbot/#/builders/163/builds/48002,
the generic build on HashTable fails with two major issues with
`werror`:
1. warnings on `error: suggest braces around initialization of
subobject`.
2. `__support/HashTable` tests are built regardless of its entrypoints`

This PR attempts to fix such issues.
2023-12-04 14:06:57 -08:00
Schrodinger ZHU Yifan
ff51b60b18 [libc] Revert #73704 and subsequent fixes #73984, #74026 (#74355)
The test cases of mincore require getting correct page size from OS. As
`sysconf` is not functioning correctly, these patches are implemented in
a somewhat confusing way. We revert such patches and will reintroduce
mincore after we correct sysconf.

This reverts 54878b8, 985c0d1 and 418a3a4.
2023-12-04 12:49:12 -08:00
Guillaume Chatelet
977af4252d [libc][NFC] Rename SPECIAL_X86_LONG_DOUBLE in LIBC_LONG_DOUBLE_IS_X86_FLOAT80 (#73950) 2023-12-01 14:23:08 +01:00
Guillaume Chatelet
808b7d2203 [libc][NFC] rename LONG_DOUBLE_IS_DOUBLE into LIBC_LONG_DOUBLE_IS_FLOAT64 (#73948) 2023-12-01 13:55:31 +01:00
Schrodinger ZHU Yifan
54878b80f3 [libc] remove fragile test from mincore (#74026) 2023-11-30 23:35:35 -05:00
Schrodinger ZHU Yifan
985c0d1903 [libc][mincore] use correct page_size for test (#73984) 2023-11-30 20:36:28 -05:00
Schrodinger ZHU Yifan
418a3a4577 [libc][SysMMan] implement mincore (#73704)
Implement `mincore` as specified in
https://man7.org/linux/man-pages/man2/mincore.2.html
2023-11-30 14:22:36 -05:00
Guillaume Chatelet
b703bd821d [libc] Add more functions in CPP/bit.h (#73814)
Once this is submitted we can remove `include/__support/bit.h` that
duplicates some of this functionality.
2023-11-30 13:51:02 +01:00
Schrodinger ZHU Yifan
e399a317ef [libc] fix build on aarch64 (#73739)
* avoid implicit narrowing conversion
* move hsearch entrypoints to FULL_BUILD
2023-11-28 22:39:00 -05:00
Schrodinger ZHU Yifan
81e3e7e5d4 [libc] [search] implement hcreate(_r)/hsearch(_r)/hdestroy(_r) (#73469)
This patch implements `hcreate(_r)/hsearch(_r)/hdestroy(_r)` as
specified in https://man7.org/linux/man-pages/man3/hsearch.3.html.

Notice that `neon/asimd` extension is not yet added in this patch. 

- The implementation is largely simplified from rust's
[`hashbrown`](https://github.com/rust-lang/hashbrown/blob/master/src/raw/mod.rs)
as we only consider fix-sized insertion-only hashtables. Technical
details are provided in code comments.

- This patch also contains a portable string hash function, which is
derived from [`aHash`](https://github.com/tkaitchuck/aHash)'s fallback
routine. Not using any SIMD acceleration, it has a good enough quality
(passing all SMHasher tests) and is not too bad in speed.

- Some general functionalities are added, such as `memory_size`,
`offset_to`(alignment), `next_power_of_two`, `is_power_of_two`.
`ctz/clz` are extended to support shorter integers.
2023-11-28 21:02:25 -05:00
Nishant Mittal
18fd6df885 [libc][math] Add unit tests for raising excepts in nextafter (#73556)
Follow up to
https://github.com/llvm/llvm-project/pull/72763#discussion_r1398277962.

### Summary
- Add unit tests for raising excepts in `nextafter`. 
- Fixed a bug in testing code for `nexttoward`.  

cc: @lntue
2023-11-28 00:50:17 -05:00
michaelrj-google
f90f036efb [libc] Move in_use into OptionalStorage (#73569)
The previous optional class would call the destructor on a non-trivially
destructible object regardless of if it had already been reset. This
patch fixes this by moving tracking for if the object exists into the
internal storage class for optional.
2023-11-27 13:31:10 -08:00
Guillaume Chatelet
d924c5d721 [libc][NFC] Sink "PlatformDefs.h" into "FloatProperties.h" (#73226)
`PlatformDefs.h` does not bring a lot of value as a separate file.
It is transitively included in `FloatProperties.h` and `FPBits.h`. This
patch sinks it into `FloatProperties.h` and removes the associated build
targets.
2023-11-23 11:23:18 +01:00
Guillaume Chatelet
c444879313 [libc][NFC] Split builtin_wrapper into bit and math_extras (#73113)
Split `builtin_wrapper.h` into `bit.h` and `math_extras.h` to mimic LLVM
`llvm/ADT/Bit.h` and `llvm/Support/MathExtras.h`.
Also added unittest place holders.
2023-11-23 09:58:59 +01:00
Joseph Huber
fa1e49cf37 [libc] Disable nexttoward tests on the GPU
Summary:

These tests are currently failing for some reason. A lot of math tests
on the GPU are disabled temporarily and need to be fixed.
2023-11-22 07:46:25 -06:00
Nishant Mittal
0c49fc4c68 [libc][math] Implement nexttoward functions (#72763)
Implements the `nexttoward`, `nexttowardf` and `nexttowardl` functions.
Also, raise excepts required by the standard in `nextafter` functions.

cc: @lntue
2023-11-21 09:02:51 -05:00
Joseph Huber
abd85cd473 [libc] Remove the optional arguments for NVPTX constructors (#69536)
Summary:
We call the global constructors by function pointer. For whatever reason
the NVPTX architecture relies very specifically on the arguments to the
function pointer invocation matching what the function is implemented
as. This is problematic as most of these constructors are generated
with no arguments. This patch removes the extended arguments that GNU
and LLVM use for the constructors optionally so that it can support the
common case.
2023-11-20 17:10:15 -06:00
lntue
3f906f513e [libc][math] Add initial support for C23 float128 math functions, starting with copysignf128. (#71731) 2023-11-10 14:32:59 -05:00
Joseph Huber
b1af3c0857 [libc][FIXME] Disable math tests to make the GPU bots green (#71603)
Summary:
This is a quick hack to disable affected GPU math tests so the bots will
be green again.

The offending commit is d2361b2048. If
that is reverted along with this patch the tests also pass.
2023-11-07 17:43:21 -06:00
michaelrj-google
009ba779c4 [libc][NFC] Remove libcpp include from atanf_test (#71449)
The test for atanf used <initializer_list> to simplify iterating through
an array. This caused issues with the new features.h change by creating
a
libcpp dependency in the test. This change moves the list to an array
variable, removing the need for that dependency.
2023-11-07 10:35:09 -08:00