check overflow for VM stack

This commit is contained in:
lyon 2022-05-27 12:53:27 +08:00
parent b49c7d5c7c
commit 2fa39bb973
4 changed files with 30 additions and 2 deletions

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=pikaMain.list_iter"
"--gtest_filter=pikaMain.big_bytes"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -1890,4 +1890,17 @@ TEST(pikaMain, int_from_bytes) {
/* deinit */
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
}
}
// TEST(pikaMain, big_bytes) {
// /* init */
// pikaMemInfo.heapUsedMax = 0;
// /* run */
// PikaObj* self = newRootObj("pikaMain", New_PikaMain);
// __platform_printf("BEGIN\r\n");
// obj_run(self, "b = bytes(8192)\n");
// /* assert */
// /* deinit */
// obj_deinit(self);
// EXPECT_EQ(pikaMemNow(), 0);
// }

View File

@ -40,6 +40,7 @@ int32_t stack_init(Stack* stack) {
stack->stack_size_array =
arg_setContent(NULL, NULL, PIKA_STACK_BUFF_SIZE / 4);
stack_reset(stack);
stack->stack_totle_size = PIKA_STACK_BUFF_SIZE;
return 0;
};
@ -60,6 +61,19 @@ int32_t stack_deinit(Stack* stack) {
}
void stack_pushPyload(Stack* stack, Arg* content, size_t size) {
size_t stack_size_after_push =
size + (stack->sp - arg_getContent(stack->stack_pyload));
if (stack_size_after_push > stack->stack_totle_size) {
__platform_printf(
"OverflowError: pika VM stack overflow, please use bigger "
"PIKA_STACK_BUFF_SIZE\r\n");
__platform_printf("Info: stack size request: %d\r\n",
stack_size_after_push);
__platform_printf("Info: stack size now: %d\r\n",
stack->stack_totle_size);
while (1) {
}
}
__platform_memcpy(stack->sp, content, size);
stack->sp += size;
}

View File

@ -35,6 +35,7 @@ typedef struct Stack_t {
uint8_t* sp;
int16_t* sp_size;
int16_t top;
size_t stack_totle_size;
} Stack;
int32_t stack_deinit(Stack* stack);