Commit Graph

89 Commits

Author SHA1 Message Date
michaelrj-google
8180ea8694
[libc] Add bind function (#74014)
This patch adds the bind function to go with the socket function. It
also cleans up a lot of socket related data structures.
2023-12-12 13:36:11 -08:00
Schrodinger ZHU Yifan
81e3e7e5d4
[libc] [search] implement hcreate(_r)/hsearch(_r)/hdestroy(_r) (#73469)
This patch implements `hcreate(_r)/hsearch(_r)/hdestroy(_r)` as
specified in https://man7.org/linux/man-pages/man3/hsearch.3.html.

Notice that `neon/asimd` extension is not yet added in this patch. 

- The implementation is largely simplified from rust's
[`hashbrown`](https://github.com/rust-lang/hashbrown/blob/master/src/raw/mod.rs)
as we only consider fix-sized insertion-only hashtables. Technical
details are provided in code comments.

- This patch also contains a portable string hash function, which is
derived from [`aHash`](https://github.com/tkaitchuck/aHash)'s fallback
routine. Not using any SIMD acceleration, it has a good enough quality
(passing all SMHasher tests) and is not too bad in speed.

- Some general functionalities are added, such as `memory_size`,
`offset_to`(alignment), `next_power_of_two`, `is_power_of_two`.
`ctz/clz` are extended to support shorter integers.
2023-11-28 21:02:25 -05:00
Joseph Huber
e0b702ffc2
[libc] Fix nanosleep definition in the posix spec (#67855)
Summary:
The POSIX standard expects the first argument to this function to be
constant, e.g. https://man7.org/linux/man-pages/man2/nanosleep.2.html.
This fixes that problem and also corrects an obvious problem with
enabling this for offloading.
2023-09-29 17:35:10 -05:00
Alfred Persson Forsberg
d3e045934a
Revert "[libc] Add limits.h"
limits.h currently interferes with Clang's limits.h. include_next
emits a warning because it is a GNU extension. Will re add this once
we figure out a good solution.

This reverts commits 13bbca8d69,
002cba0329, and
0fb3066873.
2023-08-17 06:21:50 +02:00
Alfred Persson Forsberg
0fb3066873 [libc] Add limits.h
This header contains implementation specific constants.

The compiler already provides its own limits.h with numerical limits
conforming to freestanding ISO C. But it is missing extensions like
POSIX, and does for example not include <linux/limits.h> which is
expected on a Linux system, therefore, an LLVM libc implementation of
limits.h is needed for hosted (__STDC_HOSTED__) environments.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D156961
2023-08-14 01:35:44 +01:00
Mikhail R. Gadelha
c9783d2bda [libc] Add support to compile some syscalls on 32 bit platform
This patch adds a bunch of ifdefs to handle the 32 bit versions of
some syscalls, which often only append a 64 to the name of the syscall
(with exception of SYS_lseek -> SYS_llseek and SYS_futex ->
SYS_futex_time64)

This patch also tries to handle cases where wait4 is not available
(as in riscv32): to implement wait, wait4 and waitpid when wait4 is
not available, we check for alternative wait calls and ultimately rely
on waitid to implement them all.

In riscv32, only waitid is available, so we need it to support this
platform.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D148371
2023-08-03 10:08:01 -03:00
Alfred Persson Forsberg
08da9ceb64 [libc] Fix argument types for {f,}truncate specs
The current argument types are currently switched around for ftruncate
and truncate. Currently passes tests because the internal definitions
inside the __llvm_libc namespace are fine.

Reviewed By: michaelrj, thesamesam, sivachandra

Differential Revision: https://reviews.llvm.org/D152664
2023-06-12 18:00:35 +01:00
Noah Goldstein
e0b8f98b1f Add some missing [[noreturn]] attributes
Missing in header for `pthread_exit` and `exit`.

Missing in spec file for `pthread_exit`.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D151143
2023-05-22 15:54:19 -05:00
Michael Jones
ee17fd7d46 [libc] add socket function
This patch adds the function "socket" from the header "sys/socket". It's
a simple syscall wrapper, and I plan on adding the related functions in
a followup patch.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149622
2023-05-03 11:01:11 -07:00
Noah Goldstein
0432b85d8e [LIBC] Implement remainder of posix 'sched.h' minus SCHED_SPORADIC
Includes macros:
    linux/SCHED_OTHER // posix req
    linux/SCHED_FIFO // posix req
    linux/SCHED_RR // posix req
    linux/SCHED_BATCH
    linux/SCHED_ISO
    linux/SCHED_IDLE
    linux/SCHED_DEADLINE

Includes types:
    struct sched_param { int sched_priority; }

Includes functions:
    sched_setparam
    sched_getparam
    sched_setscheduler
    sched_getscheduler
    sched_get_priority_max
    sched_get_priority_min
    sched_rr_get_interval

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D148069
2023-04-20 14:53:41 -05:00
Michael Jones
1c261e360f [libc] Add implementation of getchar
added getchar and getchar_unlocked which are just wrappers getc and getc_unlocked respectively.

Reviewed By: sivachandra, lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D147919
2023-04-14 15:40:05 -07:00
Noah Goldstein
203aff2df3 [LIBC] Implement sched_yield()
Implements: https://linux.die.net/man/2/sched_yield

Possibly we don't need the return value check / errno as according to
both the manpage (and current linux source) `sched_yield` cannot fail.

Reviewed By: sivachandra, michaelrj

Differential Revision: https://reviews.llvm.org/D147985
2023-04-12 19:04:37 -05:00
Caslyn Tonelli
1a3e760eda [libc] Add swab implementation
Swab implementation is added to libc/src/unistd.

Differential Revision: https://reviews.llvm.org/D147970
2023-04-10 23:37:51 +00:00
Raman Tenneti
d0d6d78bbd [libc] Implement ntohl and ntohs
Per spec:

https://pubs.opengroup.org/onlinepubs/9699919799/functions/ntohl.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/ntohs.html

Co-authored-by: Jeff Bailey <jbailey@google.com>

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D144506
2023-02-22 10:40:38 -08:00
Raman Tenneti
fbe210dc7a [libc] Implement htonl and htons
Per spec:
* https://pubs.opengroup.org/onlinepubs/9699919799/functions/htonl.html
* https://pubs.opengroup.org/onlinepubs/9699919799/functions/htons.html

Also adds UInt16Type and UInt32Type to spec.td

Co-authored-by: Jeff Bailey <jbailey@google.com>

Reviewed By: sivachandra, jeffbailey, rtenneti

Differential Revision: https://reviews.llvm.org/D143795
2023-02-16 10:12:18 -08:00
Raman Tenneti
dbd31935ed [libc] cleanup changes to gettimeofday.
+ Deleted duplicate definitions of StructTimeVal and StructTimeValPtr.
+ Caled syscall  clock_gettime to get timespec data.
+ Added tests to test for sleeping 200 and 1000 microseconds.
+ Fixed comments from https://reviews.llvm.org/D137881

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138064
2022-11-15 15:06:35 -08:00
Raman Tenneti
35155ee7d3 Removed tabs. 2022-11-11 18:11:06 -08:00
Raman Tenneti
78f172e45a [libc] Implement gettimeofday
Implement gettimeofday per
.../onlinepubs/9699919799/functions/gettimeofday.html.
This call clock_gettime to implement gettimeofday function.

Tested:
Limited unit test: This makes a call and checks that no error was
returned. Used nanosleep for 100 microseconds and verfified it
returns a value that elapses more than 100 microseconds and less
than 300 microseconds.

Co-authored-by: Jeff Bailey <jeffbailey@google.com>

Differential Revision: https://reviews.llvm.org/D137881
2022-11-11 18:02:33 -08:00
Siva Chandra Reddy
6a6101958a [libc] Add implementation of getc, getc_unlocked and fgetc_unlocked.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137507
2022-11-07 21:47:29 +00:00
Alex Brachet
5fd03c8176 [libc] Implement getopt
Differential Revision: https://reviews.llvm.org/D133487
2022-10-31 16:55:53 +00:00
Alex Brachet
d6ac84bce8 Revert "[libc] Implement getopt"
This reverts commit a678f86351.
2022-10-27 06:47:24 +00:00
Alex Brachet
a678f86351 [libc] Implement getopt
Differential Revision: https://reviews.llvm.org/D133487
2022-10-27 06:23:33 +00:00
Siva Chandra Reddy
6faf40bb75 [libc] Add the header sys/types.h.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D136642
2022-10-25 00:24:49 +00:00
Siva Chandra Reddy
be4e425758 [libc] Add select.h and the implementation of the select function for Linux.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D136375
2022-10-22 03:17:48 +00:00
Siva Chandra Reddy
67957368ae [libc] Add implementation of sigaltstack for linux.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D135949
2022-10-18 22:04:30 +00:00
Siva Chandra Reddy
b2a294bcf8 [libc] Add termios.h and the implementation of functions declared in it.
Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D136143
2022-10-18 20:53:00 +00:00
Siva Chandra Reddy
8b0e84a6f6 [libc] Add implementation of the POSIX getcwd function.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D135905
2022-10-14 16:20:59 +00:00
Siva Chandra Reddy
02a543db66 [libc] Add a simple implementation of the posix_spawn function.
The implementation currently ignores all spawn attributes. Support for
them will be added in future changes.

A simple allocator for integration tests has been added so that the
integration test for posix_spawn can use the
posix_spawn_file_actions_add* functions.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D135752
2022-10-13 18:47:47 +00:00
Siva Chandra Reddy
28943d617a [libc] Add POSIX functions posix_spawn_file_actions_*.
Namely, posix_spawn_file_actions_addclose,
posix_spawn_file_actions_adddup2, posix_spawn_file_actions_addopen,
posix_spawn_file_actions_destroy, posix_spawn_file_actions_init have
been added.

Reviewed By: michaelrj, lntue

Differential Revision: https://reviews.llvm.org/D135603
2022-10-11 04:54:44 +00:00
Michael Jones
7639ba6906 [libc] add isatty
The isatty function uses the side effects of an ioctl call to determine
if a specific file descriptor is a terminal. I chose TIOCGETD (get line
discipline of terminal) because it didn't require any new structs.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D135618
2022-10-10 15:20:46 -07:00
Michael Jones
1ac525b28b [libc] add sysconf with pagesize
The sysconf function has many options, this patch adds the basic funtion
and the pagesize option. More options will be added in future patches.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135409
2022-10-10 14:51:45 -07:00
Siva Chandra Reddy
438e59182b [libc] Add implementation of pthread_atfork.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D135432
2022-10-10 18:28:43 +00:00
Michael Jones
07793f95c4 [libc] add strsignal and refactor message mapping
The logic for strsignal and strerror is very similar, so I've moved them
both to use a shared utility (MessageMapper) for the basic
functionality.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135322
2022-10-07 11:11:53 -07:00
Michael Jones
9015810648 [libc] add kill
Add the kill syscall wrapper and tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135406
2022-10-06 15:25:56 -07:00
Siva Chandra Reddy
3f965818b6 [libc] Add POSIX execv and execve functions.
The POSIX global variable environ has also been added.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D135351
2022-10-06 19:50:23 +00:00
Siva Chandra Reddy
995105de1b [libc] Add the POSIX waitpid function and the BSD wait4 function.
Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D135225
2022-10-05 07:38:55 +00:00
Siva Chandra Reddy
e3638e83db [libc] Add a minimal implementation of the POSIX fork function.
A very simple and minimal implementation of fork is added. Future
changes will add more functionality to satisfy POSIX and Linux
requirements.

An implementation of wait and a few support macros in sys/wait.h
have also been added to help with testing the fork function.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D135131
2022-10-04 19:12:08 +00:00
Michael Jones
1801c356f6 [libc] add syscall function
Add the syscall wrapper function and tests. It's implemented using a
macro to guarantee the minimum number of arguments.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D134919
2022-09-30 15:46:28 -07:00
Siva Chandra Reddy
215c9fa4de [libc] Re-enable functions from signal.h and re-enable abort.
They were disabled because we were including linux/signal.h from our
signal.h. Linux's signal.h is not designed to be included from user
programs as it causes a lot of non-standard name pollution. Also, it is
not self-contained. This change defines types and macros relevant for
signal related syscalls within libc's headers and removes inclusion of
Linux headers.

This patch enables the funtions only for x86_64. They will be enabled
for aarch64 also in a follow up patch after testing.

Reviewed By: abrachet, lntue

Differential Revision: https://reviews.llvm.org/D134567
2022-09-30 07:31:50 +00:00
Siva Chandra Reddy
545b954251 [libc] Add GNU extension functions sched_getaffinity and sched_setaffinity.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D134858
2022-09-29 20:31:46 +00:00
Michael Jones
b49d626cb4 [libc] add clock_gettime
Add the clock_gettime syscall wrapper and tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D134773
2022-09-29 10:23:21 -07:00
Siva Chandra Reddy
3367539010 [libc] Add implementation of pthread_once.
The existing thrd_once function has been refactored so that the
implementation can be shared between thrd_once and pthread_once
functions.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D134716
2022-09-28 06:54:48 +00:00
Raman Tenneti
8f1e362ee9 Implement nanosleep per https://pubs.opengroup.org/onlinepubs/009695399/basedefs/time.h.html
Tested:
Limited unit test: This makes a call and checks that no error was
returned, but we currently don't have the ability to ensure that
time has elapsed as expected.

Co-authored-by: Jeff Bailey <jeffbailey@google.com>

Reviewed By: sivachandra, jeffbailey

Differential Revision: https://reviews.llvm.org/D134095
2022-09-24 00:13:58 +00:00
Michael Jones
85c70da732 [libc] add madvise and posix_madvise
Add the madvise and posix_madvise syscall wrappers and tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D134498
2022-09-23 09:36:40 -07:00
Michael Jones
47b724048b [libc] add mprotect
Add the mprotect syscall wrapper and tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D134497
2022-09-23 09:36:37 -07:00
Siva Chandra Reddy
4f1474daec [libc] Add implementations of POSIX getpid, getppid, getuid, geteuid functions.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D134338
2022-09-21 18:41:20 +00:00
Siva Chandra Reddy
e310f8bddf [libc] Add implementation of functions stat, fstat and lstat.
All supporting type and macro definitions have also been added.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D134262
2022-09-21 18:35:02 +00:00
Jeff Bailey
faeb237bac [libc] Fix TWS issues in .td files
Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D134256
2022-09-20 14:25:53 +00:00
Siva Chandra Reddy
7fb96fb5d3 [libc] Add implementation of POSIX "uname" function.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D134065
2022-09-16 21:21:29 +00:00
Siva Chandra Reddy
9050a59c66 [libc][Obvious] Fix typo in struct rlimit name - remove the "_t" suffix. 2022-09-16 21:07:17 +00:00