From 54bcccdeade1ab1a8312adecebcc5cd5f1775345 Mon Sep 17 00:00:00 2001 From: Arpan Agrawal Date: Thu, 5 Sep 2024 11:36:00 +0530 Subject: [PATCH] [pg15] fix: intermittent memory leak failure in initdb in asan Summary: The test CreateInitialSysCatalogSnapshot intermittently fails on asan build with the following stack trace: [m-1] #1 0x5578be7959dc in pg_realloc /home/centos/code/yugabyte-db/src/postgres/src/common/../../../../../src/postgres/src/common/fe_memutils.c:72:8 [m-1] #2 0x5578be77b6b1 in readfile /home/centos/code/yugabyte-db/src/postgres/src/bin/initdb/../../../../../../src/postgres/src/bin/initdb/initdb.c:537:23 [m-1] #3 0x5578be77837b in bootstrap_template1 /home/centos/code/yugabyte-db/src/postgres/src/bin/initdb/../../../../../../src/postgres/src/bin/initdb/initdb.c:1434:14 This happens because during the execution of `bootstrap_template1()` in initdb, `bki_lines` first points to the memory allocated by readfile(). It then points to the memory allocated by replace_token(), without freeing the memory it was previously pointing to. Fix the issue by freeing up the memory allocated by readfile(). Note that there are more instances of memory leakage in initdb that are not detected by asan runs for some reason. For instance, memory allocated by replace_token() is never freed. These leakages are present in the YB master branch and upstream PG as well. Upstream PG doesn't care about it (https://www.postgresql.org/message-id/28473.1582440206%40sss.pgh.pa.us). The same reasoning applies to YB too. Also to prevent unnecessary deviation from PG code, we can let them remain. Test Plan: Jenkins: rebase: pg15 ./yb_build.sh asan --cxx-test create_initial_sys_catalog_snapshot --gtest_filter CreateInitialSysCatalogSnapshotTest.CreateInitialSysCatalogSnapshot -n 100 Reviewers: jason Reviewed By: jason Subscribers: svc_phabricator, yql Differential Revision: https://phorge.dev.yugabyte.com/D37736 --- src/postgres/src/bin/initdb/initdb.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/postgres/src/bin/initdb/initdb.c b/src/postgres/src/bin/initdb/initdb.c index d570d7314b26..99906fa1295c 100644 --- a/src/postgres/src/bin/initdb/initdb.c +++ b/src/postgres/src/bin/initdb/initdb.c @@ -1427,11 +1427,13 @@ bootstrap_template1(void) char **bki_lines; char headerline[MAXPGPATH]; char buf[64]; + char **yb_orig_bki_lines; printf(_("running bootstrap script ... ")); fflush(stdout); bki_lines = readfile(bki_file); + yb_orig_bki_lines = bki_lines; /* Check that bki file appears to be of the right version */ @@ -1510,6 +1512,13 @@ bootstrap_template1(void) free(bki_lines); + /* + * YB note: free the memory allocated by readfile(), if not done already. + * Without this, initdb fails on ASAN intermittently. + */ + if (yb_orig_bki_lines != bki_lines) + free(yb_orig_bki_lines); + check_ok(); }