git: 5184b149d8e9 - main - devel/google-perftools: Add a more reliable way to detect that the process is run under Valgrind

From: Yuri Victorovich <yuri_at_FreeBSD.org>
Date: Sun, 21 Nov 2021 09:40:09 UTC
The branch main has been updated by yuri:

URL: https://cgit.FreeBSD.org/ports/commit/?id=5184b149d8e9b847715bb2fb787527c52616de24

commit 5184b149d8e9b847715bb2fb787527c52616de24
Author:     Yuri Victorovich <yuri@FreeBSD.org>
AuthorDate: 2021-11-21 09:37:41 +0000
Commit:     Yuri Victorovich <yuri@FreeBSD.org>
CommitDate: 2021-11-21 09:40:05 +0000

    devel/google-perftools: Add a more reliable way to detect that the process is run under Valgrind
---
 devel/google-perftools/Makefile                    |  2 +-
 .../files/patch-src_base_dynamic__annotations.c    | 37 ++++++++++++++++++++++
 2 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/devel/google-perftools/Makefile b/devel/google-perftools/Makefile
index 0c0ebbb68dba..f8b0d79c6900 100644
--- a/devel/google-perftools/Makefile
+++ b/devel/google-perftools/Makefile
@@ -3,7 +3,7 @@
 PORTNAME=	google-perftools
 DISTVERSIONPREFIX=	gperftools-
 DISTVERSION=	2.9.1
-PORTREVISION=	1
+PORTREVISION=	2
 CATEGORIES=	devel
 MASTER_SITES=	https://github.com/gperftools/gperftools/releases/download/gperftools-${PORTVERSION}/
 DISTNAME=	gperftools-${PORTVERSION}
diff --git a/devel/google-perftools/files/patch-src_base_dynamic__annotations.c b/devel/google-perftools/files/patch-src_base_dynamic__annotations.c
new file mode 100644
index 000000000000..e02b1a5f5afe
--- /dev/null
+++ b/devel/google-perftools/files/patch-src_base_dynamic__annotations.c
@@ -0,0 +1,37 @@
+- add another way to determine if the process is run under valgrind - based on LD_PRELOAD patterns presence
+- Submitted: https://github.com/gperftools/gperftools/pull/1316
+
+--- src/base/dynamic_annotations.c.orig	2021-02-15 06:44:21 UTC
++++ src/base/dynamic_annotations.c
+@@ -43,6 +43,19 @@
+ #include "base/dynamic_annotations.h"
+ #include "getenv_safe.h" // for TCMallocGetenvSafe
+ 
++static int running_on_valgrind_preload = -1;
++void __attribute__ ((constructor)) premain() {
++  char *LD_PRELOAD = getenv("LD_PRELOAD");
++  running_on_valgrind_preload = LD_PRELOAD != NULL &&
++    (
++      strstr(LD_PRELOAD, "/valgrind/") != NULL
++      ||
++      strstr(LD_PRELOAD, "/vgpreload") != NULL
++    )
++    ?
++    1 : 0;
++}
++
+ static int GetRunningOnValgrind(void) {
+ #ifdef RUNNING_ON_VALGRIND
+   if (RUNNING_ON_VALGRIND) return 1;
+@@ -51,6 +64,11 @@ static int GetRunningOnValgrind(void) {
+   if (running_on_valgrind_str) {
+     return strcmp(running_on_valgrind_str, "0") != 0;
+   }
++
++  // use the LD_PRELOAD trick from https://stackoverflow.com/questions/365458/how-can-i-detect-if-a-program-is-running-from-within-valgrind
++  if (running_on_valgrind_preload == 1)
++    return 1;
++
+   return 0;
+ }
+