aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/module-list.c2
-rw-r--r--core/module.c14
-rw-r--r--core/module.h5
3 files changed, 18 insertions, 3 deletions
diff --git a/core/module-list.c b/core/module-list.c
index 67fbbdb..57e44c4 100644
--- a/core/module-list.c
+++ b/core/module-list.c
@@ -1580,6 +1580,8 @@ static bool logged(PureList *list, const PureUndo *undo) {
/* ---- The list */
+_Static_assert(sizeof(PureList) <= PURE_CALL_LIST, "a call's list counts at least its own bytes (module.h)");
+
PureList *pure_list_new(unsigned fields, const uint8_t *kinds) {
if (fields > 16) return NULL;
PureList *list = calloc(1, sizeof(*list));
diff --git a/core/module.c b/core/module.c
index fec9dd9..19fb7fb 100644
--- a/core/module.c
+++ b/core/module.c
@@ -4157,6 +4157,9 @@ typedef struct Call {
Walk *walk;
} Call;
typedef __typeof__(((PureApp *)0)->held[0]) Kept;
+_Static_assert(sizeof(Call) <= PURE_CALL_BYTES && sizeof(Slot) <= PURE_CALL_VALUE && sizeof(PureValue) <= PURE_CALL_LOCAL &&
+ sizeof(Loop) <= PURE_CALL_LOOP && sizeof(Kept) <= PURE_CALL_HELD && sizeof(PureList *) <= PURE_CALL_REGISTER,
+ "a frame counts at least what it holds");
/* Room for a frame of bytes among the app's blocks: where they stood before it, or NULL out of memory. */
static void *frame_room(PureApp *app, size_t bytes, unsigned *block, size_t *used) {
@@ -4193,12 +4196,14 @@ static PureList *spare_list(PureApp *app, uint32_t state) {
return spare->count ? spare->lists[--spare->count] : new_list(app->module, app->module->state_ref[state]);
}
-/* A call's list let go: emptied (what it held no longer counted) and kept for the next calls. */
+/* A call's list let go: emptied (what it held no longer counted) and kept for the next calls, unless what it keeps
+ * when empty passes what a frame counts for it. */
static void let_list_go(PureApp *app, uint32_t state, PureList *list) {
Held before = list_held(app, list);
pure_list_clear(list);
(void)grows(app, before, list_held(app, list));
__typeof__(app->spare[0]) *spare = &app->spare[state];
+ if (pure_list_extra(list) > PURE_CALL_LIST) { pure_list_free(list); return; }
if (spare->count == spare->capacity) {
unsigned capacity = spare->capacity ? 2 * spare->capacity : 4;
PureList **lists = realloc(spare->lists, capacity * sizeof(*lists));
@@ -4268,9 +4273,12 @@ static __attribute__((noinline, cold)) int enter_call(Frame *frame, Call **top,
unsigned keeps = (unsigned)__builtin_popcount(f->lists | f->registers), holds = (unsigned)__builtin_popcount(f->held);
size_t bytes = (sizeof(Call) + keeps * sizeof(PureList *) + holds * sizeof(Kept) + f->height * sizeof(Slot) +
f->locals * sizeof(PureValue) + f->nesting * sizeof(Loop) + 15) & ~(size_t)15;
+ size_t counted = PURE_CALL_BYTES + (size_t)f->height * PURE_CALL_VALUE + (size_t)f->locals * PURE_CALL_LOCAL +
+ (size_t)f->nesting * PURE_CALL_LOOP + (size_t)__builtin_popcount(f->lists) * PURE_CALL_LIST +
+ (size_t)holds * PURE_CALL_HELD + (size_t)__builtin_popcount(f->registers) * PURE_CALL_REGISTER;
frame->fault_pc = *pc - 1;
if (!charge(app, f->code.length)) { frame->kind = PURE_FAULT_WORK; return STEP_FAULT; }
- if (!grows(app, (Held){0, 0}, (Held){0, bytes})) { frame->kind = PURE_FAULT_MEMORY; return STEP_FAULT; }
+ if (!grows(app, (Held){0, 0}, (Held){0, counted})) { frame->kind = PURE_FAULT_MEMORY; return STEP_FAULT; }
unsigned block;
size_t used, base = frame->sp - f->params;
Call *call = frame_room(app, bytes, &block, &used);
@@ -4308,7 +4316,7 @@ static __attribute__((noinline, cold)) int enter_call(Frame *frame, Call **top,
*h = (Kept){0};
}
*call = (Call){.below = *top, .back = *pc, .end = frame->end, .function = function, .depth = frame->depth, .block = block,
- .sp = base, .used = used, .bytes = bytes, .row = ev->row, .stack = frame->stack, .loops = frame->loops,
+ .sp = base, .used = used, .bytes = counted, .row = ev->row, .stack = frame->stack, .loops = frame->loops,
.locals = ev->locals, .walk = ev->walk};
if (f->locals) memset(locals, 0, f->locals * sizeof(*locals));
frame->stack = stack, frame->sp = f->params, frame->loops = (Loop *)(locals + f->locals), frame->depth = 0;
diff --git a/core/module.h b/core/module.h
index 9ce354f..18947d0 100644
--- a/core/module.h
+++ b/core/module.h
@@ -131,6 +131,11 @@ enum { PURE_FORMAT_WIDTH = 8, PURE_FORMAT_ZEROS = 1 << 16, PURE_FORMAT_FORM = 17
* text of a code point, failing as LIMIT outside 0-0x10FFFF and as ENCODING
* for a surrogate. */
enum { PURE_MODULE_FUNCTIONS = 64, PURE_MODULE_SIGNATURES = 1024, PURE_SIGNATURE_BYTES = 5 };
+/* What a call's frame counts in the event's room: a table, as gas is, not the implementation's sizes (each at least
+ * what it stands for), so a module's calls fail at the same depth wherever and on whatever build they run: so much a
+ * frame, a value of its stack's height, a local, a loop it nests, a list of its own, a held value and a register. */
+enum { PURE_CALL_BYTES = 256, PURE_CALL_VALUE = 64, PURE_CALL_LOCAL = 8, PURE_CALL_LOOP = 128, PURE_CALL_LIST = 1024,
+ PURE_CALL_HELD = 128, PURE_CALL_REGISTER = 16 };
/* A local: one of an event's PURE_MODULE_LOCALS slots, 0 when the event
* begins and never saved. LOCAL_LOAD and LOCAL_STORE name the slot (bits
* 0-7), what it holds (bits 8-15: 0 a number, 1 an index, 2 a bool, 3 an i64,