2021-02-22 00:26:36 +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│
|
2020-06-15 14:18:57 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2022-09-09 23:54:28 +00:00
|
|
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
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-09-09 23:54:28 +00:00
|
|
|
#include "libc/assert.h"
|
2021-02-22 00:26:36 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2022-09-09 23:54:28 +00:00
|
|
|
#include "libc/dce.h"
|
2022-10-17 18:02:04 +00:00
|
|
|
#include "libc/errno.h"
|
2022-09-09 23:54:28 +00:00
|
|
|
#include "libc/intrin/asan.internal.h"
|
|
|
|
#include "libc/intrin/asancodes.h"
|
2022-11-02 05:36:03 +00:00
|
|
|
#include "libc/intrin/directmap.internal.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2022-09-09 23:54:28 +00:00
|
|
|
#include "libc/runtime/memtrack.internal.h"
|
2021-02-24 04:23:19 +00:00
|
|
|
#include "libc/sysv/consts/map.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-09-09 23:54:28 +00:00
|
|
|
#define G FRAMESIZE
|
2021-02-24 04:23:19 +00:00
|
|
|
|
2022-10-17 18:02:04 +00:00
|
|
|
static void *_mapframe(void *p, int f) {
|
|
|
|
int rc, prot, flags;
|
2022-09-09 23:54:28 +00:00
|
|
|
struct DirectMap dm;
|
|
|
|
prot = PROT_READ | PROT_WRITE;
|
2022-10-06 11:55:26 +00:00
|
|
|
flags = f | MAP_ANONYMOUS | MAP_FIXED;
|
2022-10-17 18:02:04 +00:00
|
|
|
if ((dm = sys_mmap(p, G, prot, flags, -1, 0)).addr == p) {
|
|
|
|
__mmi_lock();
|
|
|
|
rc = TrackMemoryInterval(&_mmi, (uintptr_t)p >> 16, (uintptr_t)p >> 16,
|
|
|
|
dm.maphandle, prot, flags, false, false, 0, G);
|
|
|
|
__mmi_unlock();
|
|
|
|
if (!rc) {
|
|
|
|
return p;
|
|
|
|
} else {
|
|
|
|
_unassert(errno == ENOMEM);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_unassert(errno == ENOMEM);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-09-09 23:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extends static allocation.
|
|
|
|
*
|
|
|
|
* This simple fixed allocator has unusual invariants
|
|
|
|
*
|
|
|
|
* !(p & 0xffff) && !(((p >> 3) + 0x7fff8000) & 0xffff)
|
|
|
|
*
|
2022-09-13 06:10:38 +00:00
|
|
|
* which must be the case when selecting a starting address. We also
|
|
|
|
* make the assumption that allocations can only grow monotonically.
|
|
|
|
* Furthermore allocations shall never be removed or relocated.
|
2022-09-09 23:54:28 +00:00
|
|
|
*
|
|
|
|
* @param p points to start of memory region
|
|
|
|
* @param n specifies how many bytes are needed
|
|
|
|
* @param e points to end of memory that's allocated
|
|
|
|
* @param h is highest address to which `e` may grow
|
2022-10-06 11:55:26 +00:00
|
|
|
* @param f should be `MAP_PRIVATE` or `MAP_SHARED`
|
2022-10-17 18:02:04 +00:00
|
|
|
* @return new value for `e` or null w/ errno
|
|
|
|
* @raise ENOMEM if we require more vespene gas
|
2022-09-09 23:54:28 +00:00
|
|
|
*/
|
2022-10-06 11:55:26 +00:00
|
|
|
noasan void *_extend(void *p, size_t n, void *e, int f, intptr_t h) {
|
2022-09-09 23:54:28 +00:00
|
|
|
char *q;
|
2022-10-14 15:25:47 +00:00
|
|
|
_unassert(!((uintptr_t)SHADOW(p) & (G - 1)));
|
|
|
|
_unassert((uintptr_t)p + (G << kAsanScale) <= h);
|
2022-09-09 23:54:28 +00:00
|
|
|
for (q = e; q < ((char *)p + n); q += 8) {
|
|
|
|
if (!((uintptr_t)q & (G - 1))) {
|
2022-10-14 15:25:47 +00:00
|
|
|
_unassert(q + G <= (char *)h);
|
2022-10-17 18:02:04 +00:00
|
|
|
if (!_mapframe(q, f)) return 0;
|
2022-09-09 23:54:28 +00:00
|
|
|
if (IsAsan()) {
|
|
|
|
if (!((uintptr_t)SHADOW(q) & (G - 1))) {
|
2022-10-17 18:02:04 +00:00
|
|
|
if (!_mapframe(SHADOW(q), f)) return 0;
|
2022-09-09 23:54:28 +00:00
|
|
|
__asan_poison(q, G << kAsanScale, kAsanProtected);
|
|
|
|
}
|
2021-02-24 04:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-09 23:54:28 +00:00
|
|
|
if (IsAsan()) {
|
|
|
|
*SHADOW(q) = 0;
|
2021-02-22 00:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-09 23:54:28 +00:00
|
|
|
return q;
|
2021-02-22 00:26:36 +00:00
|
|
|
}
|