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-02-08 17:19:00 +00:00
|
|
|
#include "libc/bits/likely.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/bits/weaken.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/mem/mem.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/nexgen32e/gc.internal.h"
|
2021-03-08 04:23:29 +00:00
|
|
|
#include "libc/runtime/gc.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/str/str.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
forceinline bool PointerNotOwnedByParentStackFrame(struct StackFrame *frame,
|
|
|
|
struct StackFrame *parent,
|
|
|
|
void *ptr) {
|
|
|
|
return !(((intptr_t)ptr > (intptr_t)frame) &&
|
|
|
|
((intptr_t)ptr < (intptr_t)parent));
|
|
|
|
}
|
|
|
|
|
2021-10-14 00:27:13 +00:00
|
|
|
static void __garbage_destroy(void) {
|
|
|
|
if (weaken(free)) {
|
|
|
|
weaken(free)(__garbage.p);
|
|
|
|
}
|
|
|
|
bzero(&__garbage, sizeof(__garbage));
|
|
|
|
}
|
|
|
|
|
2021-03-08 18:56:09 +00:00
|
|
|
void __deferer(struct StackFrame *frame, void *fn, void *arg) {
|
2021-02-08 17:19:00 +00:00
|
|
|
size_t n2;
|
|
|
|
struct Garbage *p2;
|
|
|
|
if (UNLIKELY(__garbage.i == __garbage.n)) {
|
2021-10-14 00:27:13 +00:00
|
|
|
p2 = __garbage.p;
|
2021-02-08 17:19:00 +00:00
|
|
|
n2 = __garbage.n + (__garbage.n >> 1);
|
2021-10-14 00:27:13 +00:00
|
|
|
if (__garbage.p != __garbage.initmem) {
|
|
|
|
if (!weaken(realloc)) return;
|
|
|
|
if (!(p2 = weaken(realloc)(p2, n2 * sizeof(*p2)))) return;
|
|
|
|
} else {
|
|
|
|
if (!weaken(malloc)) return;
|
|
|
|
if (!(p2 = weaken(malloc)(n2 * sizeof(*p2)))) return;
|
|
|
|
memcpy(p2, __garbage.p, __garbage.n * sizeof(*p2));
|
|
|
|
atexit(__garbage_destroy);
|
|
|
|
}
|
2021-02-08 17:19:00 +00:00
|
|
|
__garbage.p = p2;
|
|
|
|
__garbage.n = n2;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2021-08-19 15:30:00 +00:00
|
|
|
__garbage.p[__garbage.i].frame = frame;
|
2021-02-08 17:19:00 +00:00
|
|
|
__garbage.p[__garbage.i].fn = (intptr_t)fn;
|
|
|
|
__garbage.p[__garbage.i].arg = (intptr_t)arg;
|
|
|
|
__garbage.p[__garbage.i].ret = frame->addr;
|
|
|
|
__garbage.i++;
|
|
|
|
frame->addr = (intptr_t)__gc;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2021-03-08 18:56:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds destructor to garbage shadow stack.
|
|
|
|
*
|
|
|
|
* @param frame is passed automatically by wrapper macro
|
|
|
|
* @param fn takes one argument
|
|
|
|
* @param arg is passed to fn(arg)
|
|
|
|
* @return arg
|
|
|
|
*/
|
|
|
|
void __defer(struct StackFrame *frame, void *fn, void *arg) {
|
2021-10-14 00:27:13 +00:00
|
|
|
struct StackFrame *f;
|
2021-03-08 18:56:09 +00:00
|
|
|
if (!arg) return;
|
2021-10-14 00:27:13 +00:00
|
|
|
f = __builtin_frame_address(0);
|
2021-03-08 18:56:09 +00:00
|
|
|
assert(__garbage.n);
|
2021-10-14 00:27:13 +00:00
|
|
|
assert(f->next == frame);
|
|
|
|
assert(PointerNotOwnedByParentStackFrame(f, frame, arg));
|
2021-03-08 18:56:09 +00:00
|
|
|
__deferer(frame, fn, arg);
|
|
|
|
}
|