Skip to content

Commit caa6ee8

Browse files
committed
Fix memleaks in module.c
This fixes some of the memleaks found by ASAN on CI. See https://github.com/software-mansion-labs/FissionVM/tree/ci-asan These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later Signed-off-by: Mateusz Front <mateusz.front@swmansion.com>
1 parent fd8a721 commit caa6ee8

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/libAtomVM/module.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ static enum ModuleLoadResult module_populate_atoms_table(Module *this_module, ui
8585

8686
static enum ModuleLoadResult module_build_imported_functions_table(Module *this_module, uint8_t *table_data, GlobalContext *glb)
8787
{
88-
int functions_count = READ_32_ALIGNED(table_data + 8);
88+
this_module->functions_count = READ_32_ALIGNED(table_data + 8);
8989

90-
this_module->imported_funcs = calloc(functions_count, sizeof(struct ExportedFunction *));
90+
this_module->imported_funcs = calloc(this_module->functions_count, sizeof(struct ExportedFunction *));
9191
if (IS_NULL_PTR(this_module->imported_funcs)) {
9292
fprintf(stderr, "Cannot allocate memory while loading module (line: %i).\n", __LINE__);
9393
return MODULE_ERROR_FAILED_ALLOCATION;
9494
}
9595

96-
for (int i = 0; i < functions_count; i++) {
96+
for (int i = 0; i < this_module->functions_count; ++i) {
9797
int local_module_atom_index = READ_32_ALIGNED(table_data + i * 12 + 12);
9898
int local_function_atom_index = READ_32_ALIGNED(table_data + i * 12 + 4 + 12);
9999
AtomString module_atom = module_get_atom_string_by_id(this_module, local_module_atom_index, glb);
@@ -312,12 +312,30 @@ Module *module_new_from_iff_binary(GlobalContext *global, const void *iff_binary
312312
COLD_FUNC void module_destroy(Module *module)
313313
{
314314
free(module->labels);
315+
for (int i = 0; i < module->functions_count; ++i) {
316+
const struct ExportedFunction *fun = module->imported_funcs[i];
317+
switch (fun->type) {
318+
// Preallocated function types
319+
case NIFFunctionType:
320+
case BIFFunctionType:
321+
case GCBIFFunctionType:
322+
break;
323+
default:
324+
free((void *) fun);
325+
}
326+
}
315327
free(module->imported_funcs);
316328
free(module->literals_table);
317329
free(module->local_atoms_to_global_table);
318330
if (module->free_literals_data) {
319331
free(module->literals_data);
320332
}
333+
free(module->line_refs);
334+
free(module->filenames);
335+
struct ListHead *item, *tmp;
336+
MUTABLE_LIST_FOR_EACH (item, tmp, &module->line_ref_offsets) {
337+
free(GET_LIST_ENTRY(item, struct LineRefOffset, head));
338+
}
321339
#ifndef AVM_NO_SMP
322340
smp_mutex_destroy(module->mutex);
323341
#endif

src/libAtomVM/module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ struct Module
116116
struct ListHead line_ref_offsets;
117117

118118
const struct ExportedFunction **imported_funcs;
119+
int functions_count;
119120

120121
const uint8_t **labels;
121122

0 commit comments

Comments
 (0)