Skip to content

Fix memleaks in module.c #1602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release-0.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/libAtomVM/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ static enum ModuleLoadResult module_populate_atoms_table(Module *this_module, ui

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

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

for (int i = 0; i < functions_count; i++) {
for (int i = 0; i < this_module->functions_count; ++i) {
int local_module_atom_index = READ_32_ALIGNED(table_data + i * 12 + 12);
int local_function_atom_index = READ_32_ALIGNED(table_data + i * 12 + 4 + 12);
AtomString module_atom = module_get_atom_string_by_id(this_module, local_module_atom_index, glb);
Expand Down Expand Up @@ -312,12 +312,30 @@ Module *module_new_from_iff_binary(GlobalContext *global, const void *iff_binary
COLD_FUNC void module_destroy(Module *module)
{
free(module->labels);
for (int i = 0; i < module->functions_count; ++i) {
const struct ExportedFunction *fun = module->imported_funcs[i];
switch (fun->type) {
// Preallocated function types
case NIFFunctionType:
case BIFFunctionType:
case GCBIFFunctionType:
break;
default:
free((void *) fun);
}
}
free(module->imported_funcs);
free(module->literals_table);
free(module->local_atoms_to_global_table);
if (module->free_literals_data) {
free(module->literals_data);
}
free(module->line_refs);
free(module->filenames);
Copy link
Collaborator

@bettio bettio Mar 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense only in main since filenames is not available in release-0.6 branch.
The correct fix would be splitting this PR in 2, one for release-0.6 and one for main, but:
this memory leak for now is relevant only during tests so it doesn't have any practical impact on devices running release-0.6, so we can go for the lazy option that is basing this on top of main.

struct ListHead *item, *tmp;
MUTABLE_LIST_FOR_EACH (item, tmp, &module->line_ref_offsets) {
free(GET_LIST_ENTRY(item, struct LineRefOffset, head));
}
#ifndef AVM_NO_SMP
smp_mutex_destroy(module->mutex);
#endif
Expand Down
1 change: 1 addition & 0 deletions src/libAtomVM/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ struct Module
struct ListHead line_ref_offsets;

const struct ExportedFunction **imported_funcs;
int functions_count;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of saving sizeof(int) for every module, I would argue for recomputing this when the module is destroyed, especially considering we don't destroy modules at all for now.


const uint8_t **labels;

Expand Down
Loading