git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 06/10] test-lib: add a SANITIZE=leak logging mode

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jul 19, 2022, 21:05 UTC
Message-ID
<patch-06.10-ad1395f45af-20220719T205710Z-avarab@gmail.com>
In-Reply-To
<cover-00.10-00000000000-20220719T205710Z-avarab@gmail.com>

Add the ability to run the test suite under a new "GIT_TEST_SANITIZE_LEAK_LOG=true" mode, when true we'll log the leaks we find an a new "test-results/<test-name>.leak" directory.

That new path is consistent with the existing "test-results/<test-name>.<type>" results, except that those are all files, not directories.

We also set "log_exe_name=1" to include the name of the executable in the filename. This gives us files like "trace.git.<pid>" instead of the default of "trace.<pid>". I.e. we'll be able to distinguish "git" leaks from "test-tool", "git-daemon" etc.

We then set "dedup_token_length" to non-zero ("0" is the default) to succinctly log a token we can de-duplicate these stacktraces on. The string is simply a one-line stack-trace with only function names up to N frames, which we limit at "9999" as a shorthand for "infinite" (there appears to be no way to say "no limit").

With these combined we can now easily get e.g. the top 10 leaks in the test suite grouped by full stacktrace:

    grep -o -P -h '(?<=DEDUP_TOKEN: ).*' test-results/*.leak/trace.git.* | sort | uniq -c | sort -nr | head -n 10

Or add "grep -E -o '[^-]+'" to that to group by functions instead of stack traces:

    grep -o -P -h '(?<=DEDUP_TOKEN: ).*' test-results/*.leak/trace.git.* | grep -E -o '[^-]+' | sort | uniq -c | sort -nr | head -n 20
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/README      |  6 ++++++
 t/test-lib.sh | 21 +++++++++++++++++++++
 2 files changed, 27 insertions(+)
diff --git a/t/README b/t/README
index 7f409392537..c2f996656d3 100644
--- a/t/README
+++ b/t/README
@@ -371,6 +371,12 @@ declared themselves as leak-free by setting
 "TEST_PASSES_SANITIZE_LEAK=true" before sourcing "test-lib.sh". This
 test mode is used by the "linux-leaks" CI target.
 
+GIT_TEST_SANITIZE_LEAK_LOG=true will log memory leaks to
+"test-results/$TEST_NAME.leak/trace.*" files. Useful in combination
+with "GIT_TEST_PASSING_SANITIZE_LEAK" to check if we're falsely
+reporting a test as "passing" with SANITIZE=leak due to ignored exit
+codes.
+
 GIT_TEST_PROTOCOL_VERSION=<n>, when set, makes 'protocol.version'
 default to n.
 
diff --git a/t/test-lib.sh b/t/test-lib.sh
index 7f730b0f94f..73600c4d887 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -305,6 +305,10 @@ TEST_NUMBER="${TEST_NAME%%-*}"
 TEST_NUMBER="${TEST_NUMBER#t}"
 TEST_RESULTS_DIR="$TEST_OUTPUT_DIRECTORY/test-results"
 TEST_RESULTS_BASE="$TEST_RESULTS_DIR/$TEST_NAME$TEST_STRESS_JOB_SFX"
+TEST_RESULTS_SAN_FILE_PFX=trace
+TEST_RESULTS_SAN_DIR_SFX=leak
+TEST_RESULTS_SAN_FILE=
+TEST_RESULTS_SAN_DIR="$TEST_RESULTS_DIR/$TEST_NAME.$TEST_RESULTS_SAN_DIR_SFX"
 TRASH_DIRECTORY="trash directory.$TEST_NAME$TEST_STRESS_JOB_SFX"
 test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
 case "$TRASH_DIRECTORY" in
@@ -1455,6 +1459,23 @@ then
 			test_done
 		fi
 	fi
+
+	if test_bool_env GIT_TEST_SANITIZE_LEAK_LOG false
+	then
+		if ! mkdir -p "$TEST_RESULTS_SAN_DIR"
+		then
+			BAIL_OUT "cannot create $TEST_RESULTS_SAN_DIR"
+		fi &&
+		TEST_RESULTS_SAN_FILE="$TEST_RESULTS_SAN_DIR/$TEST_RESULTS_SAN_FILE_PFX"
+
+		# Don't litter *.leak dirs if there was nothing to report
+		test_atexit "rmdir \"$TEST_RESULTS_SAN_DIR\" 2>/dev/null || :"
+
+		prepend_var LSAN_OPTIONS : dedup_token_length=9999
+		prepend_var LSAN_OPTIONS : log_exe_name=1
+		prepend_var LSAN_OPTIONS : log_path=\"$TEST_RESULTS_SAN_FILE\"
+		export LSAN_OPTIONS
+	fi
 elif test_bool_env GIT_TEST_PASSING_SANITIZE_LEAK false
 then
 	BAIL_OUT "GIT_TEST_PASSING_SANITIZE_LEAK=true has no effect except when compiled with SANITIZE=leak"
-- 
2.37.1.1062.g385eac7fccf
Previous: Derrick StoleeNext: Ævar Arnfjörð Bjarmason
Message 8 of 57 in “leak test: add "check" test mode, mark leak-free tests”
  1. 00/10 leak test: add "check" test mode, mark leak-free testsÆvar Arnfjörð Bjarmason, Jul 19, 2022
  2. 01/10 test-lib.sh: use $1, not $@ in test_known_broken_{ok,failure}_Ævar Arnfjörð Bjarmason, Jul 19, 2022
  3. 02/10 test-lib.sh: don't set GIT_EXIT_OK before calling test_atexit_handlerÆvar Arnfjörð Bjarmason, Jul 19, 2022
  4. 03/10 test-lib.sh: fix GIT_EXIT_OK logic errors, use BAIL_OUTÆvar Arnfjörð Bjarmason, Jul 19, 2022
  5. 04/10 test-lib.sh: add a --invert-exit-code switchÆvar Arnfjörð Bjarmason, Jul 19, 2022
  6. 07/10 test-lib.sh: add a GIT_TEST_PASSING_SANITIZE_LEAK=check modeÆvar Arnfjörð Bjarmason, Jul 19, 2022
  7. Derrick StoleeJul 20, 2022
  8. 06/10 test-lib: add a SANITIZE=leak logging modeÆvar Arnfjörð Bjarmason, Jul 19, 2022
  9. 05/10 t/README: reword the "GIT_TEST_PASSING_SANITIZE_LEAK" descriptionÆvar Arnfjörð Bjarmason, Jul 19, 2022
  10. Derrick StoleeJul 20, 2022
  11. 09/10 leak tests: mark passing SANITIZE=leak tests as leak-freeÆvar Arnfjörð Bjarmason, Jul 19, 2022
  12. Derrick StoleeJul 20, 2022
  13. 08/10 test-lib: have the "check" mode for SANITIZE=leak consider leak logsÆvar Arnfjörð Bjarmason, Jul 19, 2022
  14. Derrick StoleeJul 20, 2022
  15. 10/10 log tests: don't use "exit 1" outside a sub-shellÆvar Arnfjörð Bjarmason, Jul 19, 2022
  16. Junio C HamanoJul 20, 2022
  17. 00/14 leak test: add "check" test mode, mark leak-free testsÆvar Arnfjörð Bjarmason, Jul 20, 2022
  18. 01/14 test-lib: use $1, not $@ in test_known_broken_{ok,failure}_Ævar Arnfjörð Bjarmason, Jul 20, 2022
  19. 02/14 test-lib: don't set GIT_EXIT_OK before calling test_atexit_handlerÆvar Arnfjörð Bjarmason, Jul 20, 2022
  20. 03/14 test-lib: fix GIT_EXIT_OK logic errors, use BAIL_OUTÆvar Arnfjörð Bjarmason, Jul 20, 2022
  21. 04/14 test-lib: add a --invert-exit-code switchÆvar Arnfjörð Bjarmason, Jul 20, 2022
  22. 05/14 t/README: reword the "GIT_TEST_PASSING_SANITIZE_LEAK" descriptionÆvar Arnfjörð Bjarmason, Jul 20, 2022
  23. 06/14 test-lib: add a SANITIZE=leak logging modeÆvar Arnfjörð Bjarmason, Jul 20, 2022
  24. 07/14 t/Makefile: don't remove test-results in "clean-except-prove-cache"Ævar Arnfjörð Bjarmason, Jul 20, 2022
  25. 08/14 tests: move copy/pasted PERL + Test::More checks to a lib-perl.shÆvar Arnfjörð Bjarmason, Jul 20, 2022
  26. 10/14 test-lib: add a GIT_TEST_PASSING_SANITIZE_LEAK=check modeÆvar Arnfjörð Bjarmason, Jul 20, 2022
  27. 09/14 test-lib: simplify by removing test_externalÆvar Arnfjörð Bjarmason, Jul 20, 2022
  28. 11/14 test-lib: have the "check" mode for SANITIZE=leak consider leak logsÆvar Arnfjörð Bjarmason, Jul 20, 2022
  29. 12/14 leak tests: mark passing SANITIZE=leak tests as leak-freeÆvar Arnfjörð Bjarmason, Jul 20, 2022
  30. 13/14 upload-pack: fix a memory leak in create_pack_file()Ævar Arnfjörð Bjarmason, Jul 20, 2022
  31. 14/14 CI: use "GIT_TEST_SANITIZE_LEAK_LOG=true" in linux-leaksÆvar Arnfjörð Bjarmason, Jul 20, 2022
  32. 00/15 leak test: add "check" test mode, mark leak-free testsÆvar Arnfjörð Bjarmason, Jul 27, 2022
  33. 01/15 test-lib: use $1, not $@ in test_known_broken_{ok,failure}_Ævar Arnfjörð Bjarmason, Jul 27, 2022
  34. 02/15 test-lib: don't set GIT_EXIT_OK before calling test_atexit_handlerÆvar Arnfjörð Bjarmason, Jul 27, 2022
  35. 03/15 test-lib: fix GIT_EXIT_OK logic errors, use BAIL_OUTÆvar Arnfjörð Bjarmason, Jul 27, 2022
  36. 05/15 t/README: reword the "GIT_TEST_PASSING_SANITIZE_LEAK" descriptionÆvar Arnfjörð Bjarmason, Jul 27, 2022
  37. 04/15 test-lib: add a --invert-exit-code switchÆvar Arnfjörð Bjarmason, Jul 27, 2022
  38. 06/15 test-lib: add a SANITIZE=leak logging modeÆvar Arnfjörð Bjarmason, Jul 27, 2022
  39. 07/15 t/Makefile: don't remove test-results in "clean-except-prove-cache"Ævar Arnfjörð Bjarmason, Jul 27, 2022
  40. t/Makefile: remove 'test-results' on 'make clean'SZEDER Gábor, Sep 20, 2022
  41. Jeff KingSep 20, 2022
  42. SZEDER GáborSep 20, 2022
  43. Jeff KingSep 20, 2022
  44. t/Makefile: remove 'test-results' on 'make clean'SZEDER Gábor, Sep 20, 2022
  45. Ævar Arnfjörð BjarmasonSep 21, 2022
  46. Junio C HamanoSep 21, 2022
  47. Junio C HamanoSep 21, 2022
  48. Ævar Arnfjörð BjarmasonSep 26, 2022
  49. Junio C HamanoSep 26, 2022
  50. 08/15 tests: move copy/pasted PERL + Test::More checks to a lib-perl.shÆvar Arnfjörð Bjarmason, Jul 27, 2022
  51. 09/15 test-lib: simplify by removing test_externalÆvar Arnfjörð Bjarmason, Jul 27, 2022
  52. 10/15 test-lib: add a GIT_TEST_PASSING_SANITIZE_LEAK=check modeÆvar Arnfjörð Bjarmason, Jul 27, 2022
  53. 12/15 leak tests: don't skip some tests under SANITIZE=leakÆvar Arnfjörð Bjarmason, Jul 27, 2022
  54. 11/15 test-lib: have the "check" mode for SANITIZE=leak consider leak logsÆvar Arnfjörð Bjarmason, Jul 27, 2022
  55. 13/15 leak tests: mark passing SANITIZE=leak tests as leak-freeÆvar Arnfjörð Bjarmason, Jul 27, 2022
  56. 15/15 CI: use "GIT_TEST_SANITIZE_LEAK_LOG=true" in linux-leaksÆvar Arnfjörð Bjarmason, Jul 27, 2022
  57. 14/15 upload-pack: fix a memory leak in create_pack_file()Ævar Arnfjörð Bjarmason, Jul 27, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.