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"
|
2021-08-26 04:35:58 +00:00
|
|
|
#include "libc/calls/sysdebug.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/dce.h"
|
2021-02-01 11:33:13 +00:00
|
|
|
#include "libc/intrin/asan.internal.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/log/backtrace.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"
|
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"
|
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"
|
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 SMALL(n) ((n) <= 0xffffffffffff)
|
|
|
|
#define ALIGNED(p) (!(IP(p) & (FRAMESIZE - 1)))
|
|
|
|
#define LEGAL(p) (-0x800000000000 <= IP(p) && IP(p) <= 0x7fffffffffff)
|
|
|
|
#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))
|
|
|
|
|
|
|
|
forceinline wontreturn void Die(void) {
|
|
|
|
if (weaken(__die)) weaken(__die)();
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
noasan static bool ChooseInterval(int x, int n, int *res) {
|
|
|
|
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;
|
|
|
|
if (ChooseInterval(FRAME(kAutomapStart), n, res)) {
|
|
|
|
assert(*res >= FRAME(kAutomapStart));
|
|
|
|
if (*res + n <= FRAME(kAutomapStart + (kAutomapStart - 1))) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x) ENOMEM (automap interval exhausted)",
|
|
|
|
ADDR(*res), ADDR(n + 1));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x) ENOMEM (automap failed)", ADDR(*res),
|
|
|
|
ADDR(n + 1));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2020-06-15 14:18:57 +00:00
|
|
|
struct DirectMap dm;
|
2021-10-14 00:27:13 +00:00
|
|
|
int a, b, i, f, m, n, x;
|
|
|
|
char mode[8], *p = addr;
|
|
|
|
if (UNLIKELY(!size)) {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x) EINVAL (size=0)", p, size);
|
|
|
|
return VIP(einval());
|
|
|
|
}
|
|
|
|
if (UNLIKELY(!SMALL(size))) {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x) EINVAL (size isn't 48-bit)", p, size);
|
|
|
|
return VIP(einval());
|
|
|
|
}
|
|
|
|
if (UNLIKELY(!LEGAL(p))) {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x) EINVAL (p isn't 48-bit)", p, size);
|
|
|
|
return VIP(einval());
|
|
|
|
}
|
|
|
|
if (UNLIKELY(!ALIGNED(p))) {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x) EINVAL (p isn't 64kb aligned)", p, size);
|
|
|
|
return VIP(einval());
|
|
|
|
}
|
|
|
|
if (UNLIKELY(fd < -1)) {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x, fd=%d) EBADF", p, size, (long)fd);
|
|
|
|
return VIP(ebadf());
|
|
|
|
}
|
|
|
|
if (UNLIKELY(!((fd != -1) ^ !!(flags & MAP_ANONYMOUS)))) {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x, %s, %d, %d) EINVAL (fd anonymous mismatch)", p,
|
|
|
|
size, DescribeMapping(prot, flags, mode), (long)fd, off);
|
|
|
|
return VIP(einval());
|
|
|
|
}
|
|
|
|
if (UNLIKELY(!(!!(flags & MAP_PRIVATE) ^ !!(flags & MAP_SHARED)))) {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x) EINVAL (MAP_SHARED ^ MAP_PRIVATE)", p, size);
|
|
|
|
return VIP(einval());
|
|
|
|
}
|
|
|
|
if (UNLIKELY(off < 0)) {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x, off=%d) EINVAL (neg off)", p, size, off);
|
|
|
|
return VIP(einval());
|
|
|
|
}
|
|
|
|
if (UNLIKELY(INT64_MAX - size < off)) {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x, off=%d) EINVAL (too large)", p, size, off);
|
|
|
|
return VIP(einval());
|
|
|
|
}
|
|
|
|
if (UNLIKELY(!ALIGNED(off))) {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x) EINVAL (p isn't 64kb aligned)", p, size);
|
|
|
|
return VIP(einval());
|
|
|
|
}
|
|
|
|
if ((flags & MAP_FIXED_NOREPLACE) && IsMapped(p, size)) {
|
|
|
|
if (OverlapsImageSpace(p, size)) {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x) EFAULT (overlaps image)", p, size);
|
|
|
|
} else {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x) EFAULT (overlaps existing)", p, size);
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
return VIP(efault());
|
|
|
|
}
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x, %s, %d, %d)", p, size,
|
|
|
|
DescribeMapping(prot, flags, mode), (long)fd, off);
|
|
|
|
if (fd == -1) {
|
|
|
|
size = ROUNDUP(size, FRAMESIZE);
|
|
|
|
if (IsWindows()) {
|
|
|
|
prot |= PROT_WRITE; /* kludge */
|
2020-09-03 12:44:37 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
|
|
|
n = FRAME(size) + !!(size & (FRAMESIZE - 1));
|
|
|
|
f = (flags & ~MAP_FIXED_NOREPLACE) | MAP_FIXED;
|
|
|
|
if (flags & MAP_FIXED) {
|
|
|
|
x = FRAME(p);
|
|
|
|
if (IsWindows()) {
|
|
|
|
if (UntrackMemoryIntervals(p, size)) {
|
|
|
|
SYSDEBUG("FIXED UNTRACK FAILED %s", strerror(errno));
|
|
|
|
Die();
|
|
|
|
}
|
2020-09-03 12:44:37 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
} else if (!NeedAutomap(p, size)) {
|
|
|
|
x = FRAME(p);
|
|
|
|
} else if (!Automap(n, &x)) {
|
|
|
|
return VIP(enomem());
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
p = (char *)ADDR(x);
|
2021-02-03 08:10:12 +00:00
|
|
|
if (IsOpenbsd() && (f & MAP_GROWSDOWN)) { /* openbsd:dubstack */
|
2021-10-14 00:27:13 +00:00
|
|
|
dm = sys_mmap(p, size, prot, f & ~MAP_GROWSDOWN, fd, off);
|
|
|
|
if (dm.addr == MAP_FAILED) return MAP_FAILED;
|
2021-02-03 08:10:12 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
dm = sys_mmap(p, size, prot, f, fd, off);
|
|
|
|
if (UNLIKELY(dm.addr == MAP_FAILED)) {
|
|
|
|
if (IsWindows() && (flags & MAP_FIXED)) {
|
|
|
|
SYSDEBUG("mmap(0x%p, 0x%x) -> %s (%s)", p, size, strerror(errno),
|
|
|
|
"can't recover from MAP_FIXED errors on Windows");
|
|
|
|
Die();
|
|
|
|
}
|
2020-08-25 11:23:25 +00:00
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
if (UNLIKELY(dm.addr != p)) {
|
|
|
|
SYSDEBUG("KERNEL DIDN'T RESPECT MAP_FIXED");
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
if (!IsWindows() && (flags & MAP_FIXED)) {
|
|
|
|
if (UntrackMemoryIntervals(p, size)) {
|
|
|
|
SYSDEBUG("FIXED UNTRACK FAILED %s", strerror(errno));
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (TrackMemoryInterval(&_mmi, x, x + (n - 1), dm.maphandle, prot, flags)) {
|
|
|
|
if (sys_munmap(p, n) == -1) {
|
|
|
|
SYSDEBUG("TRACK MUNMAP FAILED %s", strerror(errno));
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
return MAP_FAILED;
|
2020-09-03 12:44:37 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
if (weaken(__asan_map_shadow) && !OverlapsShadowSpace(p, size)) {
|
|
|
|
weaken(__asan_map_shadow)((intptr_t)p, size);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
return p;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|