2020-08-25 11:23:25 +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-08-25 11:23:25 +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-08-25 11:23:25 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/assert.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/calls/calls.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-08-31 12:17:31 +00:00
|
|
|
#include "libc/dce.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/errno.h"
|
|
|
|
#include "libc/intrin/asan.internal.h"
|
2022-09-11 18:02:07 +00:00
|
|
|
#include "libc/intrin/bits.h"
|
|
|
|
#include "libc/intrin/likely.h"
|
|
|
|
#include "libc/intrin/weaken.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/log/libfatal.internal.h"
|
2022-05-18 23:41:29 +00:00
|
|
|
#include "libc/log/log.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2021-09-04 20:20:47 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2021-09-28 05:58:51 +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"
|
|
|
|
#include "libc/str/str.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/sysv/consts/map.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2022-09-12 11:19:32 +00:00
|
|
|
#if IsModeDbg()
|
|
|
|
#define ASSERT_MEMTRACK() \
|
|
|
|
if (!AreMemoryIntervalsOk(mm)) { \
|
|
|
|
PrintMemoryIntervals(2, mm); \
|
|
|
|
notpossible; \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define ASSERT_MEMTRACK()
|
|
|
|
#endif
|
|
|
|
|
2022-04-23 01:55:28 +00:00
|
|
|
static void *MoveMemoryIntervals(struct MemoryInterval *d,
|
|
|
|
const struct MemoryInterval *s, int n) {
|
2021-09-28 05:58:51 +00:00
|
|
|
int i;
|
2022-09-12 11:19:32 +00:00
|
|
|
if (n < 0) unreachable;
|
2021-09-28 05:58:51 +00:00
|
|
|
if (d > s) {
|
|
|
|
for (i = n; i--;) {
|
|
|
|
d[i] = s[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
d[i] = s[i];
|
|
|
|
}
|
2021-07-12 06:17:47 +00:00
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
return d;
|
2021-07-12 06:17:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 01:55:28 +00:00
|
|
|
static void RemoveMemoryIntervals(struct MemoryIntervals *mm, int i, int n) {
|
2022-09-12 11:19:32 +00:00
|
|
|
if (i < 0) unreachable;
|
|
|
|
if (i + n > mm->i) unreachable;
|
2021-09-28 05:58:51 +00:00
|
|
|
MoveMemoryIntervals(mm->p + i, mm->p + i + n, mm->i - (i + n));
|
2020-08-25 11:23:25 +00:00
|
|
|
mm->i -= n;
|
|
|
|
}
|
|
|
|
|
2022-04-23 01:55:28 +00:00
|
|
|
static bool ExtendMemoryIntervals(struct MemoryIntervals *mm) {
|
2021-09-28 05:58:51 +00:00
|
|
|
int prot, flags;
|
2021-10-15 02:36:49 +00:00
|
|
|
char *base, *shad;
|
|
|
|
size_t gran, size;
|
2021-09-28 05:58:51 +00:00
|
|
|
struct DirectMap dm;
|
2021-10-15 02:36:49 +00:00
|
|
|
gran = kMemtrackGran;
|
|
|
|
base = (char *)kMemtrackStart;
|
|
|
|
prot = PROT_READ | PROT_WRITE;
|
|
|
|
flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED;
|
2022-05-19 23:57:49 +00:00
|
|
|
// TODO(jart): These map handles should not leak across NT fork()
|
2021-09-28 05:58:51 +00:00
|
|
|
if (mm->p == mm->s) {
|
2022-07-11 12:55:17 +00:00
|
|
|
// TODO(jart): How can we detect ASAN mode under GREG?
|
|
|
|
if (1 || IsAsan()) {
|
2021-10-15 02:36:49 +00:00
|
|
|
shad = (char *)(((intptr_t)base >> 3) + 0x7fff8000);
|
|
|
|
dm = sys_mmap(shad, gran >> 3, prot, flags, -1, 0);
|
2022-04-20 16:56:53 +00:00
|
|
|
if (!dm.addr) return false;
|
2021-09-28 05:58:51 +00:00
|
|
|
}
|
2021-10-15 02:36:49 +00:00
|
|
|
dm = sys_mmap(base, gran, prot, flags, -1, 0);
|
2022-04-20 16:56:53 +00:00
|
|
|
if (!dm.addr) return false;
|
2021-10-15 02:36:49 +00:00
|
|
|
MoveMemoryIntervals(dm.addr, mm->p, mm->i);
|
|
|
|
mm->p = dm.addr;
|
|
|
|
mm->n = gran / sizeof(*mm->p);
|
|
|
|
} else {
|
|
|
|
size = ROUNDUP(mm->n * sizeof(*mm->p), gran);
|
|
|
|
base += size;
|
2021-09-28 05:58:51 +00:00
|
|
|
if (IsAsan()) {
|
2021-10-15 02:36:49 +00:00
|
|
|
shad = (char *)(((intptr_t)base >> 3) + 0x7fff8000);
|
|
|
|
dm = sys_mmap(shad, gran >> 3, prot, flags, -1, 0);
|
2022-04-20 16:56:53 +00:00
|
|
|
if (!dm.addr) return false;
|
2021-09-28 05:58:51 +00:00
|
|
|
}
|
2021-10-15 02:36:49 +00:00
|
|
|
dm = sys_mmap(base, gran, prot, flags, -1, 0);
|
2022-04-20 16:56:53 +00:00
|
|
|
if (!dm.addr) return false;
|
2021-10-15 02:36:49 +00:00
|
|
|
mm->n = (size + gran) / sizeof(*mm->p);
|
2021-09-28 05:58:51 +00:00
|
|
|
}
|
2022-09-12 11:19:32 +00:00
|
|
|
ASSERT_MEMTRACK();
|
2021-10-15 02:36:49 +00:00
|
|
|
return true;
|
2021-09-28 05:58:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 01:55:28 +00:00
|
|
|
int CreateMemoryInterval(struct MemoryIntervals *mm, int i) {
|
2022-09-12 11:19:32 +00:00
|
|
|
if (i < 0) unreachable;
|
|
|
|
if (i > mm->i) unreachable;
|
|
|
|
if (mm->n < 0) unreachable;
|
2021-10-15 02:36:49 +00:00
|
|
|
if (UNLIKELY(mm->i == mm->n) && !ExtendMemoryIntervals(mm)) return enomem();
|
2021-09-28 05:58:51 +00:00
|
|
|
MoveMemoryIntervals(mm->p + i + 1, mm->p + i, mm->i++ - i);
|
2021-09-04 20:20:47 +00:00
|
|
|
return 0;
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 01:55:28 +00:00
|
|
|
static int PunchHole(struct MemoryIntervals *mm, int x, int y, int i) {
|
2021-09-04 20:20:47 +00:00
|
|
|
if (CreateMemoryInterval(mm, i) == -1) return -1;
|
2022-05-18 23:41:29 +00:00
|
|
|
mm->p[i + 0].size -= (size_t)(mm->p[i + 0].y - (x - 1)) * FRAMESIZE;
|
|
|
|
mm->p[i + 0].y = x - 1;
|
|
|
|
mm->p[i + 1].size -= (size_t)((y + 1) - mm->p[i + 1].x) * FRAMESIZE;
|
2020-08-25 11:23:25 +00:00
|
|
|
mm->p[i + 1].x = y + 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-23 01:55:28 +00:00
|
|
|
int ReleaseMemoryIntervals(struct MemoryIntervals *mm, int x, int y,
|
|
|
|
void wf(struct MemoryIntervals *, int, int)) {
|
2020-08-25 11:23:25 +00:00
|
|
|
unsigned l, r;
|
2022-09-12 11:19:32 +00:00
|
|
|
ASSERT_MEMTRACK();
|
|
|
|
if (y < x) unreachable;
|
2020-08-25 11:23:25 +00:00
|
|
|
if (!mm->i) return 0;
|
2022-05-18 23:41:29 +00:00
|
|
|
// binary search for the lefthand side
|
2020-08-25 11:23:25 +00:00
|
|
|
l = FindMemoryInterval(mm, x);
|
|
|
|
if (l == mm->i) return 0;
|
|
|
|
if (y < mm->p[l].x) return 0;
|
2022-05-18 23:41:29 +00:00
|
|
|
|
|
|
|
// binary search for the righthand side
|
2020-08-25 11:23:25 +00:00
|
|
|
r = FindMemoryInterval(mm, y);
|
|
|
|
if (r == mm->i || (r > l && y < mm->p[r].x)) --r;
|
2022-09-12 11:19:32 +00:00
|
|
|
if (r < l) unreachable;
|
|
|
|
if (x > mm->p[r].y) unreachable;
|
2022-05-18 23:41:29 +00:00
|
|
|
|
|
|
|
// remove the middle of an existing map
|
|
|
|
//
|
|
|
|
// ----|mmmmmmmmmmmmmmmm|--------- before
|
|
|
|
// xxxxx
|
|
|
|
// ----|mmmm|-----|mmmmm|--------- after
|
|
|
|
//
|
|
|
|
// this isn't possible on windows because we track each
|
|
|
|
// 64kb segment on that platform using a separate entry
|
2020-08-25 11:23:25 +00:00
|
|
|
if (l == r && x > mm->p[l].x && y < mm->p[l].y) {
|
|
|
|
return PunchHole(mm, x, y, l);
|
|
|
|
}
|
2022-05-18 23:41:29 +00:00
|
|
|
|
|
|
|
// trim the right side of the lefthand map
|
|
|
|
//
|
|
|
|
// ----|mmmmmmm|-------------- before
|
|
|
|
// xxxxx
|
|
|
|
// ----|mmmm|----------------- after
|
|
|
|
//
|
2020-08-25 11:23:25 +00:00
|
|
|
if (x > mm->p[l].x && x <= mm->p[l].y) {
|
2022-09-12 11:19:32 +00:00
|
|
|
if (y < mm->p[l].y) unreachable;
|
2020-08-25 11:23:25 +00:00
|
|
|
if (IsWindows()) return einval();
|
2022-05-18 23:41:29 +00:00
|
|
|
mm->p[l].size -= (size_t)(mm->p[l].y - (x - 1)) * FRAMESIZE;
|
2020-08-25 11:23:25 +00:00
|
|
|
mm->p[l].y = x - 1;
|
2022-09-12 11:19:32 +00:00
|
|
|
if (mm->p[l].x > mm->p[l].y) unreachable;
|
2020-08-25 11:23:25 +00:00
|
|
|
++l;
|
|
|
|
}
|
2022-05-18 23:41:29 +00:00
|
|
|
|
|
|
|
// trim the left side of the righthand map
|
|
|
|
//
|
|
|
|
// ------------|mmmmm|-------- before
|
|
|
|
// xxxxx
|
|
|
|
// ---------------|mm|-------- after
|
|
|
|
//
|
2020-08-25 11:23:25 +00:00
|
|
|
if (y >= mm->p[r].x && y < mm->p[r].y) {
|
2022-09-12 11:19:32 +00:00
|
|
|
if (x > mm->p[r].x) unreachable;
|
2020-08-25 11:23:25 +00:00
|
|
|
if (IsWindows()) return einval();
|
2022-05-18 23:41:29 +00:00
|
|
|
mm->p[r].size -= (size_t)((y + 1) - mm->p[r].x) * FRAMESIZE;
|
2020-08-25 11:23:25 +00:00
|
|
|
mm->p[r].x = y + 1;
|
2022-09-12 11:19:32 +00:00
|
|
|
if (mm->p[r].x > mm->p[r].y) unreachable;
|
2020-08-25 11:23:25 +00:00
|
|
|
--r;
|
|
|
|
}
|
2022-05-18 23:41:29 +00:00
|
|
|
|
2020-08-25 11:23:25 +00:00
|
|
|
if (l <= r) {
|
2021-02-03 14:22:51 +00:00
|
|
|
if (IsWindows() && wf) {
|
|
|
|
wf(mm, l, r);
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
|
|
|
RemoveMemoryIntervals(mm, l, r - l + 1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-23 01:55:28 +00:00
|
|
|
int TrackMemoryInterval(struct MemoryIntervals *mm, int x, int y, long h,
|
|
|
|
int prot, int flags, bool readonlyfile, bool iscow,
|
|
|
|
long offset, long size) {
|
2020-08-25 11:23:25 +00:00
|
|
|
unsigned i;
|
2022-09-12 11:19:32 +00:00
|
|
|
ASSERT_MEMTRACK();
|
|
|
|
if (y < x) unreachable;
|
2020-08-25 11:23:25 +00:00
|
|
|
i = FindMemoryInterval(mm, x);
|
2022-05-18 23:41:29 +00:00
|
|
|
|
|
|
|
// try to extend the righthand side of the lefthand entry
|
|
|
|
// we can't do that if we're tracking independent handles
|
|
|
|
// we can't do that if it's a file map with a small size!
|
2020-11-13 09:27:49 +00:00
|
|
|
if (i && x == mm->p[i - 1].y + 1 && h == mm->p[i - 1].h &&
|
2022-05-18 23:41:29 +00:00
|
|
|
prot == mm->p[i - 1].prot && flags == mm->p[i - 1].flags &&
|
|
|
|
mm->p[i - 1].size ==
|
|
|
|
(size_t)(mm->p[i - 1].y - mm->p[i - 1].x) * FRAMESIZE + FRAMESIZE) {
|
|
|
|
mm->p[i - 1].size += (size_t)(y - mm->p[i - 1].y) * FRAMESIZE;
|
2020-08-25 11:23:25 +00:00
|
|
|
mm->p[i - 1].y = y;
|
2022-05-18 23:41:29 +00:00
|
|
|
// if we filled the hole then merge the two mappings
|
2020-11-13 09:27:49 +00:00
|
|
|
if (i < mm->i && y + 1 == mm->p[i].x && h == mm->p[i].h &&
|
|
|
|
prot == mm->p[i].prot && flags == mm->p[i].flags) {
|
2020-08-25 11:23:25 +00:00
|
|
|
mm->p[i - 1].y = mm->p[i].y;
|
2022-05-18 23:41:29 +00:00
|
|
|
mm->p[i - 1].size += mm->p[i].size;
|
2020-08-25 11:23:25 +00:00
|
|
|
RemoveMemoryIntervals(mm, i, 1);
|
|
|
|
}
|
2022-05-18 23:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// try to extend the lefthand side of the righthand entry
|
|
|
|
// we can't do that if we're creating a smaller file map!
|
|
|
|
else if (i < mm->i && y + 1 == mm->p[i].x && h == mm->p[i].h &&
|
|
|
|
prot == mm->p[i].prot && flags == mm->p[i].flags &&
|
|
|
|
size == (size_t)(y - x) * FRAMESIZE + FRAMESIZE) {
|
|
|
|
mm->p[i].size += (size_t)(mm->p[i].x - x) * FRAMESIZE;
|
2020-08-25 11:23:25 +00:00
|
|
|
mm->p[i].x = x;
|
2022-05-18 23:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, create a new entry and memmove the items
|
|
|
|
else {
|
2021-09-04 20:20:47 +00:00
|
|
|
if (CreateMemoryInterval(mm, i) == -1) return -1;
|
2020-08-25 11:23:25 +00:00
|
|
|
mm->p[i].x = x;
|
|
|
|
mm->p[i].y = y;
|
2020-11-13 09:27:49 +00:00
|
|
|
mm->p[i].h = h;
|
|
|
|
mm->p[i].prot = prot;
|
|
|
|
mm->p[i].flags = flags;
|
2022-03-20 15:01:14 +00:00
|
|
|
mm->p[i].offset = offset;
|
|
|
|
mm->p[i].size = size;
|
2022-04-13 05:11:00 +00:00
|
|
|
mm->p[i].iscow = iscow;
|
|
|
|
mm->p[i].readonlyfile = readonlyfile;
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
2022-09-12 11:19:32 +00:00
|
|
|
|
2020-08-25 11:23:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|