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
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/assert.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2021-04-18 18:34:59 +00:00
|
|
|
#include "libc/dce.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/intrin/asan.internal.h"
|
2022-04-13 05:11:00 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2022-11-02 05:36:03 +00:00
|
|
|
#include "libc/intrin/directmap.internal.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/likely.h"
|
|
|
|
#include "libc/intrin/strace.internal.h"
|
|
|
|
#include "libc/intrin/weaken.h"
|
2021-04-18 18:34:59 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/nt/runtime.h"
|
|
|
|
#include "libc/runtime/memtrack.internal.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/sysv/consts/map.h"
|
2021-04-18 18:34:59 +00:00
|
|
|
#include "libc/sysv/consts/mremap.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2021-04-18 18:34:59 +00:00
|
|
|
#define IP(X) (intptr_t)(X)
|
|
|
|
#define VIP(X) (void *)IP(X)
|
|
|
|
#define ALIGNED(p) (!(IP(p) & (FRAMESIZE - 1)))
|
2021-10-14 00:27:13 +00:00
|
|
|
#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))
|
|
|
|
|
|
|
|
static size_t GetMapSize(size_t i, size_t *j) {
|
|
|
|
size_t n;
|
|
|
|
n = (size_t)(_mmi.p[i].y - _mmi.p[i].x + 1) << 16;
|
|
|
|
while (i + 1 < _mmi.i) {
|
|
|
|
if (_mmi.p[i + 1].x != _mmi.p[i].y + 1) break;
|
|
|
|
++i;
|
|
|
|
n += (size_t)(_mmi.p[i].y - _mmi.p[i].x + 1) << 16;
|
|
|
|
}
|
|
|
|
*j = i;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool MustMoveMap(intptr_t y, size_t j) {
|
|
|
|
return ADDR(_mmi.p[j].y) + FRAMESIZE > y ||
|
|
|
|
(j + 1 < _mmi.i && ADDR(_mmi.p[j + 1].x) < y);
|
|
|
|
}
|
2021-04-18 18:34:59 +00:00
|
|
|
|
|
|
|
/**
|
2021-10-14 00:27:13 +00:00
|
|
|
* Extends and/or relocates memory pages.
|
2021-04-18 18:34:59 +00:00
|
|
|
*
|
|
|
|
* @param p is old address
|
|
|
|
* @param n is old size
|
|
|
|
* @param m is new size
|
|
|
|
* @param f should have MREMAP_MAYMOVE and may have MAP_FIXED
|
|
|
|
* @param q is new address
|
|
|
|
*/
|
|
|
|
void *mremap(void *p, size_t n, size_t m, int f, ... /* void *q */) {
|
2022-04-15 06:39:48 +00:00
|
|
|
enosys();
|
|
|
|
return MAP_FAILED;
|
|
|
|
|
|
|
|
#if 0
|
2021-04-18 18:34:59 +00:00
|
|
|
va_list va;
|
2022-04-13 05:11:00 +00:00
|
|
|
void *res, *q;
|
|
|
|
if (f & MREMAP_FIXED) {
|
|
|
|
va_start(va, f);
|
|
|
|
q = va_arg(va, void *);
|
|
|
|
va_end(va);
|
|
|
|
} else {
|
|
|
|
q = 0;
|
|
|
|
}
|
|
|
|
enosys();
|
|
|
|
res = MAP_FAILED;
|
|
|
|
STRACE("mremap(%p, %'zu, %'zu, %s, %p) → %p% m", p, n, m,
|
|
|
|
DescribeRemapFlags(f), q, res);
|
|
|
|
return res;
|
|
|
|
|
|
|
|
// TODO(jart): perhaps some day?
|
|
|
|
// probably not a big perf gain at this point :|
|
2021-10-14 00:27:13 +00:00
|
|
|
size_t i, j, k;
|
|
|
|
struct DirectMap dm;
|
|
|
|
int a, b, prot, flags;
|
2022-10-10 05:38:28 +00:00
|
|
|
_unassert(!__vforked);
|
2021-10-14 00:27:13 +00:00
|
|
|
if (UNLIKELY(!m)) {
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("m=0");
|
2021-10-14 00:27:13 +00:00
|
|
|
return VIP(einval());
|
2022-04-13 05:11:00 +00:00
|
|
|
} else if (UNLIKELY(!n)) {
|
|
|
|
STRACE("n=0");
|
2021-10-14 00:27:13 +00:00
|
|
|
return VIP(eopnotsupp());
|
2022-04-13 05:11:00 +00:00
|
|
|
} else if (UNLIKELY(!ALIGNED(n))) {
|
|
|
|
STRACE("n align");
|
2021-10-14 00:27:13 +00:00
|
|
|
return VIP(eopnotsupp());
|
2022-04-13 05:11:00 +00:00
|
|
|
} else if (UNLIKELY(!ALIGNED(m))) {
|
|
|
|
STRACE("n align");
|
2021-10-14 00:27:13 +00:00
|
|
|
return VIP(eopnotsupp());
|
2022-04-13 05:11:00 +00:00
|
|
|
} else if (UNLIKELY(!ALIGNED(p))) {
|
|
|
|
STRACE("64kb align");
|
2021-10-14 00:27:13 +00:00
|
|
|
return VIP(einval());
|
2022-04-13 05:11:00 +00:00
|
|
|
} else if (UNLIKELY(!IsLegalSize(n))) {
|
|
|
|
STRACE("n too big");
|
2021-10-14 00:27:13 +00:00
|
|
|
return VIP(enomem());
|
2022-04-13 05:11:00 +00:00
|
|
|
} else if (UNLIKELY(!IsLegalSize(m))) {
|
|
|
|
STRACE("m too big");
|
2021-10-14 00:27:13 +00:00
|
|
|
return VIP(enomem());
|
2022-04-13 05:11:00 +00:00
|
|
|
} else if (f & ~(MREMAP_MAYMOVE | MREMAP_FIXED)) {
|
|
|
|
STRACE("bad flag");
|
2021-10-14 00:27:13 +00:00
|
|
|
return VIP(einval());
|
2022-04-13 05:11:00 +00:00
|
|
|
} else if (!IsMemtracked(FRAME(p), FRAME((intptr_t)p + (n - 1)))) {
|
|
|
|
STRACE("interval not tracked");
|
2021-10-14 00:27:13 +00:00
|
|
|
return VIP(efault());
|
|
|
|
}
|
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("mremap(%p, %'zu, %'zu, %#b)", p, n, m, f);
|
2021-10-14 00:27:13 +00:00
|
|
|
i = FindMemoryInterval(&_mmi, FRAME(p));
|
|
|
|
if (i >= _mmi.i) return VIP(efault());
|
|
|
|
flags = _mmi.p[i].flags;
|
|
|
|
if (!(flags & MAP_ANONYMOUS)) {
|
|
|
|
return VIP(eopnotsupp()); /* TODO */
|
|
|
|
}
|
|
|
|
if (f & MREMAP_FIXED) {
|
|
|
|
if (!ALIGNED(q)) return VIP(einval());
|
|
|
|
return VIP(eopnotsupp()); /* TODO */
|
|
|
|
}
|
|
|
|
prot = _mmi.p[i].prot;
|
|
|
|
for (k = i + 1; k <= j; ++k) {
|
|
|
|
prot |= _mmi.p[k].prot;
|
|
|
|
if (_mmi.p[k].flags != flags) {
|
|
|
|
return VIP(enomem());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m == n) {
|
|
|
|
return p;
|
|
|
|
} else if (m < n) {
|
|
|
|
if (munmap((char *)p + n, m - n) != -1) {
|
|
|
|
return p;
|
2021-04-18 18:34:59 +00:00
|
|
|
} else {
|
2021-10-14 00:27:13 +00:00
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
|
|
|
} else if (!MustMoveMap(j, (intptr_t)p + n)) {
|
|
|
|
dm = sys_mmap((char *)p + n, m - n, prot, flags | MAP_FIXED, -1, 0);
|
|
|
|
if (dm.addr == MAP_FAILED) return 0;
|
|
|
|
if (TrackMemoryInterval(&_mmi, ((uintptr_t)p + n) >> 16,
|
|
|
|
((uintptr_t)p + m - FRAMESIZE) >> 16, dm.maphandle,
|
2022-04-13 05:11:00 +00:00
|
|
|
prot, flags, false, false, 0, m - n) != -1) {
|
2022-09-13 06:10:38 +00:00
|
|
|
if (_weaken(__asan_map_shadow)) {
|
|
|
|
_weaken(__asan_map_shadow)((uintptr_t)dm.addr, m - n);
|
2021-04-18 18:34:59 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
return p;
|
|
|
|
} else {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
} else if (!(f & MREMAP_MAYMOVE)) {
|
|
|
|
return VIP(enomem());
|
|
|
|
} else if (IsLinux()) {
|
|
|
|
a = (uint32_t)(kAutomapStart >> 16);
|
|
|
|
i = FindMemoryInterval(&_mmi, a);
|
|
|
|
if (i < _mmi.i) {
|
|
|
|
for (; i + 1 < _mmi.i; ++i) {
|
|
|
|
if (((size_t)(_mmi.p[i + 1].x - _mmi.p[i].y - 1) << 16) >= m) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (__builtin_add_overflow(_mmi.p[i].y, 1, &a)) {
|
|
|
|
return VIP(enomem());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (__builtin_add_overflow(a, (int)((m >> 16) - 1), &b)) {
|
|
|
|
return VIP(enomem());
|
|
|
|
}
|
|
|
|
q = sys_mremap((void *)p, n, m, MREMAP_MAYMOVE | MREMAP_FIXED,
|
|
|
|
(void *)ADDR(a));
|
2022-04-16 17:40:23 +00:00
|
|
|
KERNTRACE("sys_mremap(%p, %'zu, %'zu, %#b, %p) → %p", p, n, m,
|
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
|
|
|
MREMAP_MAYMOVE | MREMAP_FIXED, ADDR(a), q);
|
2021-10-14 00:27:13 +00:00
|
|
|
if (q == MAP_FAILED) return 0;
|
|
|
|
if (ReleaseMemoryIntervals(&_mmi, (uintptr_t)p >> 16,
|
|
|
|
((uintptr_t)p + n - FRAMESIZE) >> 16, 0) != -1 &&
|
2022-04-13 05:11:00 +00:00
|
|
|
TrackMemoryInterval(&_mmi, a, b, -1, prot, flags, false, false, 0, m) !=
|
|
|
|
-1) {
|
2022-09-13 06:10:38 +00:00
|
|
|
if (_weaken(__asan_poison)) {
|
2021-10-14 00:27:13 +00:00
|
|
|
if (!OverlapsShadowSpace(p, n)) {
|
2022-09-13 06:10:38 +00:00
|
|
|
_weaken(__asan_poison)((intptr_t)p, n, kAsanUnmapped);
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
|
|
|
if (!OverlapsShadowSpace(q, m)) {
|
2022-09-13 06:10:38 +00:00
|
|
|
_weaken(__asan_map_shadow)((intptr_t)q, m);
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (void *)ADDR(a);
|
|
|
|
} else {
|
|
|
|
abort();
|
2021-04-18 18:34:59 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
} else if ((q = mmap(0, m, prot, flags, -1, 0)) != MAP_FAILED) {
|
|
|
|
memcpy(q, p, n);
|
|
|
|
munmap(p, n);
|
|
|
|
return q;
|
2021-04-18 18:34:59 +00:00
|
|
|
} else {
|
2021-10-14 00:27:13 +00:00
|
|
|
return q;
|
2021-04-18 18:34:59 +00:00
|
|
|
}
|
2022-04-13 05:11:00 +00:00
|
|
|
#endif
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|