From 233fa95e1499a1e8666fe52e87042d3f81fdc3cc Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Tue, 12 Nov 2024 21:08:41 +0300 Subject: [PATCH] fixes --- byterun/gmon.out | Bin 26858 -> 0 bytes byterun/include/utils.h | 1 + byterun/src/cli.c | 1 + byterun/src/interpreter.c | 13 +++++++++++-- byterun/src/parser.c | 27 ++++++++++++++++++++++----- byterun/src/types.c | 2 ++ 6 files changed, 37 insertions(+), 7 deletions(-) delete mode 100644 byterun/gmon.out diff --git a/byterun/gmon.out b/byterun/gmon.out deleted file mode 100644 index 8b7b81faa82e018595e9cdf4dcddfb793c83d6d5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 26858 zcmeI4O=w(I6vyvv(iFjxq1Y}8)`4QG)z&FB^`k95gXzLA;zA3q25bC;$bZ z02F`%Pyh-*0Vn_kpuk`im>z8SdpP=(tKFI@_Z9gknc42nZgVd_bexRZ#{9)`4TJaY zjo`_Idxghj)Hbvkt(jJ! zdq|J$Trp#{h>yM5#_d2yLxKO_$=MF*SEg%=7+Qt1eq}O@IR3iX)^jhkbQkvD^OfPM z8AeCE;(68i&Q_%T%_P5Ge=qs?X?yv7B@8+mcw!)ZSjBQR;Satjq!0sBqo|dU- zu=`-J(aY2eA1D9?pa2wr0#E=7KmjNK1)u;FD60Ve#w)Ab==-guxBP!u|IB8m+2NjQ zTeJG}8~?mE7CXkus^nv3@%Two*;w#{0#E=7KmjNK1)u;FfC5lpR29g6>tA1nW&PH- z3ap_36o3LyVCyL`%%2c`tzoXW^}2zKp#T(s0#E=7KmjNK1)u;FxHlERUrOWDcXV?+ zx`{rHM;}L_3hbp(=t6&U*mzIx9-(;F&zWWKVigtrVm2XLzlZlP=M}Czelu0Y=02S# zt5VNqWH9gavtWj`UYXA~i02lkgHb7qS|-%9GzEJYGZkYeaz497B_FVdWPd| zVa>w{Rrt+3k6t)i75L$i9IVXnBm%#@1XF>p%Iar--Q!d$xD%`%O5BDf&%j_gz3UP!Bn8S6YL07kxzYf$(8vm$Gjz%48e-PS=r_U z6M-6&;F3U%**@;>V9SM=1iOWp1kG_?UG*fG2-KJdPq}t91ZwRB(}Lxv2WFg}1xtdp z6TFbdq;@1RmIgsuqjaYJi&IsefRYp zZYLMq2`(4h2{xV&lb?Ud^{Dzn2$MM{aw0HDsU=YRPL^h#-EZaZA&E@tZ}3UFT{Hwf Mn1>yKALZQt1IJntdjJ3c diff --git a/byterun/include/utils.h b/byterun/include/utils.h index 7a460b892..1c3b11f39 100644 --- a/byterun/include/utils.h +++ b/byterun/include/utils.h @@ -13,6 +13,7 @@ typedef struct { int *public_ptr; /* A pointer to the beginning of publics table */ char *code_ptr; /* A pointer to the bytecode itself */ int *global_ptr; /* A pointer to the global area */ + int code_size; /* The size (in bytes) of code */ int stringtab_size; /* The size (in bytes) of the string table */ int global_area_size; /* The size (in words) of global area */ int public_symbols_number; /* The number of public symbols */ diff --git a/byterun/src/cli.c b/byterun/src/cli.c index aa1a2d513..dca7f954f 100644 --- a/byterun/src/cli.c +++ b/byterun/src/cli.c @@ -15,6 +15,7 @@ int main(int argc, char** argv) { #endif run(f, argc - 1, argv + 1); + free(f->global_ptr); free(f); return 0; diff --git a/byterun/src/interpreter.c b/byterun/src/interpreter.c index 599309aac..15bc22ebb 100644 --- a/byterun/src/interpreter.c +++ b/byterun/src/interpreter.c @@ -24,9 +24,9 @@ static inline char *ip_read_string(char **ip, bytefile *bf) { const size_t BUFFER_SIZE = 1000; void run(bytefile *bf, int argc, char **argv) { - void *stack[STACK_SIZE]; + size_t stack[STACK_SIZE]; void *buffer[BUFFER_SIZE]; - construct_state(bf, &s, stack); + construct_state(bf, &s, (void**)stack); #ifdef DEBUG_VERSION printf("--- interpreter run ---\n"); @@ -74,6 +74,15 @@ void run(bytefile *bf, int argc, char **argv) { // char *before_op_ip = s.ip; // save to set s.prev_ip bool call_happened = false; + if (s.ip >= bf->code_ptr + bf->code_size) { + s_failure(&s, "instruction pointer is out of range (>= size)"); + } + + if (s.ip < bf->code_ptr) { + s_failure(&s, "instruction pointer is out of range (< 0)"); + } + + s.instr_ip = s.ip; char x = ip_read_byte(&s.ip), h = (x & 0xF0) >> 4, l = x & 0x0F; #ifdef DEBUG_VERSION diff --git a/byterun/src/parser.c b/byterun/src/parser.c index fba5b510a..c070275e0 100644 --- a/byterun/src/parser.c +++ b/byterun/src/parser.c @@ -26,12 +26,16 @@ bytefile* read_file (char *fname) { } long size = ftell (f); - file = (bytefile*) malloc (size + sizeof(void*) * 4); + long additional_size = sizeof(void*) * 4 + sizeof(int); + file = (bytefile*) malloc (size + additional_size); // file itself + additional data + + char* file_begin = (char*)file + additional_size; + char* file_end = file_begin + size; if (file == 0) { failure ("unable to allocate memory to store file data\n"); } - + rewind (f); if (size != fread (&file->stringtab_size, 1, size, f)) { @@ -39,12 +43,25 @@ bytefile* read_file (char *fname) { } fclose (f); - - file->string_ptr = &file->buffer [file->public_symbols_number * 2 * sizeof(int)]; + + long public_symbols_size = file->public_symbols_number * 2 * sizeof(int); + if (file->buffer + public_symbols_size >= file_end) { + failure ("public symbols are out of the file size\n"); + } + if (file->string_ptr + file->stringtab_size > file_end) { + failure ("strings table is out of the file size\n"); + } + if (file->code_size < 0 || public_symbols_size < 0 || file->stringtab_size < 0) { + failure ("file zones sizes should be >= 0\n"); + } + + file->string_ptr = &file->buffer [public_symbols_size]; file->public_ptr = (int*) file->buffer; file->code_ptr = &file->string_ptr [file->stringtab_size]; file->global_ptr = (int*) calloc (file->global_area_size, sizeof (int)); - + + file->code_size = size - public_symbols_size - file->stringtab_size; + return file; } diff --git a/byterun/src/types.c b/byterun/src/types.c index fed704fc6..78f72bb2f 100644 --- a/byterun/src/types.c +++ b/byterun/src/types.c @@ -24,6 +24,8 @@ static void init_state(bytefile *bf, struct State* s, void** stack) { s->stack[i] = NULL; } + // printf("%p:%zu - %zu", s->stack, (size_t)s->stack, (size_t)s->stack & 0xF); + s->sp = s->stack + STACK_SIZE; // [top -> bottom] stack s->fp = NULL; }