Fix ZipOS deadlock/segfault (#1011)

This change adds a new stress test for ZipOS which helped
us improve the locking semantics in open() and close().
This commit is contained in:
Jōshin 2023-12-14 22:59:20 -05:00 committed by GitHub
parent 897fa6ac00
commit 8a10ccf9c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 6 deletions

View file

@ -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));
}