mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 11:18:30 +00:00
Add torture test for zipos file descriptors
This change hardens the code for opening /zip/ files using the system call interface. Thread safety and signal safety has been improved for file descriptors in general. We now document fixed addresses that are needed for low level allocations.
This commit is contained in:
parent
579080cd4c
commit
e466dd0553
44 changed files with 2981 additions and 307 deletions
|
@ -16,27 +16,15 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/safemacros.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/cmpxchg.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/pthread.h"
|
||||
#include "libc/intrin/spinlock.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/alloca.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/auxv.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/zip.h"
|
||||
#include "libc/zipos/zipos.internal.h"
|
||||
|
||||
|
@ -63,52 +51,52 @@ static void __zipos_munmap_unneeded(const uint8_t *base, const uint8_t *cdir,
|
|||
|
||||
/**
|
||||
* Returns pointer to zip central directory of current executable.
|
||||
* @asyncsignalsafe (TODO: verify this)
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
*/
|
||||
struct Zipos *__zipos_get(void) {
|
||||
int fd;
|
||||
char *path;
|
||||
ssize_t size;
|
||||
const char *msg;
|
||||
static bool once;
|
||||
sigset_t neu, old;
|
||||
struct Zipos *res;
|
||||
const char *progpath;
|
||||
static struct Zipos zipos;
|
||||
uint8_t *map, *base, *cdir;
|
||||
static pthread_mutex_t lock;
|
||||
pthread_mutex_lock(&lock);
|
||||
if (_cmpxchg(&once, false, true)) {
|
||||
sigfillset(&neu);
|
||||
if (!once) {
|
||||
__zipos_lock();
|
||||
progpath = GetProgramExecutableName();
|
||||
if ((fd = open(progpath, O_RDONLY)) != -1) {
|
||||
if ((size = getfiledescriptorsize(fd)) != SIZE_MAX &&
|
||||
if ((size = getfiledescriptorsize(fd)) != -1ul &&
|
||||
(map = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0)) != MAP_FAILED) {
|
||||
if ((base = FindEmbeddedApe(map, size))) {
|
||||
size -= base - map;
|
||||
} else {
|
||||
base = map;
|
||||
}
|
||||
if ((cdir = GetZipCdir(base, size))) {
|
||||
if ((cdir = GetZipCdir(base, size)) && _cmpxchg(&zipos.map, 0, base)) {
|
||||
__zipos_munmap_unneeded(base, cdir, map);
|
||||
zipos.map = base;
|
||||
zipos.cdir = cdir;
|
||||
STRACE("__zipos_get(%#s)", progpath);
|
||||
msg = "ok";
|
||||
} else {
|
||||
munmap(map, size);
|
||||
STRACE("__zipos_get(%#s) → eocd not found", progpath);
|
||||
msg = "eocd not found";
|
||||
}
|
||||
} else {
|
||||
msg = "map failed";
|
||||
}
|
||||
close(fd);
|
||||
} else {
|
||||
STRACE("__zipos_get(%#s) → open failed %m", progpath);
|
||||
msg = "open failed";
|
||||
}
|
||||
once = true;
|
||||
__zipos_unlock();
|
||||
STRACE("__zipos_get(%#s) → %s% m", progpath, msg);
|
||||
}
|
||||
if (zipos.cdir) {
|
||||
res = &zipos;
|
||||
} else {
|
||||
res = 0;
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
return res;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue