Commit Graph

41 Commits

Author SHA1 Message Date
Dan Gohman
7fcc4f29df
Revamp and simplify the libpreopen code. (#110)
wasi-libc's copy of libpreopen has evolved so many local changes that
it's no longer worth keeping the upstream code structure and marking
changes with __wasilibc_unmodified_upstream.

This PR merges the source files into a single file, removes all
__wasilibc_unmodified_upstream code, eliminates the ability to
allocate multiple preopen lists, eliminates the need for
__wasilibc_init_preopen, eliminates the non-standard eaccess, and
makes several other cleanups. It also enables NDEBUG so that internal
assertions are disabled in release builds.
2019-11-04 14:56:35 -08:00
Dan Gohman
a8a5dbcf91
Ensure __environ is initialized even when it's empty. (#125)
POSIX requires `environ` to be a pointer to a NULL-terminated array of
pointers, so it itself can't be NULL.

This fixes a regression in src/functional/env.c in wasi-libc-test.
2019-10-28 10:34:57 -07:00
Dan Gohman
afbf94c39e
Call populate_args only if we actually need command-line arguments (#112)
* Link `populate_args` only if we actually need command-line arguments.

This avoids linking in the argv/argc initialization code,
and the __wasi_args_sizes_get and __wasi_args_get imports, in
programs that don't use command-line arguments. The way this works is,
if the user writes `int main(int argc, char *argv[])`, the argument
initialization code is loaded, and if they write `int main(void)`,
it's not loaded.

This promotes the `__original_main` mechanism into an effective contract
between the compiler and libc, which wasn't its original purpose,
however it seems to fit this purpose quite well.

* Document that `__original_main` may be the user's zero-arg `main`.
2019-10-24 17:30:46 -07:00
Dan Gohman
1f11b91e7f
Fix corner cases and style in the argv/argc code. (#118)
This fixes some potential memory leaks in pathological situations, and
updates the code to use `//`-style comments.
2019-10-22 11:25:19 -07:00
Dan Gohman
bdfbb54520
Call populate_environ only if we actually need environment variables. (#109)
* Link `populate_environ` only if we actually need environment variables.

This avoids linking in the environment variable initialization code,
and the __wasi_environ_sizes_get and __wasi_environ_get imports, in
programs that don't use environment variables.

This also removes the "___environ" (three underscores) alias symbol,
which is only in musl for backwards compatibility.

* Switch to //-style comments.

* If malloc fails, don't leave `__environ` pointing to an uninitialized buffer.

* Fix a memory leak if one malloc succeeds and the other fails.

* Use calloc to handle multiplication overflow.

This also handles the NULL terminator.

* Don't initialize __environ until everything has succeeded.

* Avoid leaking in case __wasi_environ_get fails.

* Handle overflow in the add too.

* Add #include <stdlib.h> for malloc etc.

* If the environment is empty, don't allocate any memory.
2019-10-22 06:02:44 -07:00
vms
d253aa3de2 Make sbrk(0) deterministic (#115)
* fix the behavior of sbrk(0)

* review changes
2019-10-21 15:44:23 -07:00
Dan Gohman
b59b83cbc2
Miscellaneous cleanups (#113)
* Tidy up some #include names.

* Move non-cloudlibc sources out of the cloudlibc directory.
2019-10-18 13:53:13 -07:00
vms
c54004fcce fix the typo (#108)
* fix the typo
2019-10-14 14:52:10 -07:00
Dan Gohman
d498fc3ddf Fix a warning about a spurious redundant static keyword. 2019-08-29 10:27:41 -07:00
Dan Gohman
b7ae2fe33b WASI libc's dirent does have t_type, so define _DIRENT_HAVE_D_TYPE.
Also update predefined-macros.txt.
2019-08-29 10:22:03 -07:00
Sam Clegg
92eaf255a1 Ensure argv is null terminated.
This matches the behavior of populate __environ.

See #27
2019-05-30 08:15:23 -07:00
Dan Gohman
6cddc9250b Implement truncate(2).
This adds a simple implementation of truncate in terms of open and
ftruncate.
2019-05-15 11:53:11 -07:00
Dan Gohman
256081f628 Move the mode_t macros into their own header.
This allows them to be defined by <fcntl.h> as well, as specified by
POSIX.

This fixes a part of src/api/fcntl.c.
2019-05-10 12:37:49 -07:00
Dan Gohman
861f0d2ec8 Define O_TTY_INIT.
Define it to 0, as several popular operating systems ignore this flag.

This fixes a part of src/api/fcntl.c.
2019-05-10 12:37:31 -07:00
Dan Gohman
a077330632 Update to libpreopen 8835639f27fc42d32096d59d294a0bbb857dc368.
This replaces our custom `unlink` wrapper with an upstream one. We still
end up replacing the entire body with local changes, but this makes it
easier to see what those changes are.

The other change here is a fix to ignore repeated '/'s in paths.
2019-05-09 09:16:56 -07:00
Dan Gohman
60cba39d11 Move the SEEK_* macros into their own header and use it from fcntl.h.
POSIX requires fcntl.h to define the SEEK_* macros, so this satisfies
that requirement. Also, this allows <stdio.h> to avoid including as much
unnecessary content.

This fixes one issue with src/api/fcntl.c.
2019-05-09 09:11:43 -07:00
Dan Gohman
1db93d1169 Switch from the cloudlibc inet_pton etc. to the musl versions.
These functions aren't specific to the underlying system call interface,
so they don't need to be in the "bottom half".

This also fixes src/functional/inet_pton.c and
src/regression/inet_pton-empty-last-field.c in musl's libc-test.
2019-05-09 09:10:57 -07:00
Dan Gohman
46ce01276f Don't declare dup in unistd.h.
Dup isn't defined in libc.a, so don't declare it.
2019-05-09 09:07:57 -07:00
Dan Gohman
382b944a2e Rename __wasilibc_rmfileat to __wasilibc_unlinkat.
The POSIX terminology is that directories are "removed" while files
are "unlinked".
2019-05-07 15:04:17 -07:00
Dan Gohman
124f6a4d0a Add __restrict qualifiers to the fstatat declaration.
This makes it consistent with its definition.
2019-05-07 15:03:26 -07:00
Dan Gohman
07858345bc Ensure that environ is always initialized.
Initialize `environ` even if there are no environment variables, so that
it alwasy points to a NULL-terminated array even if that array just
contains the NULL. This fixes src/functional/env.c.
2019-05-07 11:35:48 -07:00
Dan Gohman
c87e877f88 Move abort.c out of libc-bottom-half and into basics. 2019-05-07 11:34:41 -07:00
vms
d987aad467 use ENOMEM instead of EINVAL, set EINVAL if length == 0 2019-05-06 09:46:36 -07:00
vms
30e5a1fd1f move to __builtin_add_overflow 2019-05-06 09:46:36 -07:00
vms
ccfe4bda30 code deduplication 2019-05-06 09:46:36 -07:00
vms
4a8d1c7cf5 fix comment to correspond existing coding style 2019-05-06 09:46:36 -07:00
vms
20c48b57c7 fix mmap overflow and some other stuff 2019-05-06 09:46:36 -07:00
Dan Gohman
51969495ee Define TIME_UTC to be 1.
TIME_UTC is the only time base defined by the relevant standards. This
change doesn't preclude adding other time bases in the future.
2019-05-01 12:52:28 -07:00
Dan Gohman
1bdb356bef Define CLOCKS_PER_SEC to have type clock_t.
The C standard requires CLOCKS_PER_SEC to have type clock_t.
2019-05-01 12:52:28 -07:00
Dan Gohman
cf366c06d1 Optimize lseek in the tell case.
`lseek(x, 0, SEEK_CUR)` has no effect other than to return the current
file offset. The patch here uses a macro with `__builtin_constant_p` to
recognize this case and rewrite it to a library call that uses `fd_tell`
rather than `fd_seek`, so that programs that don't need actual seeking
don't end up importing `fd_seek`.

This is also the first usage of `__wasi_fd_tell` in WASI libc, so this
adds it to undefined-symbols.txt.
2019-04-30 16:19:05 -07:00
Dan Gohman
401012952d Declare getentropy in <sys/random.h>
Some systems, such as Darwin, only declare getentropy in <sys/random.h>,
so declare it there on WASI too for compatibility.

Also, give getentropy the underscore-prefix/weak-symbol treatment, as
it's not a standard-reserved identifier.
2019-04-30 16:07:49 -07:00
Dan Gohman
1cbedc6d7e Implement FD_SET, FD_CLR, etc.
Previously, FD_SET and friends were missing their actual definitions.
This provides definitions, entirely within the system headers in a
way that doesn't need instantiated out-of-line definitions.
2019-04-23 15:01:23 -07:00
Dan Gohman
538937e2c8 Remove capsicum.h; WASI libc doesn't support that API, even internally. 2019-04-23 15:01:05 -07:00
Dan Gohman
49f1a57cb8 Declare getrusage.
It's defined publicly in libc, so publicly declare it too.
2019-04-22 11:41:40 -07:00
Dan Gohman
1cc98f27f5 Update to cloudlibc 8835639f27fc42d32096d59d294a0bbb857dc368.
There's no functional change here; the only change is the patch I
submitted upstream, so we can remove some local changes.
2019-04-15 09:49:02 -07:00
Dan Gohman
65107e782f Rename README to README.md. 2019-04-15 09:47:16 -07:00
Dan Gohman
e18d69c801 Remove socket.c, which was a stub that always failed with ENOSYS. 2019-04-15 09:47:16 -07:00
Dan Gohman
ab594e4d51 Use the same indentation style as surrounding code. 2019-04-15 09:47:16 -07:00
Dan Gohman
7ba6adfc61 Fix miscellaneous lint warnings. 2019-04-15 09:47:16 -07:00
Dan Gohman
685d014446
Provide a public interface to preopened directory lookups. (#10)
* Provide a public interface to preopened directory lookups.

For users of especially non-C compilers, provide an API for looking up
preopened directories. This is used internally in WASI libc to translate
from eg. `open` to `openat`, but it can now also be used by user code.
2019-04-05 06:30:30 -07:00
Dan Gohman
320054e84f WASI libc prototype implementation.
This incoporates pieces from musl-libc, cloudlibc, cloudabi, libpreopen,
and dlmalloc, as well as a significant amount of new code.
2019-03-27 07:59:55 -07:00