test-lib: move malloc-debug setup after $PATH setup

Originally, the conditional definition of the setup/teardown functions for malloc checking could be run at any time, because they depended only on command-line options and the system getconf function. But since 02d900361c (test-lib: check malloc debug LD_PRELOAD before using, 2024-11-11), we probe the system by running "git version". Since this code runs before we've set $PATH to point to the version of Git we intend to test, we actually run the system version of git. This mostly works, since what we really care about is whether the LD_PRELOAD works, and it should work the same with any program. But there are some corner cases: 1. You might not have a system git at all, in which case the preload will appear to fail, even though it could work with the actual built version of git. 2. Your system git could be linked in a different way. For example, if it was built statically, then it will ignore LD_PRELOAD entirely, and we might assume that the preload works, even though it might not when used with a dynamic build. We could give a more complete path to the version of Git we intend to test, but features like GIT_TEST_INSTALLED make that not entirely trivial. So instead, let's just bump the setup until after we've set up the $PATH. There's no need for us to do it early, as long as it is done before the first test runs. Reported-by: Toon Claes <toon@iotcl.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 13, 2024 at 20:39 UTC 72ad6dc368892803c761fe3d82b98eb2555e4fbc
1 file changed +50 -50
t/test-lib.sh
+50 -50
@@ -577,56 +577,6 @@ case $GIT_TEST_FSYNC in
577 ;;
578 esac
579
580 -# Add libc MALLOC and MALLOC_PERTURB test only if we are not executing
581 -# the test with valgrind and have not compiled with conflict SANITIZE
582 -# options.
583 -if test -n "$valgrind" ||
584 - test -n "$SANITIZE_ADDRESS" ||
585 - test -n "$SANITIZE_LEAK" ||
586 - test -n "$TEST_NO_MALLOC_CHECK"
587 -then
588 - setup_malloc_check () {
589 - : nothing
590 - }
591 - teardown_malloc_check () {
592 - : nothing
593 - }
594 -else
595 - _USE_GLIBC_TUNABLES=
596 - _USE_GLIBC_PRELOAD=libc_malloc_debug.so.0
597 - if _GLIBC_VERSION=$(getconf GNU_LIBC_VERSION 2>/dev/null) &&
598 - _GLIBC_VERSION=${_GLIBC_VERSION#"glibc "} &&
599 - expr 2.34 \<= "$_GLIBC_VERSION" >/dev/null &&
600 - stderr=$(LD_PRELOAD=$_USE_GLIBC_PRELOAD git version 2>&1 >/dev/null) &&
601 - test -z "$stderr"
602 - then
603 - _USE_GLIBC_TUNABLES=YesPlease
604 - fi
605 - setup_malloc_check () {
606 - local g
607 - local t
608 - MALLOC_CHECK_=3 MALLOC_PERTURB_=165
609 - export MALLOC_CHECK_ MALLOC_PERTURB_
610 - if test -n "$_USE_GLIBC_TUNABLES"
611 - then
612 - g=
613 - LD_PRELOAD=$_USE_GLIBC_PRELOAD
614 - for t in \
615 - glibc.malloc.check=1 \
616 - glibc.malloc.perturb=165
617 - do
618 - g="${g#:}:$t"
619 - done
620 - GLIBC_TUNABLES=$g
621 - export LD_PRELOAD GLIBC_TUNABLES
622 - fi
623 - }
624 - teardown_malloc_check () {
625 - unset MALLOC_CHECK_ MALLOC_PERTURB_
626 - unset LD_PRELOAD GLIBC_TUNABLES
627 - }
628 -fi
629 -
580 # Protect ourselves from common misconfiguration to export
581 # CDPATH into the environment
582 unset CDPATH
@@ -1486,6 +1436,56 @@ GIT_ATTR_NOSYSTEM=1
1436 GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/.."
1437 export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM GIT_CEILING_DIRECTORIES
1438
1439 +# Add libc MALLOC and MALLOC_PERTURB test only if we are not executing
1440 +# the test with valgrind and have not compiled with conflict SANITIZE
1441 +# options.
1442 +if test -n "$valgrind" ||
1443 + test -n "$SANITIZE_ADDRESS" ||
1444 + test -n "$SANITIZE_LEAK" ||
1445 + test -n "$TEST_NO_MALLOC_CHECK"
1446 +then
1447 + setup_malloc_check () {
1448 + : nothing
1449 + }
1450 + teardown_malloc_check () {
1451 + : nothing
1452 + }
1453 +else
1454 + _USE_GLIBC_TUNABLES=
1455 + _USE_GLIBC_PRELOAD=libc_malloc_debug.so.0
1456 + if _GLIBC_VERSION=$(getconf GNU_LIBC_VERSION 2>/dev/null) &&
1457 + _GLIBC_VERSION=${_GLIBC_VERSION#"glibc "} &&
1458 + expr 2.34 \<= "$_GLIBC_VERSION" >/dev/null &&
1459 + stderr=$(LD_PRELOAD=$_USE_GLIBC_PRELOAD git version 2>&1 >/dev/null) &&
1460 + test -z "$stderr"
1461 + then
1462 + _USE_GLIBC_TUNABLES=YesPlease
1463 + fi
1464 + setup_malloc_check () {
1465 + local g
1466 + local t
1467 + MALLOC_CHECK_=3 MALLOC_PERTURB_=165
1468 + export MALLOC_CHECK_ MALLOC_PERTURB_
1469 + if test -n "$_USE_GLIBC_TUNABLES"
1470 + then
1471 + g=
1472 + LD_PRELOAD=$_USE_GLIBC_PRELOAD
1473 + for t in \
1474 + glibc.malloc.check=1 \
1475 + glibc.malloc.perturb=165
1476 + do
1477 + g="${g#:}:$t"
1478 + done
1479 + GLIBC_TUNABLES=$g
1480 + export LD_PRELOAD GLIBC_TUNABLES
1481 + fi
1482 + }
1483 + teardown_malloc_check () {
1484 + unset MALLOC_CHECK_ MALLOC_PERTURB_
1485 + unset LD_PRELOAD GLIBC_TUNABLES
1486 + }
1487 +fi
1488 +
1489 if test -z "$GIT_TEST_CMP"
1490 then
1491 if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"