Skip to content

Commit

Permalink
enhance VM stack overflow detection
Browse files Browse the repository at this point in the history
  • Loading branch information
pikasTech committed Jul 28, 2023
1 parent 3a2bd13 commit f9f1e16
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
3 changes: 2 additions & 1 deletion port/linux/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
// "--gtest_filter=pikaMain.slice2"
// "--gtest_filter=re.match"
// "--gtest_filter=parser.for_indent"
"--gtest_filter=VM.comprehension"
// "--gtest_filter=VM.comprehension"
"--gtest_filter=zlib.zlib1"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
Expand Down
2 changes: 1 addition & 1 deletion src/PikaVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 12
#define PIKA_VERSION_MICRO 4

#define PIKA_EDIT_TIME "2023/07/27 17:57:28"
#define PIKA_EDIT_TIME "2023/07/27 18:55:17"
50 changes: 36 additions & 14 deletions src/dataStack.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,41 @@
#include "PikaObj.h"
#include "dataQueue.h"

void _stack_overflow_handler(Stack* stack, size_t stack_require) {
pika_platform_printf(
"OverflowError: pika VM stack overflow, please use bigger "
"PIKA_STACK_BUFF_SIZE\r\n");
pika_platform_printf("Info: stack size request: %d\r\n",
(int)stack_require);
pika_platform_printf("Info: stack size now: %d\r\n",
(int)stack->stack_totle_size);
while (1) {
}
}

uint8_t* stack_popPyload(Stack* stack, int32_t size);

uint8_t* stack_getSpStart(Stack* stack) {
return (uint8_t*)arg_getContent(stack->stack_pyload);
}

int32_t* stack_getSpSizeStart(Stack* stack) {
return (int32_t*)arg_getContent(stack->stack_size_array);
}

uint32_t stack_spFree(Stack* stack) {
return stack->stack_totle_size -
((uintptr_t)stack->sp - (uintptr_t)stack_getSpStart(stack));
}

uint32_t stack_spSizeFree(Stack* stack) {
return stack->stack_totle_size -
((uintptr_t)stack->sp_size - (uintptr_t)stack_getSpSizeStart(stack));
}

void stack_reset(Stack* stack) {
stack->sp = (uint8_t*)arg_getContent(stack->stack_pyload);
stack->sp_size = (int32_t*)arg_getContent(stack->stack_size_array);
stack->sp = stack_getSpStart(stack);
stack->sp_size = stack_getSpSizeStart(stack);
stack->top = 0;
}

Expand All @@ -49,6 +79,7 @@ int32_t stack_init(Stack* stack) {
void stack_pushSize(Stack* stack, int32_t size) {
*(stack->sp_size) = size;
stack->sp_size++;
pika_assert(stack_spSizeFree(stack) >= sizeof(int32_t));
}

int32_t stack_popSize(Stack* stack) {
Expand Down Expand Up @@ -94,18 +125,9 @@ void stack_pushPyload(Stack* stack,
uint8_t* in,
size_t size,
pika_bool is_sample_copy) {
size_t stack_size_after_push =
size + (stack->sp - arg_getContent(stack->stack_pyload));
if (stack_size_after_push > stack->stack_totle_size) {
pika_platform_printf(
"OverflowError: pika VM stack overflow, please use bigger "
"PIKA_STACK_BUFF_SIZE\r\n");
pika_platform_printf("Info: stack size request: %d\r\n",
(int)stack_size_after_push);
pika_platform_printf("Info: stack size now: %d\r\n",
(int)stack->stack_totle_size);
while (1) {
}
size_t stack_require = size + (stack->sp - stack_getSpStart(stack));
if (stack_require > stack->stack_totle_size) {
_stack_overflow_handler(stack, stack_require);
}
Arg* top = (Arg*)stack->sp;
if (is_sample_copy) {
Expand Down
1 change: 1 addition & 0 deletions src/dataStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ int32_t stack_init(Stack* stack);
int32_t stack_popSize(Stack* stack);
void stack_pushSize(Stack* stack, int32_t size);
void stack_reset(Stack* stack);
uint8_t* stack_getSpStart(Stack* stack);
#endif
#ifdef __cplusplus
}
Expand Down

0 comments on commit f9f1e16

Please sign in to comment.