Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix make THREAD_MODEL=posix #311

Merged
merged 24 commits into from
Aug 9, 2022
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
626362a
Fixes for the THREAD_MODEL=posix build
sbc100 Jul 18, 2022
384da08
Fix expected symbols from previous commit
abrown Jul 26, 2022
0e7956a
Enable `lock` in `random.c` when threads are enabled
abrown Jul 26, 2022
2ab390a
Disable `aio.h` when compiling for threads
abrown Jul 26, 2022
b2f40de
Specify the TLS model until LLVM 15 is released
abrown Jul 27, 2022
9d3b1e4
Rename `__wasi_libc_pthread_self` to `__wasilibc_pthread_self`
abrown Jul 28, 2022
04f6c3f
Add different sets of expected output based on THREAD_MODEL
abrown Jul 28, 2022
d2d3e5c
Re-add trailing whitespace to `predefined-macros.txt`
abrown Jul 28, 2022
83a80a7
Protect `preopens.c` against concurrent access
abrown Jul 28, 2022
247bfeb
Only build thread-capable wasi-libc on latest version of Clang
abrown Jul 28, 2022
0acb15f
Use `thrd_sleep` from MUSL instead of aliasing `nanosleep`
abrown Jul 28, 2022
240c35f
Define `pthread_setcancelstate` in `THREAD_MODEL=posix` builds
abrown Jul 28, 2022
e74d263
Define a Wasm global to store `pthread_self`
abrown Aug 1, 2022
0878de7
Remove `g_needs_dynamic_alloc` global
abrown Aug 1, 2022
db89382
Document the state of pthread support
abrown Aug 1, 2022
53a4091
review: de-duplicate symbols based on #314
abrown Aug 1, 2022
6539705
review: only define `__wasilibc_cwd_{un}lock` when needed
abrown Aug 1, 2022
3784170
review: add #ifdefs to `__pthread_setcancelstate`
abrown Aug 1, 2022
2339e18
review: add additional #ifdefs to `pthread_self.c`
abrown Aug 1, 2022
80d9a43
review: put lock definition behind #ifdef _REENTRANT
abrown Aug 8, 2022
d5bf750
review: remove pthread_setcancelstate.c
abrown Aug 8, 2022
eb50290
review: re-fix indentation
abrown Aug 8, 2022
1d6a27c
review: alias __clock_nanosleep in bottom half
abrown Aug 8, 2022
831de19
review: remove extra line
abrown Aug 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Protect preopens.c against concurrent access
abrown committed Aug 1, 2022
commit 83a80a7180104afe07f65c978074ad6cbe7e73ea
19 changes: 15 additions & 4 deletions libc-bottom-half/sources/preopens.c
Original file line number Diff line number Diff line change
@@ -2,14 +2,11 @@
//! environment, with associated path prefixes, which can be used to map
//! absolute paths to capabilities with relative paths.

#ifdef _REENTRANT
//#error "__wasilibc_register_preopened_fd doesn't yet support multiple threads"
#endif

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <lock.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
@@ -32,6 +29,10 @@ static preopen *preopens;
static size_t num_preopens;
static size_t preopen_capacity;

/// Access to the the above preopen must be protected in the presence of
/// threads.
static volatile int lock[1];

#ifdef NDEBUG
#define assert_invariants() // assertions disabled
#else
@@ -55,13 +56,15 @@ static void assert_invariants(void) {

/// Allocate space for more preopens. Returns 0 on success and -1 on failure.
static int resize(void) {
LOCK(lock);
size_t start_capacity = 4;
size_t old_capacity = preopen_capacity;
size_t new_capacity = old_capacity == 0 ? start_capacity : old_capacity * 2;

preopen *old_preopens = preopens;
preopen *new_preopens = calloc(sizeof(preopen), new_capacity);
if (new_preopens == NULL)
UNLOCK(lock);
return -1;

memcpy(new_preopens, old_preopens, num_preopens * sizeof(preopen));
@@ -70,6 +73,7 @@ static int resize(void) {
free(old_preopens);

assert_invariants();
UNLOCK(lock);
return 0;
}

@@ -97,21 +101,26 @@ static const char *strip_prefixes(const char *path) {
///
/// This function takes ownership of `prefix`.
static int internal_register_preopened_fd(__wasi_fd_t fd, const char *relprefix) {
LOCK(lock);

// Check preconditions.
assert_invariants();
assert(fd != AT_FDCWD);
assert(fd != -1);
assert(relprefix != NULL);

if (num_preopens == preopen_capacity && resize() != 0)
UNLOCK(lock);
return -1;

char *prefix = strdup(strip_prefixes(relprefix));
if (prefix == NULL)
UNLOCK(lock);
return -1;
preopens[num_preopens++] = (preopen) { prefix, fd, };

assert_invariants();
UNLOCK(lock);
return 0;
}

@@ -166,6 +175,7 @@ int __wasilibc_find_abspath(const char *path,
// recently added preopens take precedence over less recently addded ones.
size_t match_len = 0;
int fd = -1;
LOCK(lock);
for (size_t i = num_preopens; i > 0; --i) {
const preopen *pre = &preopens[i - 1];
const char *prefix = pre->prefix;
@@ -182,6 +192,7 @@ int __wasilibc_find_abspath(const char *path,
*abs_prefix = prefix;
}
}
UNLOCK(lock);

if (fd == -1) {
errno = ENOENT;