summaryrefslogtreecommitdiff
path: root/libc/src/stdlib
AgeCommit message (Collapse)Author
2025-11-17[libc] Move mbtowc, mbstowcs and inverse functions to stdlib.h (#168455)Alexey Samsonov
These functions should be declared in `stdlib.h`, not `wchar.h`, as confusing as it is. Move them to the proper header file and matching directories in src/ and test/ trees. This was discovered while testing libc++ build against llvm-libc, which re-declares functions like mbtowc in std-namespace in `<cstdlib>` header, and then uses those functions in its locale implementation.
2025-11-05[libc] Add printf error handling (with fixes #2) (#166517)Marcell Leleszi
https://github.com/llvm/llvm-project/issues/159474 Another try of trying to land https://github.com/llvm/llvm-project/pull/166382 - Fix some leftover tests checking for specific errnos - Guard errno checking tests to not run on the GPU @michaelrj-google
2025-11-05[libc] Migrate ctype_utils to use char instead of int where applicable. ↵Alexey Samsonov
(#166225) Functions like isalpha / tolower can operate on chars internally. This allows us to get rid of unnecessary casts and open a way to creating wchar_t overloads with the same names (e.g. for isalpha), that would simplify templated code for conversion functions (see 315dfe5865962d8a3d60e21d1fffce5214fe54ef). Add the int->char converstion to public entrypoints implementation instead. We also need to introduce bounds check on the input argument values - these functions' behavior is unspecified if the argument is neither EOF nor fits in "unsigned char" range, but the tests we've had verified that they always return false for small negative values. To preserve this behavior, cover it explicitly.
2025-11-04Revert commit d8e5698 and 15b19c7 (#166498)Kewen Meng
2025-11-04[libc] Add printf error handling (with fixes) (#166382)Marcell Leleszi
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-03Revert "[libc] Add printf error handling" (#166232)Kewen Meng
2025-11-03[libc] Add printf error handling (#162876)Marcell Leleszi
[#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-09-12[libc] Change __builtin_memcpy to inline_memcpy. (#158345)lntue
2025-09-04[libc] Fix undefined exit symbol for the NVPTX buildJoseph Huber
Summary: NVPTX does not support external weak symbols being defined, disable this to get the bots working.
2025-09-03[libc] ensure tls dtors are called in main thread (#133641)Schrodinger ZHU Yifan
This is a part of allocator patch since I want to make sure the TLS for allocators are correctly handled. TLS dtors are not invoked on exit previously. This departures from major libc implementations. This PR fixes the issue by aligning the behavior with bionic. https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/bionic/exit.cpp I believe the finalization order is also consistent with glibc now: On main thread exiting: - pthread_key dtors are not called (unless exiting with `pthread_exit`) - `__cxa` based tls dtors are called - `::__cxa_atexit` and `::atexit` dtors are called - `.fini` dtors are called ![image](https://github.com/user-attachments/assets/737c4845-cab6-47a9-aa00-32997be141bd)
2025-07-23[libc][NFC] Add stdint.h proxy header to fix dependency issue with ↵lntue
<stdint.h> includes. (#150303) https://github.com/llvm/llvm-project/issues/149993
2025-07-02[libc] Efficiently implement `aligned_alloc` for AMDGPU (#146585)Joseph Huber
Summary: This patch uses the actual allocator interface to implement `aligned_alloc`. We do this by simply rounding up the amount allocated. Because of how index calculation works, any offset within an allocated pointer will still map to the same chunk, so we can just adjust internally and it will free all the same.
2025-06-30[libc] Efficiently implement 'realloc' for AMDGPU devices (#145960)Joseph Huber
Summary: Now that we have `malloc` we can implement `realloc` efficiently. This uses the known chunk sizes to avoid unnecessary allocations. We just return nullptr for NVPTX. I'd remove the list for the entrypoint but then the libc++ code would stop working. When someone writes the NVPTX support this will be trivial.
2025-06-11[libc] Move libc_errno.h to libc/src/__support and make ↵lntue
LIBC_ERRNO_MODE_SYSTEM to be header-only. (#143187) This is the first step 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-05-05qsort_r.h: qsort_r() is POSIX now. (#138545)enh-google
2025-03-28[libc] implement `memalignment` (#132493)Mohamed Emad
This patch adds the `memalignment` function to LLVM-libc, following its description in [WG14 N3220, §7.24.2.1](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=387). - [x] Add the implementation of `memalignment` in [`/src/stdlib`](https://github.com/llvm/llvm-project/tree/main/libc/src/stdlib) - [x] Add tests for `memalignment` in [`/test/src/stdlib`](https://github.com/llvm/llvm-project/tree/main/libc/test/src/stdlib) - [x] Add `memalignment` to [`entrypoints.txt`](https://github.com/llvm/llvm-project/blob/main/libc/config/linux/x86_64/entrypoints.txt) for at least x86_64 and whatever you're building on - [x] Add `memalignment` to [`include/stdlib.yaml`](https://github.com/llvm/llvm-project/blob/main/libc/include/stdlib.yaml) Closes #132300 --------- Co-authored-by: Joseph Huber <huberjn@outlook.com>
2025-03-12[libc] Template the writing mode for the writer class (#111559)Joseph Huber
Summary: Currently we dispatch the writing mode off of a runtime enum passed in by the constructor. This causes very unfortunate codegen for the GPU targets where we get worst-case codegen because of the unused function pointer for `sprintf`. Instead, this patch moves all of this to a template so it can be masked out. This results in no dynamic stack and uses 60 VGPRs instead of 117. It also compiles about 5x as fast.
2025-03-10[libc] Add `-Wno-sign-conversion` & re-attempt `-Wconversion` (#129811)Vinay Deshmukh
Relates to https://github.com/llvm/llvm-project/issues/119281#issuecomment-2699470459
2025-03-05Revert "[libc] Enable -Wconversion for tests. (#127523)"Augie Fackler
This reverts commit 1e6e845d49a336e9da7ca6c576ec45c0b419b5f6 because it changed the 1st parameter of adjust() to be unsigned, but libc itself calls adjust() with a negative argument in align_backward() in op_generic.h.
2025-03-04[libc] Enable -Wconversion for tests. (#127523)Vinay Deshmukh
Relates to: #119281
2025-03-04[libc] Add a missing includeDmitri Gribenko
This is a fixup for https://github.com/llvm/llvm-project/commit/da6d5fa79a558b66c281bed3f5ce848a69a65208.
2025-03-03Add missing LIBC_INLINE to qsort_pivot.h (#126249)Lukas Bergdoll
Fixes #111495
2025-02-27[libc] implement l64a (#129099)Michael Jones
Adds l64a, which generates the base 64 string expected by a64l.
2025-02-25[libc] implement a64l (#128758)Michael Jones
Implement the posix function a64l. Standard: https://pubs.opengroup.org/onlinepubs/9799919799/functions/a64l.html
2025-01-24[libc] Use the NVIDIA device allocator for GPU malloc (#124277)Joseph Huber
Summary: This is a blocker on another patch in the OpenMP runtime. The problem is that NVIDIA truly doesn't handle RPC-based allocations very well. It cannot reliably update the MMU while a kernel is running and it will usually deadlock if called from a separate thread due to internal use of TLS. This patch just removes the definition of `malloc` and `free` for NVPTX. The result here is that they will be undefined, which is the cue for the `nvlink` linker to define them for us. So, as far as `libc` is concerned it still implements malloc.
2025-01-23[libc][NFC] Strip all training whitespace and missing newlines (#124163)Joseph Huber
2025-01-08[libc] Add hardening for FixedVector data structure and fix exposed bug. ↵Alexey Samsonov
(#122159) Add LIBC_ASSERT statements to FixedVector implementation, and zero out the memory when the elements are removed to flag out-of-bound access and dangling pointer/reference access. This change unmasks the bug in one of FixedVector uses for atexit handlers: dangling reference use, which was actually led to crashes in the wild (with prod blockstore implementation). Fix it in this CL.
2025-01-06Fix after #121482 (#121764)JoelWee
2025-01-05[libc] Improve qsort (with build fix) (#121482)Lukas Bergdoll
2024-12-29Revert "[libc] Improve qsort" (#121303)Schrodinger ZHU Yifan
Reverts llvm/llvm-project#120450
2024-12-29[libc] Improve qsort (#120450)Lukas Bergdoll
2024-12-16[libc] Breakup freelist_malloc into separate files (#119806)Petr Hosek
This better matches the structure we use for the rest of libc.
2024-12-12Revert "[libc] Breakup freelist_malloc into separate files" (#119749)Petr Hosek
Reverts llvm/llvm-project#98784 which broke libc builders.
2024-12-12[libc] Breakup freelist_malloc into separate files (#98784)Petr Hosek
This better matches the structure we use for the rest of libc.
2024-12-02[libc][NFC] Rename RPC opcodes to better reflect their usageJoseph Huber
Summary: RPC_ is a generic prefix here, use LIBC_ to indicate that these are opcodes used to implement the C library
2024-11-21Reapply "[libc] Use best-fit binary trie to make malloc logarithmic (#117065)"Daniel Thornburgh
This reverts commit 93b83642ee34d0092b94776728dad0117c2b72a1. - Correct riscv32 assumption about alignment (bit of a hack). - Fix test case where the largest_small and smallest sizes are the same.
2024-11-21Revert "[libc] Use best-fit binary trie to make malloc logarithmic (#117065)"Daniel Thornburgh
This reverts commit b05600d96f46697e21f6b1b7ad901391326243a8. riscv32 unit test still broken
2024-11-21Reapply "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)Daniel Thornburgh
- Fix assertion expressions. - Fix incorrect small size in freestore_test. - There may only be one small size for high alignment and small pointers (riscv32). - Don't rely on stack alignment in FreeList test.
2024-11-20Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)Daniel Thornburgh
Reverts llvm/llvm-project#106259 Unit tests break on AArch64.
2024-11-20[libc] Use best-fit binary trie to make malloc logarithmic (#106259)Daniel Thornburgh
This reworks the free store implementation in libc's malloc to use a dlmalloc-style binary trie of circularly linked FIFO free lists. This data structure can be maintained in logarithmic time, but it still permits a relatively small implementation compared to other logarithmic-time ordered maps. The implementation doesn't do the various bitwise tricks or optimizations used in actual dlmalloc; it instead optimizes for (relative) readability and minimum code size. Specific optimization can be added as necessary given future profiling.
2024-11-19[libc] Replace usage of GPU helpers with ones from 'gpuintrin.h' (#116454)Joseph Huber
Summary: These are provided by a resource header now, cut these from the dependencies and only provide the ones we use for RPC.
2024-11-01[libc] Remove the #include <stdlib.h> header (#114453)Job Henandez Lara
2024-10-15[libc] Remove dependency on `cpp::function` in `rpc.h` (#112422)Joseph Huber
Summary: I'm going to attempt to move the `rpc.h` header to a separate folder that we can install and include outside of `libc`. Before doing this I'm going to try to trim up the file so there's not as many things I need to copy to make it work. This dependency on `cpp::functional` is a low hanging fruit. I only did it so that I could overload the argument of the work function so that passing the id was optional in the lambda, that's not a *huge* deal and it makes it more explicit I suppose.
2024-10-08[libc] Bound the worst-case stack usage in qsort(). (#110849)Simon Tatham
Previously, the Quicksort implementation was written in the obvious way: after each partitioning step, it explicitly recursed twice to sort the two sublists. Now it compares the two sublists' sizes, and recurses only to sort the smaller one. To handle the larger list it loops back round to the top of the function, so as to handle it within the existing stack frame. This means that every recursive call is handling a list at most half that of its caller. So the maximum recursive call depth is O(lg N). Otherwise, in Quicksort's bad cases where each partition step peels off a small constant number of array elements, the stack usage could grow linearly with the array being sorted, i.e. it might be Θ(N). I tested this code by manually constructing a List Of Doom that causes this particular quicksort implementation to hit its worst case, and confirming that it recursed very deeply in the old code and doesn't in the new code. But I haven't added that list to the test suite, because the List Of Doom has to be constructed in a way based on every detail of the quicksort algorithm (pivot choice and partitioning strategy), so it would silently stop being a useful regression test as soon as any detail changed.
2024-10-03[libc] Add malloc.h header defining mallopt (#110908)Fabio D'Urso
This patch adds the malloc.h header, declaring Scudo's mallopt entrypoint when built LLVM_LIBC_INCLUDE_SCUDO, as well as two constants that can be passed to it (M_PURGE and M_PURGE_ALL). Due to limitations of the current build system, only the declaration of mallopt is gated by LLVM_LIBC_INCLUDE_SCUDO, and the two new constants are defined irrespectively of it. We may need to refine this in the future. Note that some allocators other than Scudo may offer a mallopt implementation too (e.g. man 3 mallopt), albeit with different supported input values. This patch only supports the specific case of LLVM_LIBC_INCLUDE_SCUDO.
2024-09-23[libc] Add GPU support for the 'system' function (#109687)Joseph Huber
Summary: This function can easily be implemented by forwarding it to the host process. This shows up in a few places that we might want to test the GPU so it should be provided. Also, I find the idea of the GPU offloading work to the CPU via `system` very funny.
2024-09-11[libc] fix tls teardown while being used (#108229)Schrodinger ZHU Yifan
The call chain to `Mutex:lock` can be polluted by stack protector. For completely safe, let's postpone the main TLS tearing down to a separate phase. fix #108030
2024-08-29[libc] Implement locale variants for 'stdlib.h' functions (#105718)Joseph Huber
Summary: This provides the `_l` variants for the `stdlib.h` functions. These are just copies of the same entrypoint and don't do anything with the locale information.
2024-08-08[libc] Implement 'getenv' on the GPU target (#102376)Joseph Huber
Summary: This patch implements 'getenv'. I was torn on how to implement this, since realistically we only have access to this environment pointer in the "loader" interface. An alternative would be to use an RPC call every time, but I think that's overkill for what this will be used for. A better solution is just to emit a common `DataEnvironment` that contains all of the host visible resources to initialize. Right now this is the `env_ptr`, `clock_freq`, and `rpc_client`. I did this by making the `app.h` interface that Linux uses more general, could possibly move that into a separate patch, but I figured it's easier to see with the usage.
2024-08-03[libc] enable most of the entrypoints on aarch64 (#101797)Schrodinger ZHU Yifan
This is a non-feature change that enables most of the entrypoints for aarch64 based runtime builds. It fixes an additional problem that some compiler-rt targets are not defined at the time of dependency checking thus leading to false-negatives.