Skip to content

Commit 433ac33

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
1 parent 33af056 commit 433ac33

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/libAtomVM/module.c

+21-3
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ static enum ModuleLoadResult module_populate_atoms_table(Module *this_module, ui
9292

9393
static enum ModuleLoadResult module_build_imported_functions_table(Module *this_module, uint8_t *table_data, GlobalContext *glb)
9494
{
95-
int functions_count = READ_32_ALIGNED(table_data + 8);
95+
this_module->functions_count = READ_32_ALIGNED(table_data + 8);
9696

97-
this_module->imported_funcs = calloc(functions_count, sizeof(struct ExportedFunction *));
97+
this_module->imported_funcs = calloc(this_module->functions_count, sizeof(struct ExportedFunction *));
9898
if (IS_NULL_PTR(this_module->imported_funcs)) {
9999
fprintf(stderr, "Cannot allocate memory while loading module (line: %i).\n", __LINE__);
100100
return MODULE_ERROR_FAILED_ALLOCATION;
101101
}
102102

103-
for (int i = 0; i < functions_count; i++) {
103+
for (int i = 0; i < this_module->functions_count; ++i) {
104104
int local_module_atom_index = READ_32_ALIGNED(table_data + i * 12 + 12);
105105
int local_function_atom_index = READ_32_ALIGNED(table_data + i * 12 + 4 + 12);
106106
AtomString module_atom = module_get_atom_string_by_id(this_module, local_module_atom_index, glb);
@@ -319,12 +319,30 @@ Module *module_new_from_iff_binary(GlobalContext *global, const void *iff_binary
319319
COLD_FUNC void module_destroy(Module *module)
320320
{
321321
free(module->labels);
322+
for (int i = 0; i < module->functions_count; ++i) {
323+
const struct ExportedFunction *fun = module->imported_funcs[i];
324+
switch (fun->type) {
325+
// Preallocated function types
326+
case NIFFunctionType:
327+
case BIFFunctionType:
328+
case GCBIFFunctionType:
329+
break;
330+
default:
331+
free((void *) fun);
332+
}
333+
}
322334
free(module->imported_funcs);
323335
free(module->literals_table);
324336
free(module->local_atoms_to_global_table);
325337
if (module->free_literals_data) {
326338
free(module->literals_data);
327339
}
340+
free(module->line_refs);
341+
free(module->filenames);
342+
struct ListHead *item, *tmp;
343+
MUTABLE_LIST_FOR_EACH (item, tmp, &module->line_ref_offsets) {
344+
free(GET_LIST_ENTRY(item, struct LineRefOffset, head));
345+
}
328346
#ifndef AVM_NO_SMP
329347
smp_mutex_destroy(module->mutex);
330348
#endif

src/libAtomVM/module.h

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ struct Module
114114
struct ListHead line_ref_offsets;
115115

116116
const struct ExportedFunction **imported_funcs;
117+
int functions_count;
117118

118119
const uint8_t **labels;
119120

0 commit comments

Comments
 (0)