Skip to content

Commit

Permalink
Eliminate memory leak in the native coverage feature
Browse files Browse the repository at this point in the history
If a module was prepared for loading, but the loading never finished,
the memory allocated for coverage information could leak. This bug
was introduced in 141a287.
  • Loading branch information
bjorng committed Dec 11, 2023
1 parent 1cfee42 commit a305eed
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions erts/emulator/beam/jit/asm_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ int beam_load_prepared_dtor(Binary *magic) {
erts_free(ERTS_ALC_T_PREPARED_CODE, hdr->are_nifs);
hdr->are_nifs = NULL;
}
if (hdr->coverage) {
erts_free(ERTS_ALC_T_CODE_COVERAGE, hdr->coverage);
hdr->coverage = NULL;
}
if (hdr->line_coverage_valid) {
erts_free(ERTS_ALC_T_CODE_COVERAGE, hdr->line_coverage_valid);
hdr->line_coverage_valid = NULL;
}

erts_free(ERTS_ALC_T_PREPARED_CODE, hdr);
stp->load_hdr = NULL;
Expand Down Expand Up @@ -871,6 +879,12 @@ int beam_load_finish_emit(LoaderState *stp) {
(const char *)stp->beam.checksum,
sizeof(stp->beam.checksum));

/* Transfer ownership of the coverage tables to the loaded code. */
stp->load_hdr->coverage = stp->coverage;
stp->load_hdr->line_coverage_valid = stp->line_coverage_valid;
stp->coverage = NULL;
stp->line_coverage_valid = NULL;

/* Move the code to its final location. */
beamasm_codegen(stp->ba,
&stp->executable_region,
Expand All @@ -887,13 +901,6 @@ int beam_load_finish_emit(LoaderState *stp) {
stp->code_hdr = code_hdr_ro;
stp->loaded_size = module_size;

/* Transfer ownership of the coverage tables to the loaded code. */
code_hdr_rw->coverage = stp->coverage;
code_hdr_rw->line_coverage_valid = stp->line_coverage_valid;

stp->coverage = NULL;
stp->line_coverage_valid = NULL;

/*
* Place the literals in their own allocated heap (for fast range check)
* and fix up all instructions that refer to it.
Expand Down Expand Up @@ -1134,6 +1141,8 @@ void beam_load_finalize_code(LoaderState *stp,
/* Prevent literals and code from being freed. */
(stp->load_hdr)->literal_area = NULL;
stp->load_hdr->are_nifs = NULL;
stp->load_hdr->coverage = NULL;
stp->load_hdr->line_coverage_valid = NULL;
stp->executable_region = NULL;
stp->writable_region = NULL;
stp->code_hdr = NULL;
Expand Down

0 comments on commit a305eed

Please sign in to comment.