The fact that `hdr/elf_proxy.h` was generated with headergen was causing
dependency issues. This shouldn't be necessary, and this PR moves it out
into `include` to avoid needing headergen for an overlay build.
Assisted-by: Automated tooling, human reviewed.
Implement `statfs` and `fstatfs` functions on Linux.
We already have `struct statfs` and syscall wrappers defined (to
implement POSIX `<sys/statvfs.h>`). Use them to provide Linux-specific
functions as well.
This is a Linux extension that allows to obtain credentials of a peer
process connected to a socket, e.g. via passing `SO_PEERCRED` to
`getsockopt` function.
* Extract statfs/fstatfs Linux syscall wrappers to
`linux/syscall_wrappers` directory;
* Provide our own definition of `struct statfs` type and a corresponding
proxy header (to use system type in overlay mode);
* Migrate callers (e.g. statfs->statvfs translation) to use the new
syscall wrapper.
This would allow us to add Linux-specific (non-POSIX) `<sys/statfs.h>`
header as a next step.
Assisted by: Gemini, human-reviewed
Added struct passwd type definition, pwd.yaml header specification, and
parse_passwd_line utility returning ErrorOr<struct passwd> for
colon-separated
password file parsing with hermetic unit tests.
Note: Additional POSIX pwd.h functions (getpwuid, getpwnam, getpwuid_r,
getpwnam_r, fgetpwent) are omitted from this initial change and will be
added in follow-up PRs.
* Added libc/include/llvm-libc-types/struct_passwd.h and proxy header
* Added libc/include/pwd.yaml header specification
* Added pwd_utils.h and pwd_utils.cpp implementing ErrorOr<struct
passwd> parse_passwd_line
* Added pwd.h to target public headers for linux architectures
* Added unit tests in libc/test/src/pwd/pwd_utils_test.cpp
Assisted-by: Automated tooling, human reviewed.
Extend the `<dirent.h>` header with macro and types specified in recent
POSIX.1-2024:
* Add `posix_dent` structure, which has more fields than `dirent`, that
are actually used in practice. This struct would be identical to
`dirent` that we have on Linux
* Add `reclen_t` type for `d_reclen` field.
* Add macro `DT_BLK` and friends
Also, extend the tests to verify the values of `d_type` field, now that
we have the proper macro defined.
Assisted by: Gemini, human-verified
Add missing type dependencies to the sys_types header target in
libc/include/CMakeLists.txt for types defined in sys/types.yaml,
including __off_t, __off64_t, __uint64_t, off64_t, and BSD/System V
types added in commit 39df67d0ed.
Without these DEPENDS entries, CMake and Ninja do not copy the
corresponding type headers into the build and install directories,
causing missing header includes (e.g. __off64_t.h) when consuming
generated <sys/types.h>.
Assisted-by: Automated tooling, human reviewed.
Add header YAML specifications for memory.h, sys/poll.h, sys/unistd.h,
sys/fcntl.h, and sys/signal.h using public_includes.
POSIX.1-2017 and historical X/Open System Interfaces (XSI) standards
define these headers as alternate or legacy header locations that
forward to string.h, poll.h, unistd.h, fcntl.h, and signal.h
respectively. The YAML header specs allow hdrgen to generate the public
headers automatically with proper license headers and guards.
Updated Linux target headers.txt configuration files to register the new
public header targets for installation.
Assisted-by: Automated tooling, human reviewed.
Added the POSIX putenv() function and its internal support.
Implemented EnvironmentManager::put() to insert caller-provided
"name=value" strings directly into the environment array, managing
ownership correctly (caller retains ownership).
Registered for x86_64, aarch64, and riscv. Integration tests cover basic
operations, ownership semantics, validation, and edge cases.
Assisted-by: Automated tooling, human reviewed.
This PR adds a stub for `strptime` under
`LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS` for x86_64 and aarch64 as a
first step in implementing the function.
Extend SIGCHLD-specific `si_code` values, already available in
`signal-macros.h` with Linux values for the rest of the signal-specific
codes specified in the POSIX.1-2024.
Add missing header macro constants:
* endian-macros.h: __BYTE_ORDER, __LITTLE_ENDIAN, and __BIG_ENDIAN
* sys-stat-macros.h: S_IREAD, S_IWRITE, S_IEXEC, and ACCESSPERMS
* limits-macros.h: _POSIX_HOST_NAME_MAX and HOST_NAME_MAX
* unistd-macros.h: TEMP_FAILURE_RETRY
POSIX.1-2017 specifies _POSIX_HOST_NAME_MAX as 255. The Linux
gethostname(2) man page specifies HOST_NAME_MAX as 64. The
double-underscore byte order macros and TEMP_FAILURE_RETRY macro follow
the Linux and GNU C library conventions.
Assisted-by: Automated tooling, human reviewed.
Add missing type definitions to llvm-libc-types and sys/types.yaml:
* POSIX standard types: id_t, useconds_t
* BSD and System V compatibility typedefs: caddr_t, u_int, u_int8_t,
u_int16_t, u_long, u_short, ulong, ushort
* Large File Support (LFS) and Linux kernel internal types: ino64_t,
off64_t, __off_t, __off64_t, __uint64_t
POSIX.1-2017 defines id_t and useconds_t in sys/types.h. The BSD and
System V standards specify the legacy unsigned integer shorthand types,
and Linux kernel/glibc interfaces use the double-underscore prefix
offset and integer types.
Assisted-by: Automated tooling, human reviewed.
Implemented the pthread_setschedparam and pthread_getschedparam
functions.
Added Linux syscall wrappers:
* sched_setscheduler
* sched_getscheduler
* sched_getparam
Updated the Thread class to support getting and setting scheduling
parameters.
Added integration tests to verify the implementation.
Assisted-by: Automated tooling, human reviewed.
Add definitions for struct group_req and struct group_source_req in
llvm-libc-types and wire them into <netinet/in.h>.
Also define the multicast socket options using these structs:
- MCAST_JOIN_GROUP
- MCAST_LEAVE_GROUP
- MCAST_BLOCK_SOURCE
- MCAST_UNBLOCK_SOURCE
- MCAST_JOIN_SOURCE_GROUP
- MCAST_LEAVE_SOURCE_GROUP
Add layout assertions for struct group_req and struct group_source_req
to the netinet/in.h unit test.
Assisted by Gemini.
My cleverness with the union backfired as it introduced an
(architecture-specific) padding between before it. Drop the union to
avoid that.
This probably wouldn't break much as the structure exists just to
allocate space (and this made it larger), but we should fix it
nonetheless.
`gettimeofday` is supposed to be declared in the `<sys/time.h>` header,
not the `<time.h>` where we've had it previously. Move the declaration
to the correct header, and move the files with implementation and unit
tests accordingly.
Note that `gettimeofday` is removed removed from POSIX-2024, but let's
keep it available for now, since there's fair amount of existing code
that still uses it.
Implemented the alloca.h header for LLVM libc.
* libc/include/llvm-libc-macros/alloca-macros.h: Defined alloca macro
using __builtin_alloca.
* libc/include/alloca.yaml: Added header specification.
* libc/include/CMakeLists.txt,
libc/include/llvm-libc-macros/CMakeLists.txt: Registered the new header
and macro targets.
* libc/config/linux/*/headers.txt: Enabled the header for Linux targets
(x86_64, aarch64, arm, i386, riscv).
* libc/test/include/alloca_test.cpp, libc/test/include/CMakeLists.txt:
Added and registered functional unit tests.
Tested by running the new unit and hermetic tests.
Assisted-by: Automated tooling, human reviewed.
This patch fixes three issues with the previous implementation:
- the macro argument was being evaluated more than once
- casting to other types (uint32_t in particular) was an aliasing
violation
- it was casting to non-const pointers, resulting in compiler errors in
cases where the user passes a const ptr.
A statement expression fixes the first issue by using a temporary
variable. The second issue is fixed by using the appropriate members of
struct in6_addr. The last issue is fixed by dropping the cast
completely. This requires the user to pass a correctly types pointer (as
POSIX requires). Implementations keep it for compatibility with old code
passing void * and similar, but a quick survey shows that most modern
code passes the correct types. If this turns out to be an issue, we can
easily add the cast (to a const type) back.
Implementation notes:
- IN6_IS_ADDR_MULTICAST doesn't use the statement expression because the
argument is evaluated only once
- I use private helper macros (__IN6_IS_ADDR_UNSPECIFIED and
__IN6_IS_ADDR_LOOPBACK) so IN6_IS_ADDR_V4COMPAT does not nest statement
expressions
- I considered using private entrypoints (like we do with cpuset macros)
instead of statement expressions, but decided against it because these
macros are simpler and would thus result in a higher
boilerplate-to-useful-code ratio (particularly given our restriction on
calling other entrypoints)
Assisted by Gemini.
Implement inet_ntoa, reusing the internal net::ipv4_to_str helper that
backs inet_ntop.
The result is stored in a *thread-local* static buffer. A thread-local
buffer is not required for POSIX conformance, but there is some
precedent for that, both in llvm libc (e.g. `strsignal`) and in glibc
(whose `inet_ntoa` uses it). I'm doing the same for maximum
compatiblity.
Assisted by Gemini.
This seems to work, but I ran into this problem when trying to expand
hermetic tests.
The hdrgen-generated headers already use relative includes of this file.
Defined SA_NODEFER and SA_RESETHAND in Linux signal-macros.h.
Added unit tests in sigaction_test.cpp to verify the behavior of the new
flags. Also added a test case for SA_SIGINFO.
Assisted-by: Automated tooling, human reviewed.
This started out as a patch adding a syscall wrapper for the two
functions. It still does that, but along the way I (or rather AI)
discovered an edge case in the truncation of microsecond values, where
we could turn bogus values into seemingly valid ones by throwing away
the high-order bits.
Additionally, when writing a test case, I noticed that the (64-bit)
kernel returns EINVAL for these out-or-range values. Our check returns
EOVERFLOW in this case, so I also change our error code in order to
provide an uniform behavior for the test case.
Neither of these (the check and the EINVAL error code) are present in
glibc (which lets truncated usec values through and returns EOVERFLOW on
second truncation), but I think it's better to be consistent with the
64-bit behavior. The EOVERFLOW error also has no foundation in POSIX
whereas EINVAL is mentioned as an error for invalid microseconds.
While in there, I also define ITIMER_REAL, ITIMER_VIRTUAL, and
ITIMER_PROF in linux/sys-time-macros.h and update code to use them
instead of raw numbers.
Assisted by Gemini.
Implemented the BSD-extension functions defined in <err.h>:
- err, errx, verr, verrx
- warn, warnx, vwarn, vwarnx
Added an internal reporting helper in src/__support/Err. The err family
functions call internal::exit directly to satisfy [[noreturn]]
requirements. Enabled these entrypoints for aarch64, riscv, and x86_64.
Assisted-by: Automated tooling, human reviewed.
This patch implements if_nametoindex and if_indextoname for Linux.
Both functions work by creating a temporary AF_UNIX socket and issuing
ioctl calls (SIOCGIFINDEX and SIOCGIFNAME, respectively).
The main implementation details:
- for if_nametoindex, I check that the interface name fits within
IF_NAMESIZE before issuing the ioctl
- for if_indextoname, if the kernel returns ENODEV for an unknown index,
I map it to ENXIO to comply with POSIX requirements
- added the definition of struct ifreq to support these operations.
Similar to the other net structure definitions, I'm using an anonymous
union to avoid #defining members.
Assisted by Gemini.
The function converts from IPv4/6 addresses to their string forms. The
complication comes from the v6 part due to address compression and
v4-mapping.
The traditional implementation of this is to sprintf the address into a
temporary buffer. We cannot do that here due to the ban on calling other
entry points. Using the lower-level constructs is possible, but a
straight forward application of IntegerToString does not result in
particularly impressive performance: 50%-100% slower than what's in this
patch (and slower than glibc). It also doesn't make the code much
smaller as the majority of it is dedicated to v6 compression.
The IPv6 implementation uses a temporary buffer, but it also has a
fast-path which skips the buffer if its size is guaranteed to be
sufficient. The IPv4 implementation also has a fast path, but the
fallback here is to compute the precise length of the string instead
(the buffer option was slower and IPv4 makes it easier to compute the
precise length).
The v6 compression code is unrolled to make it easier to optimize. While
it is possible to write a single printing loop that handles all cases,
its branchiness does not make it amenable for optimization.
The resulting function is faster than its glibc counterpart, though the
exact ratio varies depending on the input:
- random IPv4: 15% faster
- random IPv6: 6% faster
- "::": 50% faster
Added missing macros to elf.h:
* sys-auxv-macros.h: Added AT_RSEQ_FEATURE_SIZE and AT_RSEQ_ALIGN.
* elf.yaml: Added PT_AARCH64_MEMTAG_MTE, PN_XNUM, ELFCLASSNUM, EV_NUM,
and AT_* macros (via sys-auxv-macros.h). Also added NN_* and NT_* note
macros (including NT_FPREGSET and NT_SIGINFO) to match system headers.
* CMakeLists.txt: Added sys_auxv_macros dependency to elf target.
Assisted-by: Automated tooling, human reviewed.
This patch adds struct ipv6_mreq and defines various IPv6 socket option
macros in <netinet/in.h>. I've also moved the existing option
definitions to the yaml file.
Assisted by Gemini.
This patch implements sockatmark as a simple wrapper around
ioctl(SIOCATMARK).
I don't test it with actual out of band data, as that requires a TCP
connection, which is more complicated to set up.
Assisted by Gemini.
This PR adds basic implementation for cabs and cabsf in LLVM libc and
also extend current MPC infrastructure to support cabs operation.
---------
Signed-off-by: jinge90 <ge.jin@intel.com>
This PR contains an incremental implementation of `realpath` that just
simplifies path traversals (e.g. `"/a/./../b"` -> `"/b"`). The complete
implementation would perform `getcwd`/`statx`/`readlinkat` to actually
resolve symlinks, validate paths, etc. This PR aims to add as much
functionality/error handling as possible without making any syscalls.
This implementation lives in `libc/src/stdlib/linux/`, since follow-on
PRs will rely on linux-specific syscalls. For testing purposes, I've
added the entrypoint for x86-64 linux when
`LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS` is enabled.
Follow-on PRs to add: path validation (e.g.
paths must exist), support for cwd-relative paths, and symlink
resolution.
Now that public_includes is supported in header yaml files, we don't
need custom .h.def templates just to include another header.
This patch removes link.h.def, string.h.def, and ucontext.h.def and
moves their inclusions directly into
their yaml definitions:
- link.yaml: add elf.h
- string.yaml: add strings.h
- sys/ucontext.yaml: add ucontext.h
Assisted by Gemini.
This patch implements CPU_AND, CPU_OR, CPU_XOR, and CPU_EQUAL macros
(along with their _S variants) from sched.h.
The implementation follows existing patterns by adding internal entry
points (__sched_andcpuset, __sched_orcpuset, __sched_xorcpuset, and
__sched_cpuequal) that perform bitwise operations on cpu_set_t. For
__sched_cpuequal, I use inline_memcmp instead of a manual loop.
Assisted by Gemini.
This patch adds support for a public_includes stanza in header YAML
files, allowing generated public headers to include other public
headers.
I use this to make arpa/inet.h include netinet/in.h, and netinet/in.h
include sys/socket.h. While POSIX doesn't strictly require these headers
to include each other, it permits it, and many real-world applications
depend on this behavior.
To make this work, I also needed to reorder the header targets in
include/CMakeLists.txt so that targets sys_socket and netinet_in are
defined before targets declare dependencies on them.
There are a couple of other files that could use this feature (right now
they handle it through the header template), but I'll do that as a
separate patch.
Assisted by Gemini.
Added the POSIX unsetenv() function and its internal support.
Implemented EnvironmentManager::unset() to remove a variable by name,
free the string if allocated, and compact the array.
Updated EnvironmentManager to synchronize the public global environ
pointer when transitioning to managed storage.
Registered for x86_64, aarch64, and riscv. Integration tests cover basic
operations and edge cases.
Assisted-by: Automated tooling, human reviewed.
This PR adds carg and cargf function to libc complex and also add test
cases to cover some special inputs.
---------
Signed-off-by: jinge90 <ge.jin@intel.com>
This patch adds struct ip_mreq, ip_mreq_source, ip_mreqn, ip_opts, and
ip_msfilter to <netinet/in.h>, along with IP level socket option macros
(IP_TOS, IP_TTL, IP_ADD_MEMBERSHIP, etc.).
I add basic unit tests verifying the size and member offsets of the new
structures against standard layout expectations, mainly to make sure
that the files are used /somewhere/.
Assisted by Gemini.
Added loff_t type definition. This is a Linux extension required for
large file offsets.
* llvm-libc-types/loff_t.h: Include linux/loff_t.h on Linux.
* llvm-libc-types/linux/loff_t.h: Added Linux definition using
__kernel_loff_t.
* llvm-libc-types/CMakeLists.txt: Include linux subdirectory and add
dependency for loff_t.
* llvm-libc-types/linux/CMakeLists.txt: Registered loff_t.
* sys/types.yaml: Added loff_t to sys/types.
* libc/include/CMakeLists.txt: Added dependency to sys_types target.
Assisted-by: Automated tooling, human reviewed.
Added the POSIX standard functions basename and dirname under a new libgen.h header. The implementations modify the input path in-place using cpp::string_view to determine boundaries safely.
Added find_last_not_of to cpp::string_view to support trailing slash removal.
Implemented:
libc/include/libgen.yaml, libgen.h.def: Public API definitions.
libc/src/libgen/basename.cpp, dirname.cpp: Generic implementations.
libc/test/src/libgen/: Unit and hermetic tests.
Registered the new entrypoints for all active Linux targets (x86_64, aarch64, arm, riscv) and added docgen configuration.
The tests are skipped when using ASan because death tests do not currently work with them.
Assisted-by: Automated tooling, human reviewed.
This patch implements CPU_CLR and CPU_CLR_S macros following the
existing CPU_SET pattern. The macro just forwards to an internal entry
point.
Assisted by Gemini.
Added the POSIX standard functions basename and dirname under a new
libgen.h header. The implementations modify the input path in-place
using cpp::string_view to determine boundaries safely.
Added find_last_not_of to cpp::string_view to support trailing slash
removal.
Implemented:
* libc/include/libgen.yaml, libgen.h.def: Public API definitions.
* libc/src/libgen/basename.cpp, dirname.cpp: Generic implementations.
* libc/test/src/libgen/: Unit and hermetic tests.
Registered the new entrypoints for all active Linux targets (x86_64,
aarch64, arm, riscv) and added docgen configuration.
Assisted-by: Automated tooling, human reviewed.