2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-04-13 05:11:00 +00:00
|
|
|
#include "libc/assert.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/errno.h"
|
2022-11-02 05:36:03 +00:00
|
|
|
#include "libc/intrin/directmap.internal.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
#include "libc/nt/enum/filemapflags.h"
|
2020-11-28 20:01:51 +00:00
|
|
|
#include "libc/nt/enum/pageflags.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/nt/errors.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
#include "libc/nt/memory.h"
|
|
|
|
#include "libc/nt/runtime.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/struct/processmemorycounters.h"
|
2022-06-09 03:01:28 +00:00
|
|
|
#include "libc/nt/struct/securityattributes.h"
|
2021-02-05 02:24:33 +00:00
|
|
|
#include "libc/sysv/consts/map.h"
|
2022-04-13 05:11:00 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/sysv/consts/prot.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-04-15 06:39:48 +00:00
|
|
|
textwindows struct DirectMap sys_mmap_nt(void *addr, size_t size, int prot,
|
|
|
|
int flags, int fd, int64_t off) {
|
2022-03-24 07:05:59 +00:00
|
|
|
|
2023-08-19 13:41:06 +00:00
|
|
|
int64_t handle;
|
2023-03-29 05:16:26 +00:00
|
|
|
if (flags & MAP_ANONYMOUS) {
|
2022-04-13 05:11:00 +00:00
|
|
|
handle = kNtInvalidHandleValue;
|
2023-03-29 05:16:26 +00:00
|
|
|
} else {
|
|
|
|
handle = g_fds.p[fd].handle;
|
2022-04-13 05:11:00 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 13:41:06 +00:00
|
|
|
const struct NtSecurityAttributes *sec;
|
2022-05-12 13:43:59 +00:00
|
|
|
if ((flags & MAP_TYPE) != MAP_SHARED) {
|
2022-03-24 07:05:59 +00:00
|
|
|
sec = 0; // MAP_PRIVATE isn't inherited across fork()
|
|
|
|
} else {
|
|
|
|
sec = &kNtIsInheritable; // MAP_SHARED gives us zero-copy fork()
|
|
|
|
}
|
|
|
|
|
2022-04-13 05:11:00 +00:00
|
|
|
// nt will whine under many circumstances if we change the execute bit
|
|
|
|
// later using mprotect(). the workaround is to always request execute
|
|
|
|
// and then virtualprotect() it away until we actually need it. please
|
|
|
|
// note that open-nt.c always requests an kNtGenericExecute accessmask
|
2023-08-19 13:41:06 +00:00
|
|
|
int iscow = false;
|
|
|
|
struct ProtectNt fl;
|
2022-04-13 05:11:00 +00:00
|
|
|
if (handle != -1) {
|
2022-05-12 13:43:59 +00:00
|
|
|
if ((flags & MAP_TYPE) != MAP_SHARED) {
|
2022-04-13 05:11:00 +00:00
|
|
|
// windows has cow pages but they can't propagate across fork()
|
|
|
|
// that means we only get copy-on-write for the root process :(
|
2022-04-07 07:15:35 +00:00
|
|
|
fl = (struct ProtectNt){kNtPageExecuteWritecopy,
|
|
|
|
kNtFileMapCopy | kNtFileMapExecute};
|
2022-04-13 05:11:00 +00:00
|
|
|
iscow = true;
|
2022-03-24 07:05:59 +00:00
|
|
|
} else {
|
2022-04-13 05:11:00 +00:00
|
|
|
if ((g_fds.p[fd].flags & O_ACCMODE) == O_RDONLY) {
|
|
|
|
fl = (struct ProtectNt){kNtPageExecuteRead,
|
|
|
|
kNtFileMapRead | kNtFileMapExecute};
|
|
|
|
} else {
|
|
|
|
fl = (struct ProtectNt){kNtPageExecuteReadwrite,
|
|
|
|
kNtFileMapWrite | kNtFileMapExecute};
|
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-07-26 20:54:49 +00:00
|
|
|
unassert(flags & MAP_ANONYMOUS);
|
2022-04-13 05:11:00 +00:00
|
|
|
fl = (struct ProtectNt){kNtPageExecuteReadwrite,
|
|
|
|
kNtFileMapWrite | kNtFileMapExecute};
|
2022-03-24 07:05:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 13:41:06 +00:00
|
|
|
int e = errno;
|
|
|
|
struct DirectMap dm;
|
|
|
|
TryAgain:
|
2022-04-07 07:15:35 +00:00
|
|
|
if ((dm.maphandle = CreateFileMapping(handle, sec, fl.flags1,
|
|
|
|
(size + off) >> 32, (size + off), 0))) {
|
|
|
|
if ((dm.addr = MapViewOfFileEx(dm.maphandle, fl.flags2, off >> 32, off,
|
|
|
|
size, addr))) {
|
2023-08-19 13:41:06 +00:00
|
|
|
uint32_t oldprot;
|
2022-04-13 05:11:00 +00:00
|
|
|
if (VirtualProtect(addr, size, __prot2nt(prot, iscow), &oldprot)) {
|
|
|
|
return dm;
|
|
|
|
}
|
2022-11-02 05:36:03 +00:00
|
|
|
UnmapViewOfFile(dm.addr);
|
2021-02-05 02:24:33 +00:00
|
|
|
}
|
2022-03-24 07:05:59 +00:00
|
|
|
CloseHandle(dm.maphandle);
|
2023-08-19 13:41:06 +00:00
|
|
|
} else if (!(prot & PROT_EXEC) && //
|
|
|
|
(fl.flags2 & kNtFileMapExecute) && //
|
|
|
|
GetLastError() == kNtErrorAccessDenied) {
|
|
|
|
// your file needs to have been O_CREAT'd with exec `mode` bits in
|
|
|
|
// order to be mapped with executable permission. we always try to
|
|
|
|
// get execute permission if the kernel will give it to us because
|
|
|
|
// win32 would otherwise forbid mprotect() from elevating later on
|
|
|
|
fl.flags2 &= ~kNtFileMapExecute;
|
|
|
|
switch (fl.flags1) {
|
|
|
|
case kNtPageExecuteWritecopy:
|
|
|
|
fl.flags1 = kNtPageWritecopy;
|
|
|
|
break;
|
|
|
|
case kNtPageExecuteReadwrite:
|
|
|
|
fl.flags1 = kNtPageReadwrite;
|
|
|
|
break;
|
|
|
|
case kNtPageExecuteRead:
|
|
|
|
fl.flags1 = kNtPageReadonly;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
errno = e;
|
|
|
|
goto TryAgain;
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
2022-03-24 07:05:59 +00:00
|
|
|
|
2021-02-05 02:24:33 +00:00
|
|
|
dm.maphandle = kNtInvalidHandleValue;
|
2022-03-20 15:01:14 +00:00
|
|
|
dm.addr = (void *)(intptr_t)-1;
|
2021-01-25 21:08:05 +00:00
|
|
|
return dm;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|