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
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/assert.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/bits/likely.h"
|
2020-09-03 12:44:37 +00:00
|
|
|
#include "libc/bits/weaken.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/calls/internal.h"
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
#include "libc/calls/strace.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/dce.h"
|
2021-10-15 02:36:49 +00:00
|
|
|
#include "libc/errno.h"
|
2021-02-01 11:33:13 +00:00
|
|
|
#include "libc/intrin/asan.internal.h"
|
2022-04-13 05:11:00 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/log/backtrace.internal.h"
|
2021-10-15 02:36:49 +00:00
|
|
|
#include "libc/log/libfatal.internal.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/log/log.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/process.h"
|
|
|
|
#include "libc/nt/runtime.h"
|
|
|
|
#include "libc/nt/struct/processmemorycounters.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/rand/rand.h"
|
2021-02-24 04:23:19 +00:00
|
|
|
#include "libc/runtime/directmap.internal.h"
|
2022-04-13 05:11:00 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2021-09-04 20:20:47 +00:00
|
|
|
#include "libc/runtime/memtrack.internal.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/map.h"
|
2022-04-13 05:11:00 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/sysv/consts/prot.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2021-10-14 00:27:13 +00:00
|
|
|
#define IP(X) (intptr_t)(X)
|
|
|
|
#define VIP(X) (void *)IP(X)
|
|
|
|
#define ALIGNED(p) (!(IP(p) & (FRAMESIZE - 1)))
|
|
|
|
#define ADDR(x) ((int64_t)((uint64_t)(x) << 32) >> 16)
|
|
|
|
#define SHADE(x) (((intptr_t)(x) >> 3) + 0x7fff8000)
|
|
|
|
#define FRAME(x) ((int)((intptr_t)(x) >> 16))
|
|
|
|
|
2022-04-13 05:11:00 +00:00
|
|
|
static wontreturn void OnUnrecoverableMmapError(const char *s) {
|
2022-04-15 06:39:48 +00:00
|
|
|
if (weaken(__die)) weaken(__die)();
|
|
|
|
STRACE("%s %m", s);
|
|
|
|
__restorewintty();
|
|
|
|
_Exit(199);
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
noasan static bool IsMapped(char *p, size_t n) {
|
|
|
|
return OverlapsImageSpace(p, n) || IsMemtracked(FRAME(p), FRAME(p + (n - 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
noasan static bool NeedAutomap(char *p, size_t n) {
|
|
|
|
return !p || OverlapsArenaSpace(p, n) || OverlapsShadowSpace(p, n) ||
|
|
|
|
IsMapped(p, n);
|
|
|
|
}
|
|
|
|
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
noasan static bool ChooseMemoryInterval(int x, int n, int *res) {
|
2021-10-14 00:27:13 +00:00
|
|
|
int i;
|
|
|
|
if (_mmi.i) {
|
|
|
|
i = FindMemoryInterval(&_mmi, x);
|
|
|
|
if (i < _mmi.i) {
|
|
|
|
if (x + n < _mmi.p[i].x) {
|
|
|
|
*res = x;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
while (++i < _mmi.i) {
|
|
|
|
if (_mmi.p[i].x - _mmi.p[i - 1].y > n) {
|
|
|
|
*res = _mmi.p[i - 1].y + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (INT_MAX - _mmi.p[i - 1].y >= n) {
|
|
|
|
*res = _mmi.p[i - 1].y + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
*res = x;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
noasan static bool Automap(int n, int *res) {
|
|
|
|
*res = -1;
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
if (ChooseMemoryInterval(FRAME(kAutomapStart), n, res)) {
|
2021-10-14 00:27:13 +00:00
|
|
|
assert(*res >= FRAME(kAutomapStart));
|
|
|
|
if (*res + n <= FRAME(kAutomapStart + (kAutomapStart - 1))) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2022-04-15 06:39:48 +00:00
|
|
|
STRACE("mmap(%.12p, %p) ENOMEM (automap interval exhausted)", ADDR(*res),
|
|
|
|
ADDR(n + 1));
|
2021-10-14 00:27:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-15 06:39:48 +00:00
|
|
|
STRACE("mmap(%.12p, %p) ENOMEM (automap failed)", ADDR(*res), ADDR(n + 1));
|
2021-10-14 00:27:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-04-18 07:01:26 +00:00
|
|
|
noasan static size_t GetMemtrackSize(struct MemoryIntervals *mm) {
|
|
|
|
size_t i, n;
|
|
|
|
for (n = i = 0; i < mm->i; ++i) {
|
|
|
|
n += ((size_t)(mm->p[i].y - mm->p[i].x) + 1) << 16;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2021-10-15 02:36:49 +00:00
|
|
|
static noasan void *MapMemory(void *addr, size_t size, int prot, int flags,
|
|
|
|
int fd, int64_t off, int f, int x, int n) {
|
|
|
|
struct DirectMap dm;
|
|
|
|
dm = sys_mmap(addr, size, prot, f, fd, off);
|
|
|
|
if (UNLIKELY(dm.addr == MAP_FAILED)) {
|
|
|
|
if (IsWindows() && (flags & MAP_FIXED)) {
|
2022-04-13 05:11:00 +00:00
|
|
|
OnUnrecoverableMmapError(
|
|
|
|
"can't recover from MAP_FIXED errors on Windows");
|
2021-10-15 02:36:49 +00:00
|
|
|
}
|
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
|
|
|
if (UNLIKELY(dm.addr != addr)) {
|
2022-04-13 05:11:00 +00:00
|
|
|
OnUnrecoverableMmapError("KERNEL DIDN'T RESPECT MAP_FIXED");
|
2021-10-15 02:36:49 +00:00
|
|
|
}
|
|
|
|
if (!IsWindows() && (flags & MAP_FIXED)) {
|
|
|
|
if (UntrackMemoryIntervals(addr, size)) {
|
2022-04-13 05:11:00 +00:00
|
|
|
OnUnrecoverableMmapError("FIXED UNTRACK FAILED");
|
2021-10-15 02:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-13 05:11:00 +00:00
|
|
|
if (TrackMemoryInterval(&_mmi, x, x + (n - 1), dm.maphandle, prot, flags,
|
|
|
|
false, false, off, size)) {
|
2021-10-15 02:36:49 +00:00
|
|
|
if (sys_munmap(addr, n) == -1) {
|
2022-04-13 05:11:00 +00:00
|
|
|
OnUnrecoverableMmapError("TRACK MUNMAP FAILED");
|
2021-10-15 02:36:49 +00:00
|
|
|
}
|
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
|
|
|
if (weaken(__asan_map_shadow) && !OverlapsShadowSpace(addr, size)) {
|
|
|
|
weaken(__asan_map_shadow)((intptr_t)addr, size);
|
|
|
|
}
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps memory from system, one frame at a time.
|
|
|
|
*
|
|
|
|
* This is useful on Windows since it allows us to partially unmap or
|
|
|
|
* punch holes into existing mappings.
|
|
|
|
*/
|
2021-11-12 23:12:18 +00:00
|
|
|
static textwindows dontinline noasan void *MapMemories(char *addr, size_t size,
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
int prot, int flags,
|
|
|
|
int fd, int64_t off,
|
2022-04-15 06:39:48 +00:00
|
|
|
int f, int x, int n) {
|
|
|
|
size_t i, m;
|
2022-03-20 15:01:14 +00:00
|
|
|
int64_t oi, sz;
|
2021-10-15 02:36:49 +00:00
|
|
|
struct DirectMap dm;
|
2022-04-13 05:11:00 +00:00
|
|
|
bool iscow, readonlyfile;
|
2022-04-15 06:39:48 +00:00
|
|
|
m = (size_t)(n - 1) << 16;
|
|
|
|
assert(m < size);
|
|
|
|
assert(m + FRAMESIZE >= size);
|
2022-03-20 15:01:14 +00:00
|
|
|
oi = fd == -1 ? 0 : off + m;
|
|
|
|
sz = size - m;
|
|
|
|
dm = sys_mmap(addr + m, sz, prot, f, fd, oi);
|
2022-04-13 05:11:00 +00:00
|
|
|
if (dm.addr == MAP_FAILED) return MAP_FAILED;
|
|
|
|
iscow = (flags & MAP_PRIVATE) && fd != -1;
|
|
|
|
readonlyfile = (flags & MAP_SHARED) && fd != -1 &&
|
|
|
|
(g_fds.p[fd].flags & O_ACCMODE) == O_RDONLY;
|
2021-10-15 02:36:49 +00:00
|
|
|
if (TrackMemoryInterval(&_mmi, x + (n - 1), x + (n - 1), dm.maphandle, prot,
|
2022-04-13 05:11:00 +00:00
|
|
|
flags, readonlyfile, iscow, oi, sz) == -1) {
|
|
|
|
OnUnrecoverableMmapError("MapMemories unrecoverable #1");
|
2021-10-15 02:36:49 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < m; i += FRAMESIZE) {
|
2022-03-20 15:01:14 +00:00
|
|
|
oi = fd == -1 ? 0 : off + i;
|
|
|
|
sz = FRAMESIZE;
|
|
|
|
dm = sys_mmap(addr + i, sz, prot, f, fd, oi);
|
2021-10-15 02:36:49 +00:00
|
|
|
if (dm.addr == MAP_FAILED ||
|
|
|
|
TrackMemoryInterval(&_mmi, x + i / FRAMESIZE, x + i / FRAMESIZE,
|
2022-04-13 05:11:00 +00:00
|
|
|
dm.maphandle, prot, flags, readonlyfile, iscow, oi,
|
|
|
|
sz) == -1) {
|
|
|
|
OnUnrecoverableMmapError("MapMemories unrecoverable #2");
|
2021-10-15 02:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (weaken(__asan_map_shadow) && !OverlapsShadowSpace(addr, size)) {
|
|
|
|
weaken(__asan_map_shadow)((intptr_t)addr, size);
|
|
|
|
}
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
2021-10-14 00:27:13 +00:00
|
|
|
* Beseeches system for page-table entries, e.g.
|
2020-06-15 14:18:57 +00:00
|
|
|
*
|
2021-10-14 00:27:13 +00:00
|
|
|
* char *m;
|
|
|
|
* m = mmap(NULL, FRAMESIZE, PROT_READ | PROT_WRITE,
|
|
|
|
* MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
* munmap(m, FRAMESIZE);
|
2020-09-28 08:13:56 +00:00
|
|
|
*
|
2021-10-14 00:27:13 +00:00
|
|
|
* @param addr should be 0 to let your memory manager choose address;
|
|
|
|
* unless MAP_FIXED or MAP_FIXED_NOREPLACE are specified in flags
|
|
|
|
* in which case this function will do precicely as you ask, even
|
|
|
|
* if p=0 (in which you need -fno-delete-null-pointer-checks); it
|
|
|
|
* needs to be 64kb aligned because it's a wise choice that sadly
|
|
|
|
* needs to be made mandatory because of Windows although you can
|
|
|
|
* use __sys_mmap() to circumvent it on System Five in which case
|
|
|
|
* runtime support services, e.g. asan memory safety, could break
|
|
|
|
* @param size must be >0 and needn't be a multiple of FRAMESIZE, but
|
|
|
|
* will be rounded up to FRAMESIZE automatically if MAP_ANONYMOUS
|
|
|
|
* is specified
|
|
|
|
* @param prot can have PROT_READ/PROT_WRITE/PROT_EXEC/PROT_NONE/etc.
|
2020-06-15 14:18:57 +00:00
|
|
|
* @param flags can have MAP_ANONYMOUS, MAP_SHARED, MAP_PRIVATE, etc.
|
2021-10-14 00:27:13 +00:00
|
|
|
* @param fd is an open()'d file descriptor, whose contents shall be
|
|
|
|
* made available w/ automatic reading at the chosen address and
|
|
|
|
* must be -1 if MAP_ANONYMOUS is specified
|
2021-02-03 08:10:12 +00:00
|
|
|
* @param off specifies absolute byte index of fd's file for mapping,
|
|
|
|
* should be zero if MAP_ANONYMOUS is specified, and sadly needs
|
|
|
|
* to be 64kb aligned too
|
2020-06-15 14:18:57 +00:00
|
|
|
* @return virtual base address of new mapping, or MAP_FAILED w/ errno
|
|
|
|
*/
|
2021-10-14 00:27:13 +00:00
|
|
|
noasan void *mmap(void *addr, size_t size, int prot, int flags, int fd,
|
|
|
|
int64_t off) {
|
2022-04-18 15:54:42 +00:00
|
|
|
#if defined(SYSDEBUG) && (_KERNTRACE || _NTTRACE)
|
|
|
|
if (IsWindows()) {
|
|
|
|
STRACE("mmap(%p, %'zu, %s, %s, %d, %'ld) → ...", addr, size,
|
|
|
|
DescribeProtFlags(prot), DescribeMapFlags(flags), fd, off);
|
|
|
|
}
|
|
|
|
#endif
|
2022-04-13 05:11:00 +00:00
|
|
|
void *res;
|
|
|
|
char *p = addr;
|
2020-06-15 14:18:57 +00:00
|
|
|
struct DirectMap dm;
|
2022-04-18 07:01:26 +00:00
|
|
|
size_t virtualused, virtualneed;
|
2021-10-14 00:27:13 +00:00
|
|
|
int a, b, i, f, m, n, x;
|
2022-04-15 06:39:48 +00:00
|
|
|
if (UNLIKELY(!size)) {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("size=0");
|
|
|
|
res = VIP(einval());
|
2022-04-15 06:39:48 +00:00
|
|
|
} else if (UNLIKELY(!IsLegalSize(size))) {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("size isn't 48-bit");
|
|
|
|
res = VIP(einval());
|
2022-04-15 06:39:48 +00:00
|
|
|
} else if (UNLIKELY(!IsLegalPointer(p))) {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("p isn't 48-bit");
|
|
|
|
res = VIP(einval());
|
2022-04-15 06:39:48 +00:00
|
|
|
} else if (UNLIKELY(!ALIGNED(p))) {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("p isn't 64kb aligned");
|
|
|
|
res = VIP(einval());
|
2022-04-15 06:39:48 +00:00
|
|
|
} else if (UNLIKELY(fd < -1)) {
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
STRACE("mmap(%.12p, %'zu, fd=%d) EBADF", p, size, fd);
|
2022-04-13 05:11:00 +00:00
|
|
|
res = VIP(ebadf());
|
2022-04-15 06:39:48 +00:00
|
|
|
} else if (UNLIKELY(!((fd != -1) ^ !!(flags & MAP_ANONYMOUS)))) {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("fd anonymous mismatch");
|
|
|
|
res = VIP(einval());
|
2022-04-15 06:39:48 +00:00
|
|
|
} else if (UNLIKELY(!(!!(flags & MAP_PRIVATE) ^ !!(flags & MAP_SHARED)))) {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("MAP_SHARED ^ MAP_PRIVATE");
|
|
|
|
res = VIP(einval());
|
2022-04-15 06:39:48 +00:00
|
|
|
} else if (UNLIKELY(off < 0)) {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("neg off");
|
|
|
|
res = VIP(einval());
|
2022-04-15 06:39:48 +00:00
|
|
|
} else if (UNLIKELY(INT64_MAX - size < off)) {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("too large");
|
|
|
|
res = VIP(einval());
|
2022-04-15 06:39:48 +00:00
|
|
|
} else if (UNLIKELY(!ALIGNED(off))) {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("p isn't 64kb aligned");
|
|
|
|
res = VIP(einval());
|
2022-04-15 06:39:48 +00:00
|
|
|
} else if ((flags & MAP_FIXED_NOREPLACE) && IsMapped(p, size)) {
|
2022-04-13 05:11:00 +00:00
|
|
|
#ifdef SYSDEBUG
|
2021-10-14 00:27:13 +00:00
|
|
|
if (OverlapsImageSpace(p, size)) {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("overlaps image");
|
2021-10-14 00:27:13 +00:00
|
|
|
} else {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("overlaps existing");
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
2022-04-13 05:11:00 +00:00
|
|
|
#endif
|
|
|
|
res = VIP(efault());
|
2022-04-15 06:39:48 +00:00
|
|
|
} else if (__isfdkind(fd, kFdZip)) {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("fd is zipos handle");
|
|
|
|
res = VIP(einval());
|
2022-04-18 07:01:26 +00:00
|
|
|
} else if (__virtualmax &&
|
|
|
|
(__builtin_add_overflow((virtualused = GetMemtrackSize(&_mmi)),
|
|
|
|
size, &virtualneed) ||
|
|
|
|
virtualneed > __virtualmax)) {
|
|
|
|
STRACE("%'zu size + %'zu inuse exceeds virtual memory limit %'zu", size,
|
|
|
|
virtualused, __virtualmax);
|
|
|
|
res = VIP(enomem());
|
2022-04-13 05:11:00 +00:00
|
|
|
} else {
|
|
|
|
if (fd == -1) {
|
|
|
|
size = ROUNDUP(size, FRAMESIZE);
|
|
|
|
if (IsWindows()) {
|
|
|
|
prot |= PROT_WRITE; /* kludge */
|
|
|
|
}
|
2020-09-03 12:44:37 +00:00
|
|
|
}
|
2022-04-15 06:39:48 +00:00
|
|
|
n = (int)(size >> 16) + !!(size & (FRAMESIZE - 1));
|
|
|
|
assert(n > 0);
|
2022-04-13 05:11:00 +00:00
|
|
|
f = (flags & ~MAP_FIXED_NOREPLACE) | MAP_FIXED;
|
|
|
|
if (flags & MAP_FIXED) {
|
|
|
|
x = FRAME(p);
|
|
|
|
if (IsWindows()) {
|
|
|
|
if (UntrackMemoryIntervals(p, size)) {
|
|
|
|
OnUnrecoverableMmapError("FIXED UNTRACK FAILED");
|
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
2022-04-13 05:11:00 +00:00
|
|
|
} else if (!NeedAutomap(p, size)) {
|
|
|
|
x = FRAME(p);
|
|
|
|
} else if (!Automap(n, &x)) {
|
|
|
|
STRACE("AUTOMAP OUT OF MEMORY D:");
|
|
|
|
return VIP(enomem());
|
|
|
|
}
|
|
|
|
p = (char *)ADDR(x);
|
|
|
|
if (IsOpenbsd() && (f & MAP_GROWSDOWN)) { /* openbsd:dubstack */
|
|
|
|
dm = sys_mmap(p, size, prot, f & ~MAP_GROWSDOWN, fd, off);
|
|
|
|
if (dm.addr == MAP_FAILED) res = MAP_FAILED;
|
|
|
|
}
|
|
|
|
if (!IsWindows()) {
|
|
|
|
res = MapMemory(p, size, prot, flags, fd, off, f, x, n);
|
|
|
|
} else {
|
|
|
|
res = MapMemories(p, size, prot, flags, fd, off, f, x, n);
|
2020-09-03 12:44:37 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("mmap(%p, %'zu, %s, %s, %d, %'ld) → %p% m", addr, size,
|
|
|
|
DescribeProtFlags(prot), DescribeMapFlags(flags), fd, off, res);
|
|
|
|
return res;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|