Commit graph

700 commits

Author SHA1 Message Date
Andreas Kling
02c18bf6de LibC: Stub out posix_memalign() 2022-04-21 11:50:38 +02:00
Andreas Kling
ab878576bb LibC: Make nameinfo (NI_*) constants bitfield-friendly
These are supposed to be used as flags in a bitfield, so let's make
them powers of two.
2022-04-21 11:50:38 +02:00
Andreas Kling
a353ceecf1 LibC: Implement errno via a __errno_location() function
This matches how some other systems implement errno, and makes 3rd party
software that expect us to have __errno_location() work.
2022-04-21 11:49:48 +02:00
Leonardo Duarte
335fae9a71 LibC: Return early in time_to_tm for large time_t
POSIX says that localtime should fail with EOVERFLOW if the result
cannot be represented, which is the case for most large (in absolute
value) time_t inputs, since they overflow (or underflow) tm_year, which
is an int. This patch introduces this functionality. Previously, tm_year
just overflowed (or underflowed) silently.

Incidentally, this partially fixes #12729 without solving the root
problem, which is that time_to_tm is linear in its input to determine
the number of years since epoch.

This means that the bash port mktime test no longer times-out in 60s,
but it still fails (faster) in some other place. Due to underlying issue
in the algorithm, the worst case inputs still take a couple of seconds
on my machine.
2022-04-19 10:06:23 -04:00
cflip
b2ef7ee531 LibC+LibCore: Change a.m./p.m. to AM/PM 2022-04-15 08:24:20 -04:00
cflip
0500d49acb LibC+LibCore: Properly format 12-hour formatted hours
This fixes a small formatting issue where midnight and noon would
display as 00 when they should display as 12.
2022-04-15 08:24:20 -04:00
Tim Schumacher
3651da6a96 LibC: Define correct limits for long on x86_64 2022-04-14 03:12:56 +04:30
Tim Schumacher
3237efc661 LibC: Implement __fseterr 2022-04-03 19:15:14 +02:00
Tim Schumacher
4f706d819a LibC: Implement __freadptrinc 2022-04-03 19:15:14 +02:00
Tim Schumacher
89ed0649f7 LibC: Implement __freadahead 2022-04-03 19:15:14 +02:00
Tim Schumacher
9b543ddb16 LibC: Implement __freadptr 2022-04-03 19:15:14 +02:00
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Jelle Raaijmakers
cf1baddb67 LibC: Implement getdtablesize() 2022-03-29 22:49:27 +01:00
Tim Schumacher
aa7b6852ce LibC: Make prctl() a varargs function 2022-03-29 09:10:34 -07:00
Tim Schumacher
cdca6fc113 LibC: Make wchar size definitions available from stdint.h
POSIX describes WCHAR_MIN and WCHAR_MAX in stdint.h(0P), while
wchar.h(0P) only says "as described in stdint.h".

As there isn't a trivial path of "may make visible", just move it to a
shared header and include it from both files.
2022-03-29 15:19:39 +02:00
Daniel Bertalan
b23edd418c LibC: Fix inttypes.h macros for x86-64 and extend them
On x86-64, `int64_t` is defined to be `long` (not `long long`) , so for
printing, the "l" format specifier has to be used instead of i686's
"ll".

A couple of these macros weren't updated when the x86-64 target was
added, so using them produced warnings like this:

> warning: format specifies type 'long long' but the argument has type
> 'int64_t' (aka 'long') [-Wformat]
>
>       "DW_CFA_GNU_negative_offset_extended(%" PRId64 ")\n", offset);
>       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~

This commit changes the macros to be correct for both architectures, and
reorders them to be consistent and adds a couple missing ones for the
sake of completeness.
2022-03-28 22:24:05 +04:30
Tim Schumacher
4917a610ab LibC: Partially implement __fpending 2022-03-28 10:38:02 -07:00
Brian Gianforcaro
ecc0459f76 LibC: Fix potential double free in ttyname_r_for_directory
If we break out of the loop before we attempt to allocate again,
then we double free the memory pointed to by `name_path`.

Found by Static Analysis: Sonar Cloud
2022-03-27 16:43:05 -07:00
Liav A
de7566c2c4 LibC: Don't rely on ptsname and ttyname syscalls
Instead, to determine these values (both the pts name and tty name), use
other methods. For determining the new name of the allocated psuedo
terminal, use ioctl on a file descriptor we got after opening /dev/ptmx
with the TIOCGPTN option.
For determining the name of TTY, we enumerate both /dev/pts and /dev
directories to find matching inode number and matching device mode.
2022-03-22 20:26:05 +01:00
Liav A
5ffe2f117c Kernel/TTY: Implement TIOCGPTN ioctl for MasterPTY
This ioctl operation will allow userspace to determine the index number
of a MasterPTY after opening /dev/ptmx and actually getting an internal
file descriptor of MasterPTY.
2022-03-22 20:26:05 +01:00
safarp
b0b8d14a2c LibC: Automatically append null terminator in vswprintf 2022-03-20 08:40:36 -04:00
Tim Schumacher
aa9d6f17b6 Revert "LibC: Make WEOF a signed value on clang"
We have fixed Clang to define `wint_t` as an unsigned value.

This reverts commit ccb9cae8e9.
2022-03-19 00:11:45 +00:00
Lenny Maiorani
4c5e9f5633 Everywhere: Deduplicate day/month name constants
Day and month name constants are defined in numerous places. This
pulls them together into a single place and eliminates the
duplication. It also ensures they are `constexpr`.
2022-03-18 23:48:50 +00:00
Sahan Fernando
683de841e5 Kernel: Sandbox each GPU3DDevice file description into own host context 2022-03-14 17:38:18 +03:30
Brian Gianforcaro
01bd3c5563 LibC: Expose AK::timing_safe_compare via timingsafe_memcmp(..)
Ports / other userland often needs such an implementation to function.
Lets expose `AK::timing_safe_compare` under the same name used used in
OpenBSD / FreeBSD / Solaris and other projects.
2022-03-13 19:08:58 -07:00
Timothy Flynn
7fceb909a5 LibC: Use altzone for the current time zone offset during DST 2022-03-13 17:50:49 +01:00
Sahan Fernando
0e57f80460 LibC: Add ioctls for VirGL 2022-03-09 14:58:48 +03:30
Daniel Bertalan
ea52ba9fdc LibC: Set saved_str to null in strtok_r if no tokens were found
If we do not do this, the next call to strtok_r will start tokenizing
(and possibly modifying!) the memory pointed to by `saved_ptr`.
2022-03-08 23:30:47 +01:00
Tom
d124889698 LibC: Add IPv6 support to inet_ntop and inet_pton 2022-03-08 23:05:44 +01:00
Alex O'Brien
ad1065e213 LibC: Remove semicolon in definition of FD_ZERO
This causes problems in code of the form

  if (/* condition */)
    FD_ZERO(&thing);
  else
    do_other_thing();

Wrapping the call to memset() in a do/while block fixes the issue.
2022-03-06 13:00:42 +01:00
Ali Mohammad Pur
4bd01b7fe9 Kernel: Add support for SA_SIGINFO
We currently don't really populate most of the fields, but that can
wait :^)
2022-03-04 20:07:05 +01:00
Lucas CHOLLET
ddf9987c39 LibCore+LibC: Add wrapper for setenv
I also added a common interface with StringView compatible parameters:

int serenity_setenv(const char*, ssize_t, const char*, ssize_t, int)

This function is called by both C and C++ API for setenv().
2022-03-02 18:08:05 +01:00
Peter Ross
feb19646df LibC: Increase alignment for malloc() BigAllocationBlocks to 16 bytes
commit 5f3773b715 increased alignment
for ChunckedBlocks, but not for BigAllocationBlocks.

This is required to make SSE instructions work.
2022-03-02 11:40:37 +01:00
Peter Ross
36af2fdc4f LibC: Replace [[gnu::aligned(16)]] with C++ standard alignas(16) 2022-03-02 11:40:37 +01:00
Peter Ross
6c06b70911 LibC: Set PRI[xX]8/PRI[xX]16 macros to x and X
Described in:
  <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/
    inttypes.h.html>

The macros were first added in a7a456002e,
but it is not clear why the PRIx16/32 macros were defined as 'b' & 'w'.
PrintfImplementation was never able to parse these values.
2022-03-02 11:40:37 +01:00
Peter Ross
97cfb9934b LibC: Add TCP_MAXSEG to tcp.h
This is an unimplemented setsockopt. Defining it is sufficient to build
FFmpeg with networking.
2022-03-02 11:40:37 +01:00
Peter Ross
54fa06e85c LibC: Add IN_MULTICAST to in.h 2022-03-02 11:40:37 +01:00
Andrew Kaster
39f92fa131 LibC: Define offsetof in stddef.h instead of sys/cdefs.h
Just like Dr. POSIX ordered. Move the macro where it belongs, but make
sure it's visible to Userland files compiled with -DKERNEL. Parts of
LibEDID that are compiled into the Kernel use offsetof to parse the
EDID information given to us by hardware.
2022-02-28 20:28:01 -08:00
Andrew Kaster
fcb64311dc LibC: Don't export ALWAYS_INLINE from sys/cdefs.h
POSIX doesn't tell us to export a macro like this, and it's largely
going to never be defined when compiling a serenity-native C++ file,
since AK defines it already.

This does raise a strange issue where some futex-related helpers in
serenity.h are declared with ALWAYS_INLINE. Whether these helpers belong
in a C-visible header as file-static methods is questionable, but let's
work around the issue but adding some preprocessor magic to make sure
these declarations get the behavior they used to have without leaking
macros.
2022-02-28 20:28:01 -08:00
Lucas CHOLLET
8a9a9fac4d LibC: Add a wrapper for the getrusage syscall 2022-02-28 20:09:37 +01:00
Peter Ross
5b32b46ebc LibC: Do not write value when scanf assignment value is suppressed
This change has the positive side-effect of causing scanf to *segfault*
when a NULL pointer argument is passed to scanf.
e.g. sscanf(str, "%d", NULL);
2022-02-20 00:13:08 +03:30
Peter Ross
31079a56d7 LibC: Do not include suppressed assignments in scanf return value 2022-02-20 00:13:08 +03:30
Jakub Berkop
895a050e04 Kernel: Fixed argument passing for profiling_enable syscall
Arguments larger than 32bit need to be passed as a pointer on a 32bit
architectures. sys$profiling_enable has u64 event_mask argument,
which means that it needs to be passed as an pointer. Previously upper
32bits were filled by garbage.
2022-02-19 11:37:02 +01:00
Andreas Kling
5caeb8b747 LibC: Try to recover gracefully from failed LookupServer requests
gethostbyname() and gethostbyaddr() now set h_errno (per spec) and try
to recover and return (with an error) instead of choking in VERIFY()
whenever an I/O or protocol error occurs in the communication with
LookupServer.
2022-02-16 12:03:05 +01:00
Ali Mohammad Pur
18f68d1aea LibC: Stub out fmemopen() 2022-02-12 12:16:59 +01:00
Daniel Bertalan
3974cac148 LibELF: Implement support for DT_RELR relative relocations
The DT_RELR relocation is a relatively new relocation encoding designed
to achieve space-efficient relative relocations in PIE programs.

The description of the format is available here:
https://groups.google.com/g/generic-abi/c/bX460iggiKg/m/Pi9aSwwABgAJ

It works by using a bitmap to store the offsets which need to be
relocated. Even entries are *address* entries: they contain an address
(relative to the base of the executable) which needs to be relocated.
Subsequent even entries are *bitmap* entries: "1" bits encode offsets
(in word size increments) relative to the last address entry which need
to be relocated.

This is in contrast to the REL/RELA format, where each entry takes up
2/3 machine words. Certain kinds of relocations store useful data in
that space (like the name of the referenced symbol), so not everything
can be encoded in this format. But as position-independent executables
and shared libraries tend to have a lot of relative relocations, a
specialized encoding for them absolutely makes sense.

The authors of the format suggest an overall 5-20% reduction in the file
size of various programs. Due to our extensive use of dynamic linking
and us not stripping debug info, relative relocations don't make up such
a large portion of the binary's size, so the measurements will tend to
skew to the lower side of the spectrum.

The following measurements were made with the x86-64 Clang toolchain:

- The kernel contains 290989 relocations. Enabling RELR decreased its
  size from 30 MiB to 23 MiB.
- LibUnicodeData contains 190262 relocations, almost all of them
  relative. Its file size changed from 17 MiB to 13 MiB.
- /bin/WebContent contains 1300 relocations, 66% of which are relative
  relocations. With RELR, its size changed from 832 KiB to 812 KiB.

This change was inspired by the following blog post:
https://maskray.me/blog/2021-10-31-relative-relocations-and-relr
2022-02-11 18:07:53 +01:00
Andreas Kling
3ec71e1400 LibC: Remove debug spam in getaddrinfo() 2022-02-09 19:36:15 +01:00
Andrew Kaster
353e72ac9b LibC+Kernel: Remove global variable use from snprintf and fprintf
The global variable use in these functions is super thread-unsafe and
means that any concurrent calls to sprintf or fprintf in a process
could race with each other and end up writing unexpected results.
We can just replace the function + global variable with a lambda that
captures the relevant argument when calling printf_internal instead.
2022-02-09 06:22:33 +00:00
Ali Mohammad Pur
2b028f6faa LibRegex+LibJS: Avoid searching for more than one match in JS RegExps
All of JS's regular expression APIs only want a single match, so avoid
trying to produce more (which will be discarded anyway).
2022-02-05 00:09:32 +01:00
Brian Gianforcaro
2b14a11238 LibC: Add IN6_IS_ADDR_LINKLOCAL to in.h
I tried the OpenSSH port but it failed to compile due to a missing
definition of this macro. It's simple enough to add, and it's addition
allowed OpenSSH to compile once again.

I also went ahead and added spec comments for these macros as well.
2022-02-03 14:57:16 +01:00