exec_stack.h
This file
Path
src/exec_stack.h
120 lines · 3.5 KiB · C
Last changed by
Fix signed-int overflow in `stack_reallocate`itchynybefore adoption01b3cde
History of this fileTake it
1#ifndef EXEC_STACK_H2#define EXEC_STACK_H3#include <stddef.h>4#include <stdint.h>5#include <stdio.h>6#include <stdlib.h>7#include <string.h>8#include <limits.h>9#include "jv_alloc.h"1011/*12 * The stack is a directed forest of variably sized blocks. Each block has a13 * "next" block which is at a higher memory address, or 0 if the block has no14 * "next" block. More than one block may have no "next" block. A block may be15 * the "next" block of more than one other block. Pushed blocks are added at16 * the low-address end of the stack.17 *18 * Stack pointers are negative integers that are offsets relative to "mem_end",19 * the end of the allocated region. The stack "bound" is the stack pointer of20 * the last block that would be able to fit in the currently allocated region.21 * The stack "limit" is the stack pointer of the last block currently in the22 * stack. The stack pointer of the "next" block is stored directly below each23 * block.24 *25 * <- mem_end = 0x10026 * 0xF8 +------------+27 * 0xF0 | |28 * 0xE8 +------------+ <- stack_ptr1 = -0x1829 * 0xE0 next = 030 * 0xD8 +------------+31 * 0xD0 | |32 * 0xC8 | |33 * 0xC0 +------------+ <- stack_ptr2 = limit = -0x4034 * 0xB8 next = -0x1835 * 0xB036 * 0xA8 <- bound = -0x5837 * 0xA038 */3940struct determine_alignment {41 char x;42 union { int i; double d; uint64_t u64; size_t sz; void* ptr; } u;43};44enum {ALIGNMENT = offsetof(struct determine_alignment, u)};4546static size_t align_round_up(size_t sz) {47 return ((sz + (ALIGNMENT - 1)) / ALIGNMENT) * ALIGNMENT;48}4950typedef int stack_ptr;5152struct stack {53 char* mem_end; // one-past-the-end of allocated region54 stack_ptr bound;55 stack_ptr limit; // 0 - stack is empty56};5758static void stack_init(struct stack* s) {59 s->mem_end = 0;60 s->bound = ALIGNMENT;61 s->limit = 0;62}6364static void stack_reset(struct stack* s) {65 assert(s->limit == 0 && "stack freed while not empty");66 if(s->mem_end != NULL) {67 char* mem_start = s->mem_end - ( -s->bound + ALIGNMENT);68 free(mem_start);69 }70 stack_init(s);71}7273static int stack_pop_will_free(struct stack* s, stack_ptr p) {74 return p == s->limit;75}7677static void* stack_block(struct stack* s, stack_ptr p) {78 return (void*)(s->mem_end + p);79}8081static stack_ptr* stack_block_next(struct stack* s, stack_ptr p) {82 return &((stack_ptr*)stack_block(s, p))[-1];83}8485static void stack_reallocate(struct stack* s, size_t sz) {86 size_t old_mem_length = (size_t)(-(s->bound)) + ALIGNMENT;87 char* old_mem_start = s->mem_end != NULL ? s->mem_end - old_mem_length : NULL;8889 size_t new_mem_length = align_round_up((old_mem_length + sz + 256) * 2);90 if (new_mem_length > INT_MAX) {91 fprintf(stderr, "jq: error: cannot allocate memory\n");92 abort();93 }94 char* new_mem_start = jv_mem_realloc(old_mem_start, new_mem_length);95 memmove(new_mem_start + (new_mem_length - old_mem_length),96 new_mem_start, old_mem_length);97 s->mem_end = new_mem_start + new_mem_length;98 s->bound = -(int)(new_mem_length - ALIGNMENT);99}100101static stack_ptr stack_push_block(struct stack* s, stack_ptr p, size_t sz) {102 int alloc_sz = align_round_up(sz) + ALIGNMENT;103 stack_ptr r = s->limit - alloc_sz;104 if (r < s->bound) {105 stack_reallocate(s, alloc_sz);106 }107 s->limit = r;108 *stack_block_next(s, r) = p;109 return r;110}111112static stack_ptr stack_pop_block(struct stack* s, stack_ptr p, size_t sz) {113 stack_ptr r = *stack_block_next(s, p);114 if (p == s->limit) {115 int alloc_sz = align_round_up(sz) + ALIGNMENT;116 s->limit += alloc_sz;117 }118 return r;119}120#endif