Skip to content

Commit e8902da

Browse files
authored
Fix memory leak in deep_main. (#1)
1. Implement specific free functions for DEEPStack & DEEPModule. 2. Change int32_t to uint32_t consistently when applicable.
1 parent e70e027 commit e8902da

File tree

5 files changed

+69
-37
lines changed

5 files changed

+69
-37
lines changed

include/deep_interp.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ typedef struct DEEPExecEnv {
3636
//创建操作数栈
3737
DEEPStack *stack_cons(void);
3838

39+
//销毁操作数栈
40+
void stack_free(DEEPStack *stack);
41+
3942
int32_t call_main(DEEPExecEnv *current_env, DEEPModule *module);
4043

4144
void call_function(DEEPExecEnv *current_env, DEEPModule *module, int func_index);
4245

4346
#endif /* _DEEP_INTERP_H */
44-
45-

include/deep_loader.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,51 @@
2929
//type item
3030
typedef struct DEEPType
3131
{
32-
int32_t param_count;
33-
int32_t ret_count;
32+
uint32_t param_count;
33+
uint32_t ret_count;
3434
uint8_t type[1];
3535
} DEEPType;
3636

3737
//local variables item
3838
typedef struct LocalVars
3939
{
40-
int32_t count;
41-
int16_t local_type;
40+
uint32_t count;
41+
uint8_t local_type;
4242
} LocalVars;
4343

4444
//function item
4545
typedef struct DEEPFunction
4646
{
4747
DEEPType *func_type; // the type of function
4848
LocalVars **localvars;
49-
int32_t code_size;
49+
uint32_t code_size;
5050
uint8_t *code_begin;
5151
} DEEPFunction;
5252

5353
//
5454
typedef struct DEEPExport
5555
{
5656
char* name;
57-
int32_t index;
57+
uint32_t index;
5858
char tag;
5959
} DEEPExport;
6060

6161
//
6262
typedef struct DEEPData
6363
{
64-
int32_t offset;
65-
int32_t datasize;
64+
uint32_t offset;
65+
uint32_t datasize;
6666
uint8_t *data;
6767
} DEEPData;
6868

6969
/* Data structure of module, at present we only support
7070
two sections, which can make the program run*/
7171
typedef struct DEEPModule
7272
{
73-
int32_t type_count;
74-
int32_t function_count;
75-
int32_t export_count;
76-
int32_t data_count;
73+
uint32_t type_count;
74+
uint32_t function_count;
75+
uint32_t export_count;
76+
uint32_t data_count;
7777
DEEPType **type_section;
7878
DEEPFunction **func_section;
7979
DEEPExport **export_section;
@@ -90,6 +90,7 @@ typedef struct section_listnode
9090
} section_listnode;
9191

9292
DEEPModule* deep_load(uint8_t** p, int32_t size);
93+
void module_free(DEEPModule *module);
9394
uint32_t read_leb_u32(uint8_t** p);
9495
int32_t read_leb_i32(uint8_t** p);
9596
uint8_t* init_memory(uint32_t min_page);

src/deep_interp.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ DEEPStack *stack_cons(void) {
4040
return stack;
4141
}
4242

43+
//销毁操作数栈
44+
void stack_free(DEEPStack *stack) {
45+
free(stack->sp);
46+
free(stack);
47+
}
48+
4349
//执行代码块指令
4450
void exec_instructions(DEEPExecEnv *current_env, DEEPModule *module) {
4551
uint32_t *sp = current_env->cur_frame->sp;
@@ -279,14 +285,14 @@ void call_function(DEEPExecEnv *current_env, DEEPModule *module, int func_index)
279285

280286
//函数类型
281287
DEEPType *deepType = func->func_type;
282-
int param_num = deepType->param_count;
283-
int ret_num = deepType->ret_count;
288+
uint32_t param_num = deepType->param_count;
289+
uint32_t ret_num = deepType->ret_count;
284290

285291
// current_env->sp-=param_num;//操作数栈指针下移
286292
current_env->local_vars = (uint32_t *) malloc(sizeof(uint32_t) * param_num);
287-
int vars_temp = param_num;
293+
uint32_t vars_temp = param_num;
288294
while (vars_temp > 0) {
289-
int temp = *(--current_env->sp);
295+
uint32_t temp = *(--current_env->sp);
290296
current_env->local_vars[(vars_temp--) - 1] = temp;
291297
}
292298

@@ -324,8 +330,8 @@ int32_t call_main(DEEPExecEnv *current_env, DEEPModule *module) {
324330
//create DEEPFunction for main
325331
//find the index of main
326332
int main_index = -1;
327-
int export_count = module->export_count;
328-
for (int i = 0; i < export_count; i++) {
333+
uint32_t export_count = module->export_count;
334+
for (uint32_t i = 0; i < export_count; i++) {
329335
if (strcmp((module->export_section[i])->name, "main") == 0) {
330336
main_index = module->export_section[i]->index;
331337
}

src/deep_loader.c

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ static section_listnode* create_section_list(const uint8_t** p, int32_t size) {
9393
static void decode_type_section(const uint8_t* p, DEEPModule* module) {
9494

9595
const uint8_t* p_tmp;
96-
int32_t total_size = 0;
97-
int32_t type_count = 0, param_count = 0, ret_count = 0;
96+
uint32_t total_size = 0;
97+
uint32_t type_count = 0, param_count = 0, ret_count = 0;
9898
module->type_count = type_count = read_leb_u32((uint8_t**)&p);
9999
module->type_section = (DEEPType**)malloc(type_count * sizeof(DEEPType*));
100100
for (int32_t i = 0; i < type_count; i++) {
@@ -110,11 +110,11 @@ static void decode_type_section(const uint8_t* p, DEEPModule* module) {
110110
module->type_section[i]->param_count = param_count;
111111
module->type_section[i]->ret_count = ret_count;
112112

113-
for (int32_t j = 0; j < param_count; j++) {
113+
for (uint32_t j = 0; j < param_count; j++) {
114114
module->type_section[i]->type[j] = READ_BYTE(p);
115115
}
116116
read_leb_u32((uint8_t**)&p);
117-
for (int32_t j = 0; j < ret_count; j++) {
117+
for (uint32_t j = 0; j < ret_count; j++) {
118118
module->type_section[i]->type[param_count + j] = READ_BYTE(p);
119119
}
120120
}
@@ -123,17 +123,17 @@ static void decode_type_section(const uint8_t* p, DEEPModule* module) {
123123

124124
//读取函数段
125125
static void decode_func_section(const uint8_t* p, DEEPModule* module,const uint8_t* p_code) {
126-
int32_t func_count = read_leb_u32((uint8_t**)&p);
127-
int32_t code_func_count = read_leb_u32((uint8_t**)&p_code);
128-
int32_t type_index, code_size, local_set_count;
126+
uint32_t func_count = read_leb_u32((uint8_t**)&p);
127+
uint32_t code_func_count = read_leb_u32((uint8_t**)&p_code);
128+
uint32_t type_index, code_size, local_set_count;
129129
DEEPFunction* func;
130130
LocalVars* local_set;
131131
const uint8_t* p_code_temp;
132132
if (func_count == code_func_count) {
133133
module->function_count = func_count;
134134
module->func_section = (DEEPFunction**)malloc(func_count * sizeof(DEEPFunction*));
135135

136-
for (int32_t i = 0; i < func_count; i++) {
136+
for (uint32_t i = 0; i < func_count; i++) {
137137
func = module->func_section[i] = (DEEPFunction*)malloc(sizeof(DEEPFunction));
138138
memset(func, 0, sizeof(DEEPFunction));
139139
type_index = read_leb_u32((uint8_t**)&p);
@@ -146,7 +146,7 @@ static void decode_func_section(const uint8_t* p, DEEPModule* module,const uint8
146146
func->localvars = NULL;
147147
} else {
148148
func->localvars = (LocalVars**)malloc(local_set_count * sizeof(LocalVars*));
149-
for (int32_t j = 0; j < local_set_count; j++) {
149+
for (uint32_t j = 0; j < local_set_count; j++) {
150150
local_set = func->localvars[j] = (LocalVars*)malloc(sizeof(LocalVars));
151151
local_set->count = read_leb_u32((uint8_t**)&p_code);
152152
local_set->local_type = READ_BYTE(p_code);
@@ -160,13 +160,13 @@ static void decode_func_section(const uint8_t* p, DEEPModule* module,const uint8
160160

161161
//读取导出段
162162
static void decode_export_section(const uint8_t* p, DEEPModule* module) {
163-
int32_t export_count = read_leb_u32((uint8_t**)&p);
164-
int32_t name_len;
163+
uint32_t export_count = read_leb_u32((uint8_t**)&p);
164+
uint32_t name_len;
165165
DEEPExport* Export;
166166
module->export_count = export_count;
167167
module->export_section = (DEEPExport**)malloc(export_count * sizeof(DEEPExport*));
168168

169-
for (int32_t i = 0; i < export_count; i ++) {
169+
for (uint32_t i = 0; i < export_count; i ++) {
170170
Export = module->export_section[i] = (DEEPExport*)malloc(sizeof(DEEPExport));
171171
name_len = read_leb_u32((uint8_t**)&p);
172172
Export->name = str_gen(p, name_len);
@@ -178,12 +178,12 @@ static void decode_export_section(const uint8_t* p, DEEPModule* module) {
178178

179179
//读取数据段
180180
static void decode_data_section(const uint8_t* p, DEEPModule* module) {
181-
int32_t data_count = read_leb_u32((uint8_t**)&p);
181+
uint32_t data_count = read_leb_u32((uint8_t**)&p);
182182
DEEPData* Data;
183183
module->data_count = data_count;
184184
module->data_section = (DEEPData**)malloc(data_count * sizeof(DEEPData*));
185185

186-
for (int32_t i = 0; i < data_count; i ++) {
186+
for (uint32_t i = 0; i < data_count; i ++) {
187187
Data = module->data_section[i] = (DEEPData*)malloc(sizeof(DEEPData));
188188
READ_BYTE(p);
189189
p ++;
@@ -282,6 +282,28 @@ DEEPModule* deep_load(uint8_t** p, int size) {
282282
return module;
283283
}
284284

285+
void module_free(DEEPModule *module) {
286+
uint32_t i;
287+
for (i = 0; i < module->data_count; i++) {
288+
free(module->data_section[i]);
289+
}
290+
free(module->data_section);
291+
for (i = 0; i < module->type_count; i++) {
292+
free(module->type_section[i]);
293+
}
294+
free(module->type_section);
295+
for (i = 0; i < module->function_count; i++) {
296+
free(module->func_section[i]);
297+
}
298+
free(module->func_section);
299+
for (i = 0; i < module->export_count; i++) {
300+
free(module->export_section[i]->name);
301+
free(module->export_section[i]);
302+
}
303+
free(module->export_section);
304+
free(module);
305+
}
306+
285307
//内存模块实现
286308
#define PAGESIZE 65536
287309

@@ -382,4 +404,3 @@ void type_section_dump(DEEPModule* module) {
382404
}
383405
printf("%s\n", "========================================================");
384406
}
385-

src/deep_main.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stdio.h>
33
#include <stdlib.h>
44
#include <stdint.h>
5+
#include <string.h>
56
#include "deep_interp.h"
67
#include "deep_loader.h"
78
#include "deep_log.h"
@@ -16,6 +17,7 @@ int32_t main(int argv, char **args) {
1617
path = args[1];
1718
}
1819
uint8_t *q = (uint8_t *) malloc(WASM_FILE_SIZE);
20+
memset(q, 0, WASM_FILE_SIZE); // Suppress valgrind warning
1921
uint8_t *p = q;
2022
if (p == NULL) {
2123
error("malloc fail.");
@@ -53,9 +55,10 @@ int32_t main(int argv, char **args) {
5355

5456
/* release memory */
5557
fclose(fp);
56-
free(stack);
57-
free(module);
58+
stack_free(stack);
59+
module_free(module);
5860
free(current_env->global_vars);
61+
free(current_env->memory);
5962
free(q);
6063
p = NULL;
6164
return 0;

0 commit comments

Comments
 (0)