diff --git a/libc/runtime/mmap.c b/libc/runtime/mmap.c index 89e61c02d..bad8023ee 100644 --- a/libc/runtime/mmap.c +++ b/libc/runtime/mmap.c @@ -487,7 +487,9 @@ void *mmap(void *addr, size_t size, int prot, int flags, int fd, int64_t off) { void *res; size_t toto; if (__isfdkind(fd, kFdZip)) { - return _weaken(__zipos_mmap)(addr, size, prot, flags, (struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle, off); + return _weaken(__zipos_mmap)( + addr, size, prot, flags, + (struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle, off); } #if defined(SYSDEBUG) && (_KERNTRACE || _NTTRACE) if (IsWindows()) { diff --git a/libc/zipos/mmap.c b/libc/zipos/mmap.c index 67025f4f2..f727edc0e 100644 --- a/libc/zipos/mmap.c +++ b/libc/zipos/mmap.c @@ -27,8 +27,8 @@ #include "libc/sysv/errfuns.h" #include "libc/zipos/zipos.internal.h" -#define IP(X) (intptr_t)(X) -#define VIP(X) (void *)IP(X) +#define IP(X) (intptr_t)(X) +#define VIP(X) (void *)IP(X) /** * Map zipos file into memory. See mmap. @@ -46,7 +46,8 @@ * it does not need to be 64kb aligned. * @return virtual base address of new mapping, or MAP_FAILED w/ errno */ -void *__zipos_mmap(void *addr, size_t size, int prot, int flags, struct ZiposHandle *h, int64_t off) { +void *__zipos_mmap(void *addr, size_t size, int prot, int flags, + struct ZiposHandle *h, int64_t off) { if (VERY_UNLIKELY(!!(flags & MAP_ANONYMOUS))) { STRACE("MAP_ANONYMOUS zipos mismatch"); return VIP(einval()); @@ -65,9 +66,10 @@ void *__zipos_mmap(void *addr, size_t size, int prot, int flags, struct ZiposHan return MAP_FAILED; } const int64_t beforeOffset = __zipos_lseek(h, 0, SEEK_CUR); - if ((beforeOffset == -1) || (__zipos_read(h, &(struct iovec){outAddr, size}, 1, off) == -1) || - (__zipos_lseek(h, beforeOffset, SEEK_SET) == -1) || - ((prot != tempProt) && (mprotect(outAddr, size, prot) == -1))) { + if ((beforeOffset == -1) || + (__zipos_read(h, &(struct iovec){outAddr, size}, 1, off) == -1) || + (__zipos_lseek(h, beforeOffset, SEEK_SET) == -1) || + ((prot != tempProt) && (mprotect(outAddr, size, prot) == -1))) { const int e = errno; munmap(outAddr, size); errno = e; diff --git a/libc/zipos/zipos.internal.h b/libc/zipos/zipos.internal.h index 6cb78404d..d6565e0d5 100644 --- a/libc/zipos/zipos.internal.h +++ b/libc/zipos/zipos.internal.h @@ -48,7 +48,8 @@ ssize_t __zipos_write(struct ZiposHandle *, const struct iovec *, size_t, int64_t __zipos_lseek(struct ZiposHandle *, int64_t, unsigned) _Hide; int __zipos_fcntl(int, int, uintptr_t) _Hide; int __zipos_notat(int, const char *) _Hide; -void *__zipos_mmap(void *, uint64_t, int32_t, int32_t, struct ZiposHandle *, int64_t) _Hide; +void *__zipos_mmap(void *, uint64_t, int32_t, int32_t, struct ZiposHandle *, + int64_t) _Hide; #ifdef _NOPL0 #define __zipos_lock() _NOPL0("__threadcalls", __zipos_lock) diff --git a/test/libc/runtime/mmap_test.c b/test/libc/runtime/mmap_test.c index 4b2fc5812..873bab8c0 100644 --- a/test/libc/runtime/mmap_test.c +++ b/test/libc/runtime/mmap_test.c @@ -226,7 +226,9 @@ TEST(mmap, ziposCannotBeAnonymous) { int fd; void *p; ASSERT_NE(-1, (fd = open(ziposLifePath, O_RDONLY), "%s", ziposLifePath)); - EXPECT_SYS(EINVAL, MAP_FAILED, (p = mmap(NULL, 0x00010000, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0))); + EXPECT_SYS(EINVAL, MAP_FAILED, + (p = mmap(NULL, 0x00010000, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, + fd, 0))); close(fd); } @@ -234,7 +236,8 @@ TEST(mmap, ziposCannotBeShared) { int fd; void *p; ASSERT_NE(-1, (fd = open(ziposLifePath, O_RDONLY), "%s", ziposLifePath)); - EXPECT_SYS(EACCES, MAP_FAILED, (p = mmap(NULL, 0x00010000, PROT_READ, MAP_SHARED, fd, 0))); + EXPECT_SYS(EACCES, MAP_FAILED, + (p = mmap(NULL, 0x00010000, PROT_READ, MAP_SHARED, fd, 0))); close(fd); } @@ -245,8 +248,9 @@ TEST(mmap, ziposCow) { int fd; void *p; ASSERT_NE(-1, (fd = open(ziposLifePath, O_RDONLY), "%s", ziposLifePath)); - EXPECT_NE(MAP_FAILED, (p = mmap(NULL, 0x00010000, PROT_READ, MAP_PRIVATE, fd, 0))); - EXPECT_STREQN("ELF", ((const char *)p)+1, 3); + EXPECT_NE(MAP_FAILED, + (p = mmap(NULL, 0x00010000, PROT_READ, MAP_PRIVATE, fd, 0))); + EXPECT_STREQN("ELF", ((const char *)p) + 1, 3); EXPECT_NE(-1, munmap(p, 0x00010000)); EXPECT_NE(-1, close(fd)); } @@ -259,14 +263,14 @@ TEST(mmap, ziposCowFileMapReadonlyFork) { void *p; ASSERT_NE(-1, (fd = open(ziposLifePath, O_RDONLY), "%s", ziposLifePath)); EXPECT_NE(MAP_FAILED, (p = mmap(NULL, 4, PROT_READ, MAP_PRIVATE, fd, 0))); - EXPECT_STREQN("ELF", ((const char *)p)+1, 3); + EXPECT_STREQN("ELF", ((const char *)p) + 1, 3); ASSERT_NE(-1, (ws = xspawn(0))); if (ws == -2) { - ASSERT_STREQN("ELF", ((const char *)p)+1, 3); + ASSERT_STREQN("ELF", ((const char *)p) + 1, 3); _exit(0); } EXPECT_EQ(0, ws); - EXPECT_STREQN("ELF", ((const char *)p)+1, 3); + EXPECT_STREQN("ELF", ((const char *)p) + 1, 3); EXPECT_NE(-1, munmap(p, 6)); EXPECT_NE(-1, close(fd)); } @@ -279,7 +283,8 @@ TEST(mmap, ziposCowFileMapFork) { void *p; char lol[4]; ASSERT_NE(-1, (fd = open(ziposLifePath, O_RDONLY), "%s", ziposLifePath)); - EXPECT_NE(MAP_FAILED, (p = mmap(NULL, 6, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0))); + EXPECT_NE(MAP_FAILED, + (p = mmap(NULL, 6, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0))); memcpy(p, "parnt", 6); ASSERT_NE(-1, (ws = xspawn(0))); if (ws == -2) { @@ -308,8 +313,7 @@ TEST(mmap, cow) { path); EXPECT_EQ(5, write(fd, "hello", 5)); EXPECT_NE(-1, fdatasync(fd)); - EXPECT_NE(MAP_FAILED, - (p = mmap(NULL, 5, PROT_READ, MAP_PRIVATE, fd, 0))); + EXPECT_NE(MAP_FAILED, (p = mmap(NULL, 5, PROT_READ, MAP_PRIVATE, fd, 0))); EXPECT_STREQN("hello", p, 5); EXPECT_NE(-1, munmap(p, 5)); EXPECT_NE(-1, close(fd));