Workaround MAP_GROWSDOWN unavailability on WSL

This commit is contained in:
Justine Tunney 2022-11-02 01:38:06 -07:00
parent bd6069deb5
commit fc96af058b
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
4 changed files with 62 additions and 47 deletions

View file

@ -380,16 +380,20 @@ static noasan inline void *Mmap(void *addr, size_t size, int prot, int flags,
// with 4kb guards like a sane multithreaded production system.
// however this 1mb behavior oddly enough is smart enough to not
// apply if the mapping is a manually-created guard page.
int e = errno;
if ((dm = sys_mmap(p + size - PAGESIZE, PAGESIZE, prot,
f | MAP_GROWSDOWN_linux, fd, off))
.addr == MAP_FAILED) {
return MAP_FAILED;
.addr != MAP_FAILED) {
_npassert(sys_mmap(p, PAGESIZE, PROT_NONE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
.addr == p);
dm.addr = p;
return FinishMemory(p, size, prot, flags, fd, off, f, x, n, dm);
} else if (errno == ENOTSUP) {
// WSL doesn't support MAP_GROWSDOWN
needguard = true;
errno = e;
}
_npassert(sys_mmap(p, PAGESIZE, PROT_NONE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
.addr == p);
dm.addr = p;
return FinishMemory(p, size, prot, flags, fd, off, f, x, n, dm);
} else {
if (IsFreebsd()) {
f |= MAP_STACK_freebsd;