File 2366-Eliminate-memory-leak-in-the-native-coverage-feature.patch of Package erlang
From aaf99674d75abf0a05523214355a5b46f192f777 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Mon, 11 Dec 2023 13:07:14 +0100
Subject: [PATCH] Eliminate memory leak in the native coverage feature
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 141a287d2cd293c0d0a4f94edfb3852f5e110ca3.
---
erts/emulator/beam/jit/asm_load.c | 23 ++++++++++++++++-------
1 file changed, 16 insertions(+), 7 deletions(-)
diff --git a/erts/emulator/beam/jit/asm_load.c b/erts/emulator/beam/jit/asm_load.c
index 52299031c0..9e3d64053a 100644
--- a/erts/emulator/beam/jit/asm_load.c
+++ b/erts/emulator/beam/jit/asm_load.c
@@ -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;
@@ -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 prepared 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,
@@ -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.
@@ -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;
--
2.35.3