Skip to content

Commit

Permalink
[pg15] fix: intermittent memory leak failure in initdb in asan
Browse files Browse the repository at this point in the history
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
  • Loading branch information
arpang committed Sep 5, 2024
1 parent c11feef commit 54bcccd
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/postgres/src/bin/initdb/initdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 54bcccd

Please sign in to comment.