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

zipos threading stress test #1011

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 13 additions & 2 deletions libc/calls/close.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "libc/calls/internal.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/struct/fd.internal.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
Expand Down Expand Up @@ -91,8 +92,18 @@ static int close_impl(int fd) {
* @vforksafe
*/
int close(int fd) {
int rc = close_impl(fd);
if (!__vforked) __releasefd(fd);
int rc;
if (__isfdkind(fd, kFdZip)) { // XXX IsWindows()?
BLOCK_SIGNALS;
__fds_lock();
rc = close_impl(fd);
if (!__vforked) __releasefd(fd);
__fds_unlock();
ALLOW_SIGNALS;
} else {
rc = close_impl(fd);
if (!__vforked) __releasefd(fd);
}
STRACE("close(%d) → %d% m", fd, rc);
return rc;
}
8 changes: 4 additions & 4 deletions libc/runtime/zipos-open.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,21 @@ void __zipos_postdup(int oldfd, int newfd) {
if (oldfd == newfd) {
return;
}
BLOCK_SIGNALS;
__fds_lock();
if (__isfdkind(newfd, kFdZip)) {
__zipos_free((struct ZiposHandle *)(intptr_t)g_fds.p[newfd].handle);
if (!__isfdkind(oldfd, kFdZip)) {
__fds_lock();
bzero(g_fds.p + newfd, sizeof(*g_fds.p));
__fds_unlock();
}
}
if (__isfdkind(oldfd, kFdZip)) {
__zipos_keep((struct ZiposHandle *)(intptr_t)g_fds.p[oldfd].handle);
__fds_lock();
__ensurefds_unlocked(newfd);
g_fds.p[newfd] = g_fds.p[oldfd];
__fds_unlock();
}
__fds_unlock();
ALLOW_SIGNALS;
}

/**
Expand Down
1 change: 1 addition & 0 deletions libc/runtime/zipos-read.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static ssize_t __zipos_read_impl(struct ZiposHandle *h, const struct iovec *iov,
if (b) memcpy(iov[i].iov_base, h->mem + y, b);
}
if (opt_offset == -1) {
unassert(y != SIZE_MAX);
atomic_store_explicit(&h->pos, y, memory_order_release);
}
return y - x;
Expand Down
58 changes: 58 additions & 0 deletions test/libc/runtime/zipos_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,61 @@ TEST(zipos, closeAfterVfork) {
ASSERT_SYS(0, 0, close(3));
ASSERT_SYS(EBADF, -1, close(3));
}

struct State {
int id;
int fd;
pthread_t thread;
};

#define READS() \
for (int i = 0; i < 4; ++i) { \
char buf[8]; \
ASSERT_SYS(0, 8, read(fd, buf, 8)); \
}
#define SEEKS() \
for (int i = 0; i < 4; ++i) { \
rc = lseek(fd, 8, SEEK_CUR); \
ASSERT_NE(rc, -1); \
}

static void *pthread_main(void *ptr) {
struct State *s = ptr;
struct State children[2];
int fd, rc;

fd = s->fd;
if (s->id < 8) {
for (int i = 0; i < 2; ++i) {
rc = dup(fd);
ASSERT_NE(-1, rc);
children[i].fd = rc;
children[i].id = 2 * s->id + i;
ASSERT_SYS(0, 0, pthread_create(&children[i].thread, NULL, pthread_main,
children + i));
}
}
if (s->id & 1) {
SEEKS();
READS();
} else {
READS();
SEEKS();
}
ASSERT_SYS(0, 0, close(fd));
if (s->id < 8) {
for (int i = 0; i < 2; ++i) {
ASSERT_SYS(0, 0, pthread_join(children[i].thread, NULL));
}
}
return NULL;
}

TEST(zipos, ultraPosixAtomicSeekRead) {
struct State s = { 1, 4 };
ASSERT_SYS(0, 3, open("/zip/libc/testlib/hyperion.txt", O_RDONLY));
ASSERT_SYS(0, 4, dup(3));
pthread_main((void *)&s);
EXPECT_EQ(960, lseek(3, 0, SEEK_CUR));
ASSERT_SYS(0, 0, close(3));
}