test-lib.sh: Use GLIBC_TUNABLES instead of MALLOC_CHECK_ on glibc >= 2.34

In glibc >= 2.34 MALLOC_CHECK_ and MALLOC_PERTURB_ environment variables have been replaced by GLIBC_TUNABLES. Also the new glibc requires that you preload a library called libc_malloc_debug.so to get these features. Using the ordinary glibc system variable detect if this is glibc >= 2.34 and use GLIBC_TUNABLES and the new library. This patch was inspired by a Richard W.M. Jones ndbkit patch Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Elia Pinto <gitter.spiros@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elia Pinto committed Mar 4, 2022 at 13:37 UTC 131b94a10a77e40eb3b3ba276d07fb663e220cd1
1 file changed +16
t/test-lib.sh
+16
@@ -518,9 +518,25 @@ else
518 setup_malloc_check () {
519 MALLOC_CHECK_=3 MALLOC_PERTURB_=165
520 export MALLOC_CHECK_ MALLOC_PERTURB_
521 + if _GLIBC_VERSION=$(getconf GNU_LIBC_VERSION 2>/dev/null) &&
522 + _GLIBC_VERSION=${_GLIBC_VERSION#"glibc "} &&
523 + expr 2.34 \<= "$_GLIBC_VERSION" >/dev/null
524 + then
525 + g=
526 + LD_PRELOAD="libc_malloc_debug.so.0"
527 + for t in \
528 + glibc.malloc.check=1 \
529 + glibc.malloc.perturb=165
530 + do
531 + g="${g#:}:$t"
532 + done
533 + GLIBC_TUNABLES=$g
534 + export LD_PRELOAD GLIBC_TUNABLES
535 + fi
536 }
537 teardown_malloc_check () {
538 unset MALLOC_CHECK_ MALLOC_PERTURB_
539 + unset LD_PRELOAD GLIBC_TUNABLES
540 }
541 fi
542