Commit graph

964 commits

Author SHA1 Message Date
Sönke Holz e7c8ff3839 LibC: Clean up crt0
We already set these variables and call `_init` in the dynamic linker.
As we don't care about static binaries, remove these assignments and the
call to `_init` from `_entry`.

The function referenced by DT_INIT is also not necessarily called
`_init`, so directly calling `_init` is not really correct.

`s_global_initializers_ran` and `__stack_chk_guard` are unused, so
remove them.
2023-10-12 15:20:50 +02:00
Sönke Holz 0bff1f61b6 LibC+LibELF: Correctly call destructors on exit()
We currently don't call any DT_FINI_ARRAY functions, so change that.

The call to `_fini` in `exit` is unnecessary, as we now call the
function referenced by DT_FINI in `__call_fini_functions`.
2023-10-12 15:20:50 +02:00
Liav A 50429d3b22 LibC+Kernel: Move GPU-related API methods to a LibC header file
The Kernel/API directory in general shouldn't include userspace code,
but structure definitions that both are shared between the Kernel and
userspace.

All users of the ioctl API obviously use LibC so LibC is the most common
and shared library for the affected programs.
2023-09-15 11:05:25 -06:00
Liav A 8fe74c7d57 LibC+Kernel: Move device-files related methods to a LibC header file
The Kernel/API directory in general shouldn't include userspace code,
but structure definitions that both are shared between the Kernel and
userspace.

LibC is the most appropriate place for these methods as they're already
included in the sys/sysmacros.h file to create a set of convenient
macros for these methods.
2023-09-15 11:05:25 -06:00
Daniel Bertalan 725584881a LibC: Set (U)LONG_WIDTH correctly for 64-bit
All supported targets use an LP64 ABI, where both `long` and `long long`
are 64 bits wide. We set `LONG_WIDTH` to 32, which caused an error when
compiling gnulib's `vasnprintf` routine.

We might consider using built-in macros to avoid such issues in the
future.
2023-09-03 09:18:22 +02:00
René Hickersberger f2bd3904da LibC: Implement memccpy 2023-09-03 06:21:40 +02:00
René Hickersberger ad560cff0f LibC: Implement vdprintf and dprintf
These functions work just like `vfprintf` and `fprintf`, except that
they take a file descriptor as their first argument instead of a FILE*.
2023-09-03 06:21:40 +02:00
Liav A 2966188ea3 Userland: Always call syscall(SC_prctl, ...) with 4 arguments
The arguments are passed on registers, so if we pass only 3 defined
arguments then the fourth argument for the prctl syscall could have
garbage value within it.

To avoid possible bugs, always pass 3 arguments to a raw syscall prctl
call in addition to the prctl sub-option (the first argument).
2023-09-02 18:22:07 +02:00
Marcus Nilsson a2f0804f22 LibC: Move parsing of service file line to static helper function
This patch adds `parse_service_file_line()` which both `getservent()`
and `getaddrinfo()` makes use of.
2023-08-29 11:04:48 +02:00
Marcus Nilsson 2b430e1f56 LibC: Remove dependency of getservbyname() in getaddrinfo() 2023-08-29 11:04:48 +02:00
Marcus Nilsson 37cea81837 LibC: Use gethostbyname_r() in getaddrinfo()
getaddrinfo() used to use gethostbyname() which isn't MT-Safe.
2023-08-29 11:04:48 +02:00
Marcus Nilsson fd792f93c4 LibC: Use gethostbyname_r() in gethostbyname()
Since `gethostbyname()` can't fail because of memory-related issues, it
has no way of representing a memory-related failure. We therefor use
`NO_RECOVERY` to represent a memory-related failure as well
as its normal name server related failures.
2023-08-29 11:04:48 +02:00
Marcus Nilsson 1a7e262752 LibC: Implement gethostbyname_r()
gethostbyname_r() will be used to make getaddrinfo() MT-Safe.
2023-08-29 11:04:48 +02:00
Beckett Normington 8bc1f1b63e LibC: Add drand48, srand48
These functions are required for the `perl5` port's random function to
operate correctly. As a bonus, this allows us to remove a nasty patch
that replaces `drand48` with `random`.
2023-08-28 00:40:35 +02:00
Liav A 1c0aa51684 Kernel+Userland: Remove the {get,set}_thread_name syscalls
These syscalls are not necessary on their own, and they give the false
impression that a caller could set or get the thread name of any process
in the system, which is not true.

Therefore, move the functionality of these syscalls to be options in the
prctl syscall, which makes it abundantly clear that these operations
could only occur from a running thread in a process that sees other
threads in that process only.
2023-08-25 11:51:52 +02:00
kleines Filmröllchen 096cecb95e Everywhere: Add RISC-V 64 target to the build system
This is a minimal set of changes to allow `serenity.sh build riscv64` to
successfully generate the build environment and start building. This
includes some, but not all, assembly stubs that will be needed later on;
they are currently empty.
2023-08-18 08:37:43 -06:00
Daniel Bertalan ad9e674fa0 LibC+LibELF: Support loading shared libraries compiled with dynamic TLS
This is a prerequisite for upstreaming our LLVM patches, as our current
hack forcing `-ftls-model=initial-exec` in the Clang driver is not
acceptable upstream.

Currently, our kernel-managed TLS implementation limits us to only
having a single block of storage for all thread-local variables that's
initialized at load time. This PR merely implements the dynamic TLS
interface (`__tls_get_addr` and TLSDESC) on top of our static TLS
infrastructure. The current model's limitations still stand:
- a single static TLS block is reserved at load time, `dlopen()`-ing
  shared libraries that define thread-local variables might cause us to
  run out of space.
- the initial TLS image is not changeable post-load, so `dlopen()`-ing
  libraries with non-zero-initialized TLS variables is not supported.

The way we repurpose `ti_module` to mean "offset within static TLS
block" instead of "module index" is not ABI-compliant.
2023-08-18 16:20:13 +02:00
Sergey Bugaev 0a6690b745 LibC: Fix passing futex value for FUTEX_WAKE_BITSET
FUTEX_WAKE_BITSET, like FUTEX_WAKE, accepts the number of waiting
threads to wake, not the current value of the futex. Pass UINT32_MAX
instead.
2023-08-14 13:46:17 -06:00
Daniel Bertalan 286984750e Kernel+LibC: Pass 64-bit integers in syscalls by value
Now that support for 32-bit x86 has been removed, we don't have to worry
about the top half of `off_t`/`u64` values being chopped off when we try
to pass them in registers. Therefore, we no longer need the workaround
of pointers to stack-allocated values to syscalls.

Note that this changes the system call ABI, so statically linked
programs will have to be re-linked.
2023-08-12 01:14:26 +02:00
Jelle Raaijmakers ba776390b5 LibC: Define time* macros before definitions
Otherwise, if an application defines its own macros, the implementations
get messed up. This way we redefine those macros if they're set.

This fixes the PHP port build.
2023-08-09 05:40:39 +02:00
Zaggy1024 dcfec8bfbe LibC: Expect mutexes to be locked and needing wake when calling futex()
tl;dr: This fixes deadlocks that would occur in most applications in
Serenity when SMP was enabled.

As an optimization to `pthread_mutex_unlock()`, if only one thread
holds a mutex lock, it will avoid calling `futex()` to wake, since no
threads are actually waiting in `futex()`.

If a thread manages to synchronously unlock and relock the same mutex,
the state will be set to indicate that a wake is not needed, regardless
of whether any other threads are waiting for the lock. This should be
fine, as a thread that is waiting will set the mutex to needing a wake
and check if it is unlocked to unblock itself in the same atomic
operation.

However, when `pthread_mutex_lock()` was called with only one thread
holding the mutex lock, instead of telling `futex()` to atomically
sleep the thread if the state is set to indicate that a wake is
needed, the first wait would check if the state was set to indicate a
wake was _not_ needed. This means it is possible for the call to
`futex()` to wait when any subsequent call to `pthread_mutex_unlock()`
would not call `futex()` to wake, causing it to wait forever.

After that, any other thread that tries to take the lock will see that
the lock is taken and also `futex()` wait. Despite the fact that these
other threads would set the state to needing a wake, there will be no
unblocked thread holding the lock to actually wake them.

By making it wait only if the state indicates to other threads that a
wake is needed, heavily contended mutexes should no longer cause
deadlocks. Most applications would encounter these deadlocks due to the
mutex used by `malloc()`, some sooner than others. The worst offenders
(other than Ladybird) were most likely VideoPlayer and SoundPlayer.
2023-08-09 05:31:35 +02:00
Lucas CHOLLET 3f35ffb648 Userland: Prefer _string over _short_string
As `_string` can't fail anymore (since 3434412), there are no real
benefits to use the short variant in most cases.
2023-08-08 07:37:21 +02:00
Sergey Bugaev b604640950 LibC: Fix struct declaration visibility
Isn't

"expected struct timeval *, but argument is of type struct timeval *"

a fun error message? C considers a 'struct foo' mentioned inside a
function argument to be a distinct type from 'struct foo' declared on
the global level, but only if the in-function definition comes first. So
we need to ensure that struct timeval is declared (either fully, or
forward-declared) before we declare select() and pselect(). This was
taken care of by including <sys/time.h>, but
https://github.com/SerenityOS/serenity/pull/20044 made it so that
<sys/time.h> itself includes <sys/select.h>. So if the user's program
includes <sys/time.h> (before possibly including <sys/select.h>), then
<sys/select.h>'s include of <sys/time.h> will turn into a no-op (since
<sys/time.h> is already being included), yet there will not have been a
struct timeval definition yet, and we'd get the fun error message.

Fix this by including <Kernel/API/POSIX/sys/time.h> instead of
<sys/time.h>
2023-07-29 16:51:58 -06:00
Sergey Bugaev 8c8ba4cfe4 LibC: Fix duplicated function symbols
Commit cccb6c7287 has moved some function
definitions into complex.h. The functions were marked inline, but not
static, so a symbol definition was emited for them in any compilation
unit that included complex.h. If multiple such compilation units get
linked into the same binary, we get a duplicate symbol error.

Fix this by declaring the functions static inline.
2023-07-29 16:51:58 -06:00
Timothy Flynn 05fbd31727 Revert "LibC: Remove duplicate log statement"
This reverts commit 58c91f0a99.
2023-07-22 12:19:53 -04:00
Tim Schumacher f8bd607733 LibC: Make getopt available from getopt.h 2023-07-19 00:19:35 -06:00
Tim Schumacher aae106e37b LibC: Add most of the "header may make visible" dependencies from POSIX 2023-07-19 00:19:35 -06:00
Sam Atkins 58c91f0a99 LibC: Remove duplicate log statement
Now that warnln() also outputs to the debug console, this would print
the error twice.
2023-07-16 00:59:13 +02:00
sarsaparilla89 9ac4470b9b LibC: Fix max value of short 2023-07-08 15:53:02 -04:00
Timothy Flynn c911781c21 Everywhere: Remove needless trailing semi-colons after functions
This is a new option in clang-format-16.
2023-07-08 10:32:56 +01:00
Daniel Bertalan 210448b6ed LibC: Relax memmove() to memcpy() in more cases
`memcpy()` and `memmove()` are functionally equivalent if the source and
destination memory regions do not overlap. If this is the case, we
should prefer `memcpy()` as it's implemented in a simpler and faster
way. As our `memcpy()` starts copying from the lowest address first,
relaxing a `memmove()` call to `memcpy()` is safe if the destination:
- starts at a lower address than the source
- starts at a higher address than the source's end
2023-07-07 23:46:54 +02:00
kleines Filmröllchen a3f6236bec LibC: Convert semaphore and termcap to String 2023-07-06 10:15:37 +01:00
Jelle Raaijmakers eacc0bfa02 AK+LibC: Remove AK/Atomic.h includes from our RefPtrs
We don't seem to be using it there.
2023-07-04 16:30:13 +02:00
Liav A 23a7ccf607 Kernel+LibCore+LibC: Split the mount syscall into multiple syscalls
This is a preparation before we can create a usable mechanism to use
filesystem-specific mount flags.
To keep some compatibility with userland code, LibC and LibCore mount
functions are kept being usable, but now instead of doing an "atomic"
syscall, they do multiple syscalls to perform the complete procedure of
mounting a filesystem.

The FileBackedFileSystem IntrusiveList in the VFS code is now changed to
be protected by a Mutex, because when we mount a new filesystem, we need
to check if a filesystem is already created for a given source_fd so we
do a scan for that OpenFileDescription in that list. If we fail to find
an already-created filesystem we create a new one and register it in the
list if we successfully mounted it. We use a Mutex because we might need
to initiate disk access during the filesystem creation, which will take
other mutexes in other parts of the kernel, therefore making it not
possible to take a spinlock while doing this.
2023-07-02 01:04:51 +02:00
implicitfield 5dfe2eb389 Everywhere: Resolve conflicts with LibC and libc++
Since https://reviews.llvm.org/D131441, libc++ must be included before
LibC. As clang includes libc++ as one of the system includes, LibC
must be included after those, and the only correct way to do that is
to install LibC's headers into the sysroot.

Targets that don't link with LibC yet require its headers for one
reason or another must add install_libc_headers as a dependency to
ensure that the correct headers have been (re)installed into the
sysroot.

LibC/stddef.h has been dropped since the built-in stddef.h receives
a higher include priority.

In addition, string.h and wchar.h must
define __CORRECT_ISO_CPP_STRING_H_PROTO and
_LIBCPP_WCHAR_H_HAS_CONST_OVERLOADS respectively in order to tell
libc++ to not try to define methods implemented by LibC.
2023-06-27 12:40:38 +02:00
implicitfield 79adeb626b LibC+LibELF: Move ELF definitions from LibC to LibELF
This is needed to avoid including LibC headers in Lagom builds.
Unfortunately, we cannot rely on the build machine to provide a
fully POSIX-compatible ELF header for Lagom builds, so we have to
use our own.
2023-06-27 12:40:38 +02:00
implicitfield 7d19abda7a LibC+LibRegex: Move regex_defs.h from LibC to LibRegex
This is needed to avoid including LibC headers in Lagom builds.
2023-06-27 12:40:38 +02:00
implicitfield cccb6c7287 LibC: Drop complex.cpp and move its definitions to complex.h
libc++ disallows including LibC's complex.h in C++ mode. This means that
a C++ file cannot expect LibC's complex.h to be included, and thus
cannot use c-prefixed complex number functions. As a result,
complex.cpp is broken when libc++ has a higher include priority
than LibC.

A check for __cplusplus has been added to complex.h to warn users of
toolchains that don't use libc++.
2023-06-27 12:40:38 +02:00
Tim Ledbetter b9e7998b53 LibC: Return EINVAL from unsetenv() if input is empty or contains "=" 2023-06-26 19:27:45 +02:00
Dominika Liberda 75307803a2 LibC: Implement stpcpy
For better code clarity, also reformatted how strcpy increments
pointers.
2023-06-11 08:47:15 +02:00
Peter Brottveit Bock 49b29332f2 AK: Migrate IPv6Address::to_deprecated_string() to ::to_string()
Change the name and return type of
`IPv6Address::to_deprecated_string()` to `IPv6Address::to_string()`
with return type `ErrorOr<String>`.

It will now propagate errors that occur when writing to the
StringBuilder.

There are two users of `to_deprecated_string()` that now use
`to_string()`:

1. `Formatted<IPv6Address>`: it now propagates errors.

2. `inet_ntop`: it now sets errno to ENOMEM and returns.
2023-06-09 19:38:14 +01:00
Emily Trau 675cf43937 Ports: Add lowdown 1.0.2 2023-06-07 13:44:44 +02:00
Tim Ledbetter a041f1671c LibC: Ignore signals that don't have a name in getsignalbyname()
This prevents a segfault in `kill` and `killall` when an invalid
signal name is given.
2023-06-07 00:24:09 +02:00
Emily Trau b8c847a3cd LibC: Add lutimes(2) and futimes(2) implementations
Needed for a port of nix
2023-06-06 15:55:42 +02:00
Jelle Raaijmakers 162a2b66eb Tests: Un-flake the recent TestEnvironment addition
Depending on stack values being correctly and deterministically
overwritten was a bit too optimistic, to be honest. This new logic uses
a value on the heap.
2023-06-06 12:20:38 +02:00
Jelle Raaijmakers f081a89cd9 LibC: Do not crash if putenv variable is invalid
Dr. POSIX says:

    Although the space used by string is no longer used once a new
    string which defines name is passed to putenv(), if any thread in
    the application has used getenv() to retrieve a pointer to this
    variable, it should not be freed by calling free(). If the changed
    environment variable is one known by the system (such as the locale
    environment variables) the application should never free the buffer
    used by earlier calls to putenv() for the same variable.

Applications _should_ not free the data passed to `putenv`, but they
_could_ in practice. I found that our Quake II port misbehaves in this
way, but does not crash on other platforms because glibc/musl `putenv`
does not assume that environment variables are correctly formatted.

The new behavior ignores environment variables without a '=' present,
and prevents excessively reading beyond the variable's name if the data
pointed to by the environment entry does not contain any null bytes.

With this change, our Quake II port no longer crashes when switching
from fullscreen to windowed mode.
2023-06-06 08:18:44 +02:00
Emily Trau e6970ec5f4 LibC: Add missing spec links for string APIs 2023-06-05 06:55:54 +02:00
Emily Trau 04b06afd39 LibC: Fix memmem signature compliance 2023-06-05 06:55:54 +02:00
Daniel Bertalan fb11645a9b LibC: Set up a dummy first stack frame in _start
This ensures that we stop walking the stack at `_entry` and don't
crash/panic when computing backtraces.
2023-06-01 09:04:20 +02:00
Daniel Bertalan 2e0e80db69 LibC: Ensure that struct group::gr_mem pointers are aligned
Otherwise, we get a UBSan crash in SystemServer before we could even
start running tests on AArch64.
2023-05-28 05:05:09 -06:00