Commit Graph

143 Commits

Author SHA1 Message Date
Alexey Samsonov
508398021d [libc] Add hardening for FixedVector data structure and fix exposed bug. (#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-08 13:03:59 -08:00
JoelWee
f0247081fa Fix after #121482 (#121764) 2025-01-06 13:17:37 +00:00
Lukas Bergdoll
a738d81cd2 [libc] Improve qsort (with build fix) (#121482) 2025-01-05 06:10:41 +08:00
Schrodinger ZHU Yifan
0b96f1cf68 Revert "[libc] Improve qsort" (#121303)
Reverts llvm/llvm-project#120450
2024-12-29 16:03:53 -05:00
Lukas Bergdoll
d2e71c92b8 [libc] Improve qsort (#120450) 2024-12-29 14:55:44 -05:00
Petr Hosek
7bf3137c39 [libc] Breakup freelist_malloc into separate files (#119806)
This better matches the structure we use for the rest of libc.
2024-12-16 10:30:27 -08:00
Petr Hosek
379cc44f56 Revert "[libc] Breakup freelist_malloc into separate files" (#119749)
Reverts llvm/llvm-project#98784 which broke libc builders.
2024-12-12 13:28:30 -08:00
Petr Hosek
4e2a9e50f6 [libc] Breakup freelist_malloc into separate files (#98784)
This better matches the structure we use for the rest of libc.
2024-12-12 11:24:36 -08:00
Joseph Huber
a6ef0debb1 [libc][NFC] Rename RPC opcodes to better reflect their usage
Summary:
RPC_ is a generic prefix here, use LIBC_ to indicate that these are
opcodes used to implement the C library
2024-12-02 15:35:08 -06:00
Daniel Thornburgh
385961d7b2 Reapply "[libc] Use best-fit binary trie to make malloc logarithmic (#117065)"
This reverts commit 93b83642ee.

 - Correct riscv32 assumption about alignment (bit of a hack).
 - Fix test case where the largest_small and smallest sizes are the
   same.
2024-11-21 15:26:24 -08:00
Daniel Thornburgh
93b83642ee Revert "[libc] Use best-fit binary trie to make malloc logarithmic (#117065)"
This reverts commit b05600d96f.
riscv32 unit test still broken
2024-11-21 11:56:05 -08:00
Daniel Thornburgh
b05600d96f Reapply "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
- 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-21 11:30:51 -08:00
Daniel Thornburgh
9be475af81 Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
Reverts llvm/llvm-project#106259

Unit tests break on AArch64.
2024-11-20 14:00:07 -08:00
Daniel Thornburgh
c3207c31fc [libc] Use best-fit binary trie to make malloc logarithmic (#106259)
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-20 13:54:00 -08:00
Joseph Huber
197e0125c3 [libc] Replace usage of GPU helpers with ones from 'gpuintrin.h' (#116454)
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-19 11:40:02 -06:00
Job Henandez Lara
33bdb53d86 [libc] Remove the #include <stdlib.h> header (#114453) 2024-11-01 21:49:57 -07:00
Joseph Huber
be0c67c90e [libc] Remove dependency on cpp::function in rpc.h (#112422)
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-15 12:31:06 -07:00
Simon Tatham
58ef1eb061 [libc] Bound the worst-case stack usage in qsort(). (#110849)
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-08 08:42:03 +01:00
Fabio D'Urso
2396c46999 [libc] Add malloc.h header defining mallopt (#110908)
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-10-03 18:45:23 +02:00
Joseph Huber
16d11e26f3 [libc] Add GPU support for the 'system' function (#109687)
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-23 14:04:28 -07:00
Schrodinger ZHU Yifan
779a444009 [libc] fix tls teardown while being used (#108229)
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-09-11 12:22:35 -04:00
Joseph Huber
a87105121d [libc] Implement locale variants for 'stdlib.h' functions (#105718)
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-29 14:18:37 -05:00
Joseph Huber
1a92cc5a0a [libc] Implement 'getenv' on the GPU target (#102376)
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-08 06:45:42 -05:00
Schrodinger ZHU Yifan
8252d4d468 [libc] enable most of the entrypoints on aarch64 (#101797)
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.
2024-08-03 12:22:38 -07:00
Joseph Huber
bf42a7860a [libc] Implement placeholder memory functions on the GPU (#101082)
Summary:
These functions are needed for `libc++` to link successfully. We can't
implement them well currently, so simply provide some stand-in
implementations. `realloc` will currently copy garbage and potentially
fault and `aligned_alloc` will work unless your alignment is more than
4K alignment. However, these should work in practice to get tests
running. I will write a real allocator soon™.
2024-07-30 10:15:30 -05:00
Daniel Thornburgh
0c10bdc05f [libc] Lazily initialize freelist malloc using symbols (#99254)
This requires the user to set the upper bounds of the heap by defining
the symbol `__libc_heap_limit`. The heap begins at `_end` and ends
`__libc_heap_limit` bytes afterwards. This prevents a completely unused
heap from requiring any space, and it prevents the heap from being
zeroed at initialization time as part of BSS. It also allows users to
customize the available heap location without recompiling libc.
    
I'd think this should eventually be replaced with an implemenation based
on a morecore() library. This would allow the same implementation to use
sbrk() on POSIX, `_end` and `__libc_heap_limit` on embedded, and a
buffer in tests. It would also provide better "wilderness" behavior that
tends to decrease heap fragementation (see Wilson et al.)

See #98096
2024-07-25 13:38:06 -07:00
lntue
1e58c9de75 [libc] Fix math tests for macos arm64. (#100060)
Some problem with current build on macos:
- no libatomic.
- death tests do not work yet.
2024-07-23 12:23:00 -04:00
Joseph Huber
aac3a2a291 [libc] Fix callback type in exit_handlers.cpp not matching (#97642)
Summary:
This file is an object library, but uses the `LIBC_COPT_PUBLIC_PACKAING`
option. This will always be undefined which leads to a type mismatch
when uses actually try to link against it. This patch simply removes
this and turns it into a header only library. This means that the
implementations of the callback lists and the mutexes need to live in
their respective files. The result is that `atexit` needs to be defined
for `at_quick_exit` to be valid.
2024-07-22 15:28:48 -05:00
Mikhail R. Gadelha
5d42d69d93 [libc] Change rand implementation so all tests pass in both 32- and 64-bit systems (#98692)
This patch makes rand select different algorithms depending on the arch.
This is needed to avoid a test failure in 32-bit systems where the LSB
of rand was not uniform enough when the 64-bit constants are used in
32-bit systems.
2024-07-17 12:01:26 -03:00
lntue
a6d2da8b9d [libc][stdlib] Implement heap sort. (#98582) 2024-07-16 08:13:25 -04:00
Petr Hosek
5ff3ff33ff [libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98597)
This is a part of #97655.
2024-07-12 09:28:41 -07:00
Mehdi Amini
ce9035f5bd Revert "[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration" (#98593)
Reverts llvm/llvm-project#98075

bots are broken
2024-07-12 09:12:13 +02:00
Michael Jones
8901c1c280 [libc] Fix atexit not getting linked on linux (#98537)
Atexit needs to be linked into exit on linux since atexit defines
__cxa_finalize. This should probably be fixed a different way but this
works for now.
2024-07-11 14:07:11 -07:00
Petr Hosek
3f30effe1b [libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98075)
This is a part of #97655.
2024-07-11 12:35:22 -07:00
Petr Hosek
be9b1de1ab [libc] Move the internal extern "C" symbols inside the namespace (#98232)
This ensures that these symbols inherit the namespace visibility.
2024-07-09 16:22:19 -07:00
Petr Hosek
5e56d74ce4 [libc] Avoid the atexit and exit_handler dependency for exit (#98121)
These are not required and without these dependencies, we would wound up
with an unresolved reference to __cxa_finalize, which can be provided by
the vendor making this compatible with baremetal.
2024-07-09 09:30:06 -07:00
PiJoules
3eebeb7e50 [libc] Add aligned_alloc (#96586)
This adds support for aligned_alloc with the freelist allocator. This
works by finding blocks large enough to hold the requested size plus
some shift amount that's at most the requested alignment. Blocks that
meet this requirement but aren't properly aligned can be split such that
the usable_space of a new block is aligned properly. The "padding" block
created will be merged with the previous block if one exists.
2024-07-03 16:26:00 -07:00
Joseph Huber
f23a5f08f4 [libc] Remove atomic alignment diagnostics globally (#96803)
Summary:
These warnings mean that it will lower to a libcall. Previously we just
disabled it locally, which didn't work with GCC. This patch does it
globally in the compiler options if the compiler is clang.
2024-06-26 18:12:46 -05:00
Joseph Huber
e1015ae55d [libc][docs] List rand and srand as supported on the GPU (#96757)
Summary:
I initially didn't report these as supported because they didn't provide
expected behavior and were very wasteful. The recent patch moved them to
a lock-free atomic implementation so they can now actually be used.
2024-06-26 11:45:47 -05:00
Joseph Huber
d0527ab697 [libc] Fix Fuscia builder failing on atomic warnings (#96791)
Summary:
This function uses atomics now, which emit warnings on some platforms
that don't support full lock-free atomics. These aren't specifically
wrong, and in the future we could investigate a libc configuration
specialized for single-threaded microprocessors, but for now we should
get the bot running again.
2024-06-26 11:24:48 -05:00
Joseph Huber
86860be288 [libc] Make 'rand()' thread-safe using atomics instead of TLS (#96692)
Summary:
Currently, we implement the `rand` function using thread-local storage.
This is somewhat problematic because not every target supports TLS, and
even more do not support non-zero initializers on TLS.

The C standard states that the `rand()` function need not be thread,
safe. However, many implementations provide thread-safety anyway.
There's some confusing language in the 'rationale' section of
https://pubs.opengroup.org/onlinepubs/9699919799/functions/rand.html,
but given that `glibc` uses a lock, I think we should make this thread
safe as well. it mentions that threaded behavior is desirable and can be
done in the two ways:

1. A single per-process sequence of pseudo-random numbers that is shared
by all threads that call rand()
2. A different sequence of pseudo-random numbers for each thread that
calls rand()

The current implementation is (2.) and this patch moves it to (1.). This
is beneficial for the GPU case and more generic support. The downside is
that it's slightly slower to do these atomic operations, the fast path
will be two atomic reads and an atomic write.
2024-06-26 07:03:28 -05:00
lntue
c2735d2d5a [libc][stdlib] Bring all GPU's alloc/free entrypoints under the same conditional. (#96373) 2024-06-21 23:29:15 -04:00
lntue
38458144bf [libc][stdlib] Fix skipped libc.src.stdlib.freelist_malloc target for baremetal. (#96372)
Downstream build issue reported:
https://ci.chromium.org/ui/p/fuchsia/builders/toolchain.ci/clang-linux-x64/b8744479865106393873/overview
2024-06-21 22:42:07 -04:00
lntue
09bc1e8250 [libc][stdlib] Only use freelist_malloc for baremetal targets. (#96355) 2024-06-21 16:38:25 -04:00
PiJoules
d809152266 [libc] Control freelist malloc buffer size with a config (#96248)
Rather than propgating a compile define, add an explicit cmake flag for
controlling the size. The default for baremetal is 100KB and the default
for others is 1GB.
2024-06-20 16:43:47 -07:00
PiJoules
f77ade0aed [libc] Move freelist + block to __support (#96231) 2024-06-20 13:13:52 -07:00
lntue
a50bcc03cb [libc][stdlib] Only add internal malloc in full build mode. Use the system malloc in overlay mode. (#95845)
This causes an issue in overlay mode:
https://github.com/llvm/llvm-project/pull/95736#issuecomment-2172739705
2024-06-17 16:56:19 -04:00
PiJoules
005758eb6b [libc][stdlib] Make the FreeListHeap constant-initializable (#95453)
This refactors some of the FreeListHeap, FreeList, and Block classes to
have constexpr ctors so we can constinit a global allocator that does
not require running some global function or global ctor to initialize.
This is needed to prevent worrying about initialization order and any
other module-ctor can invoke malloc without worry.
2024-06-14 12:11:49 -07:00
PiJoules
3106a23155 [libc][stdlib] Fix UB in freelist (#95330)
Some of the freelist code uses type punning which is UB in C++, namely
because we read from a union member that is not the active union member.
2024-06-13 14:55:09 -07:00
PiJoules
3bcd80acba [libc][stdlib] Add the FreelistHeap (#95066)
This is the actual freelist allocator which utilizes the generic
FreeList and the Block classes. We will eventually wrap the malloc
interface around this.

This is a part of #94270 to land in smaller patches.
2024-06-12 22:47:47 -07:00