[PATCH v6 2/3] perf tests: avoid storing an absolute path in perf binary
From: Denys Zagorui
Date: Fri May 21 2021 - 13:27:57 EST
python binding test uses PYTHONPATH definition to find python/perf.so
library. This definition is an absolute path that makes perf binary
unreproducible. This path can be found during runtime execution.
Signed-off-by: Denys Zagorui <dzagorui@xxxxxxxxx>
---
tools/perf/tests/Build | 2 +-
tools/perf/tests/python-use.c | 21 +++++++++++++++++++--
tools/perf/util/util.c | 12 ++++++++++++
tools/perf/util/util.h | 1 +
4 files changed, 33 insertions(+), 3 deletions(-)
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 650aec19d490..a20098dcdbc4 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -98,5 +98,5 @@ perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
endif
CFLAGS_attr.o += -DBINDIR="BUILD_STR($(bindir_SQ))" -DPYTHON="BUILD_STR($(PYTHON_WORD))"
-CFLAGS_python-use.o += -DPYTHONPATH="BUILD_STR($(OUTPUT)python)" -DPYTHON="BUILD_STR($(PYTHON_WORD))"
+CFLAGS_python-use.o += -DPYTHON="BUILD_STR($(PYTHON_WORD))"
CFLAGS_dwarf-unwind.o += -fno-optimize-sibling-calls
diff --git a/tools/perf/tests/python-use.c b/tools/perf/tests/python-use.c
index 98c6d474aa6f..4a6ff7db2bf1 100644
--- a/tools/perf/tests/python-use.c
+++ b/tools/perf/tests/python-use.c
@@ -8,18 +8,35 @@
#include <linux/compiler.h>
#include "tests.h"
#include "util/debug.h"
+#include "util/util.h"
+#include <sys/stat.h>
int test__python_use(struct test *test __maybe_unused, int subtest __maybe_unused)
{
char *cmd;
int ret;
+ char *exec_path;
+ char *pythonpath;
+ struct stat sb;
- if (asprintf(&cmd, "echo \"import sys ; sys.path.append('%s'); import perf\" | %s %s",
- PYTHONPATH, PYTHON, verbose > 0 ? "" : "2> /dev/null") < 0)
+ exec_path = perf_exe_path();
+
+ if (asprintf(&pythonpath, "%s/python", exec_path) < 0)
return -1;
+ if (stat(pythonpath, &sb) || !S_ISDIR(sb.st_mode))
+ pythonpath[0] = 0;
+
+ if (asprintf(&cmd, "echo \"import sys ; sys.path.append('%s'); import perf\" | %s %s",
+ pythonpath, PYTHON, verbose > 0 ? "" : "2> /dev/null") < 0) {
+ ret = -1;
+ goto out;
+ }
+
pr_debug("python usage test: \"%s\"\n", cmd);
ret = system(cmd) ? -1 : 0;
free(cmd);
+out:
+ free(pythonpath);
return ret;
}
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 3bba74e431ed..53f3dbf02f58 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -22,6 +22,7 @@
#include "cap.h"
#include "strlist.h"
#include "string2.h"
+#include <libgen.h>
/*
* XXX We need to find a better place for these things...
@@ -388,3 +389,14 @@ char *perf_exe(char *buf, int len)
}
return strcpy(buf, "perf");
}
+
+char *perf_exe_path(void)
+{
+ char buf[PATH_MAX];
+ char *dname;
+
+ perf_exe(buf, PATH_MAX);
+ dname = dirname(buf);
+
+ return dname;
+}
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 80b194ee6c7d..4b506df4e8b9 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -49,6 +49,7 @@ void perf_set_singlethreaded(void);
void perf_set_multithreaded(void);
char *perf_exe(char *buf, int len);
+char *perf_exe_path(void);
#ifndef O_CLOEXEC
#ifdef __sparc__
--
2.26.2.Cisco