From 95de5d6be80fca0cba67f49821d171defca93335 Mon Sep 17 00:00:00 2001 From: lyon Date: Mon, 2 May 2022 14:48:10 +0800 Subject: [PATCH] using valgrand to find mem error --- port/linux/test/compile-test.cpp | 2 +- src/PikaCompiler.c | 48 +++++++++++++++----------------- src/PikaCompiler.h | 5 +++- src/PikaParser.c | 3 +- src/dataArg.c | 25 +++++++++++++++-- src/dataArg.h | 1 + 6 files changed, 54 insertions(+), 30 deletions(-) diff --git a/port/linux/test/compile-test.cpp b/port/linux/test/compile-test.cpp index ac818d066..196fd4c32 100644 --- a/port/linux/test/compile-test.cpp +++ b/port/linux/test/compile-test.cpp @@ -420,7 +420,7 @@ TEST(lib, lib_to_file) { LibObj_staticLinkFile(lib, "test/python/main_snake_LCD.py.o"); LibObj_listModules(lib); - LibObj_saveToFile(lib, "test/python/lib_to_file.py.a"); + LibObj_saveLibraryFile(lib, "test/python/lib_to_file.py.a"); /* asset */ EXPECT_STREQ(log_buff[0], "UnitTest\r\n"); EXPECT_STREQ(log_buff[1], "main\r\n"); diff --git a/src/PikaCompiler.c b/src/PikaCompiler.c index eb89d7dfa..71eaf49ca 100644 --- a/src/PikaCompiler.c +++ b/src/PikaCompiler.c @@ -95,20 +95,12 @@ int pikaCompile(char* output_file_name, char* py_lines) { */ int pikaCompileFileWithOutputName(char* output_file_name, char* input_file_name) { - char* file_buff = __platform_malloc(PIKA_READ_FILE_BUFF_SIZE); - __platform_memset(file_buff, 0, PIKA_READ_FILE_BUFF_SIZE); - FILE* input_f = __platform_fopen(input_file_name, "r"); - size_t size = - __platform_fread(file_buff, 1, PIKA_READ_FILE_BUFF_SIZE, input_f); - - if (size >= PIKA_READ_FILE_BUFF_SIZE) { - __platform_printf("error: not enough buff for input file.\r\n"); + Arg* input_file_arg = arg_loadFile(NULL, input_file_name); + if (NULL == input_file_arg) { return 1; } - - pikaCompile(output_file_name, file_buff); - __platform_free(file_buff); - __platform_fclose(input_f); + pikaCompile(output_file_name, (char*)arg_getBytes(input_file_arg)); + arg_deinit(input_file_arg); return 0; } @@ -157,25 +149,21 @@ int LibObj_staticLink(LibObj* self, } int LibObj_staticLinkFile(LibObj* self, char* input_file_name) { - char* file_buff = __platform_malloc(PIKA_READ_FILE_BUFF_SIZE); - __platform_memset(file_buff, 0, PIKA_READ_FILE_BUFF_SIZE); Args buffs = {0}; - FILE* input_f = __platform_fopen(input_file_name, "r"); /* read file */ - size_t size = - __platform_fread(file_buff, 1, PIKA_READ_FILE_BUFF_SIZE, input_f); + Arg* input_file_arg = arg_loadFile(NULL, input_file_name); char* module_name = strsGetLastToken(&buffs, input_file_name, '/'); /* cut off '.py.o' */ module_name[strlen(module_name) - (sizeof(".py.o") - 1)] = 0; /* push bytecode */ - LibObj_staticLink(self, module_name, (uint8_t*)file_buff, size); + LibObj_staticLink(self, module_name, arg_getBytes(input_file_arg), + arg_getBytesSize(input_file_arg)); /* deinit */ - __platform_free(file_buff); - __platform_fclose(input_f); strsDeinit(&buffs); + arg_deinit(input_file_arg); return 0; } @@ -211,11 +199,12 @@ static int32_t __foreach_handler_writeIndex(Arg* argEach, Args* handleArgs) { if (arg_getType(argEach) == ARG_TYPE_OBJECT) { PikaObj* module_obj = arg_getPtr(argEach); uint32_t bytecode_size = obj_getBytesSize(module_obj, "buff"); - char buff[32 - sizeof(uint32_t)] = {0}; + char buff[LIB_INFO_BLOCK_SIZE - sizeof(uint32_t)] = {0}; bytecode_size = aline_by(bytecode_size, sizeof(uint32_t)); char* module_name = obj_getStr(module_obj, "name"); __platform_memcpy(buff, module_name, strGetSize(module_name)); - __platform_fwrite(buff, 1, 32 - sizeof(bytecode_size), out_file); + __platform_fwrite(buff, 1, LIB_INFO_BLOCK_SIZE - sizeof(bytecode_size), + out_file); __platform_fwrite(&bytecode_size, 1, sizeof(bytecode_size), out_file); } return 0; @@ -229,7 +218,7 @@ static int32_t __foreach_handler_getModuleNum(Arg* argEach, Args* handleArgs) { return 0; } -int LibObj_saveToFile(LibObj* self, char* output_file_name) { +int LibObj_saveLibraryFile(LibObj* self, char* output_file_name) { FILE* out_file = __platform_fopen(output_file_name, "w+"); Args handleArgs = {0}; @@ -237,13 +226,13 @@ int LibObj_saveToFile(LibObj* self, char* output_file_name) { args_setInt(&handleArgs, "module_num", 0); /* write meta information */ - char buff[32] = {0}; + char buff[LIB_INFO_BLOCK_SIZE] = {0}; args_foreach(self->list, __foreach_handler_getModuleNum, &handleArgs); uint32_t module_num = args_getInt(&handleArgs, "module_num"); /* write module_num to the file */ __platform_memcpy(buff, &module_num, sizeof(uint32_t)); /* aline to 32 bytes */ - __platform_fwrite(buff, 1, 32, out_file); + __platform_fwrite(buff, 1, LIB_INFO_BLOCK_SIZE, out_file); /* write module index to file */ args_foreach(self->list, __foreach_handler_writeIndex, &handleArgs); /* write module bytecode to file */ @@ -254,3 +243,12 @@ int LibObj_saveToFile(LibObj* self, char* output_file_name) { __platform_fclose(out_file); return 0; } + +int LibObj_loadLibrary(LibObj* self, char* library) { + uint32_t module_num = library[0]; + return 0; +} + +int LibObj_loadLibraryFile(LibObj* self, char* input_file_name) { + return 0; +} diff --git a/src/PikaCompiler.h b/src/PikaCompiler.h index 19f4ef004..fdbf22a13 100644 --- a/src/PikaCompiler.h +++ b/src/PikaCompiler.h @@ -17,5 +17,8 @@ int LibObj_staticLink(LibObj* self, size_t size); int LibObj_staticLinkFile(LibObj* self, char* input_file_name); void LibObj_listModules(LibObj* self); -int LibObj_saveToFile(LibObj* self, char* output_file_name); +int LibObj_saveLibraryFile(LibObj* self, char* output_file_name); + +#define LIB_INFO_BLOCK_SIZE 32 + #endif \ No newline at end of file diff --git a/src/PikaParser.c b/src/PikaParser.c index 9b36098bd..0a75b8229 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -96,7 +96,8 @@ char* strsPopTokenWithSkip_byStr(Args* outBuffs, char* strsGetCleanCmd(Args* outBuffs, char* cmd) { int32_t size = strGetSize(cmd); - char* strOut = args_getBuff(outBuffs, size); + /* lexer may generate more chars than input */ + char* strOut = args_getBuff(outBuffs, size * 2); int32_t iOut = 0; ParserState_forEachToken(ps, cmd) { ParserState_iterStart(&ps); diff --git a/src/dataArg.c b/src/dataArg.c index 61acd5946..7c835ffd9 100644 --- a/src/dataArg.c +++ b/src/dataArg.c @@ -59,10 +59,9 @@ static uint8_t* content_init_hash(Hash nameHash, self->name_hash = nameHash; self->type = type; + __platform_memset(self->content, 0, aline_by(size, sizeof(uint32_t))); if (NULL != content) { __platform_memcpy(self->content, content, size); - } else { - __platform_memset(self->content, 0, size); } return (uint8_t*)self; @@ -149,6 +148,7 @@ Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size) { self = arg_setName(self, name); self = arg_setType(self, ARG_TYPE_BYTES); void* dir = arg_getContent(self); + __platform_memset(dir, 0, size + sizeof(size_t)); __platform_memcpy(dir, &size, sizeof(size_t)); __platform_memcpy((void*)((uintptr_t)dir + sizeof(size_t)), src, size); return self; @@ -361,6 +361,27 @@ void arg_deinitHeap(Arg* self) { } } +/* load file as byte array */ +Arg* arg_loadFile(Arg* self, char* filename) { + char* file_buff = __platform_malloc(PIKA_READ_FILE_BUFF_SIZE); + __platform_memset(file_buff, 0, PIKA_READ_FILE_BUFF_SIZE); + FILE* input_file = __platform_fopen(filename, "r"); + Arg* res = New_arg(NULL); + size_t file_size = + __platform_fread(file_buff, 1, PIKA_READ_FILE_BUFF_SIZE, input_file); + + if (file_size >= PIKA_READ_FILE_BUFF_SIZE) { + __platform_printf("error: not enough buff for input file.\r\n"); + return NULL; + } + /* add '\0' to the end of the string */ + res = arg_setBytes(res, "", (uint8_t*)file_buff, file_size + 1); + + __platform_free(file_buff); + __platform_fclose(input_file); + return res; +} + void arg_deinit(Arg* self) { /* deinit arg pointed heap */ arg_deinitHeap(self); diff --git a/src/dataArg.h b/src/dataArg.h index 7565646a7..4653bcdc6 100644 --- a/src/dataArg.h +++ b/src/dataArg.h @@ -138,5 +138,6 @@ void* arg_getHeapStruct(Arg* self); void arg_deinitHeap(Arg* self); Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size); void arg_printBytes(Arg* self); +Arg* arg_loadFile(Arg* self, char* filename); #endif