Skip to contentSkip to conversation

lib: adding tst_on_arch function in library

RFC PATCH New Li Wang 2 participants 6 messages Jun 8, 2019 – Jun 14, 2019 Library code

lore
Share & tools
lore

Li Wang

author
#0 lore raw

RFC PATCH 1/3 lib: adding tst_on_arch function in library

Testcases for specified arch should be limited on that only being supported platform to run, we now create a function tst_on_arch to achieve this new feature in LTP library. All you need to run a test on the expected arch is to set the '.arch' string in the 'struct tst_test' to choose the required arch list. e.g. '.arch = "x86_64, i386"'.

For more complicated usages such as customize your test code for a special arch, the tst_on_arch function could be invoked in the test directly.

---
 doc/test-writing-guidelines.txt | 54 +++++++++++++++++++++++++
 include/tst_arch.h              | 16 ++++++++
 include/tst_test.h              |  7 +++-
 lib/tst_arch.c                  | 71 +++++++++++++++++++++++++++++++++
 4 files changed, 147 insertions(+), 1 deletion(-)
 create mode 100644 include/tst_arch.h
 create mode 100644 lib/tst_arch.c
doc/test-writing-guidelines.txt +54 -0
index f1912dc12..10442d756 100644
@@ -1668,6 +1668,60 @@ sturct tst_test test = {
 };
 -------------------------------------------------------------------------------
 
+2.2.30 Testing on specified architecture
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Testcases for specified arch should be limited on that only being supported
+platform to run, we now create a function tst_on_arch to achieve this new
+feature in LTP library.  All you need to run a test on the expected arch is
+to set the '.arch' string in the 'struct tst_test' to choose the required
+arch list. e.g. '.arch = "x86_64, i386"'.
+
+[source,c]
+-------------------------------------------------------------------------------
+#include "tst_test.h"
+
+static void setup(void)
+{
+	...
+}
+
+static struct tst_test test = {
+	...
+	.setup = setup,
+	.arch = "x86_64, i386",
+	...
+}
+-------------------------------------------------------------------------------
+
+For more complicated usages such as customize your test code for a special
+arch, the tst_on_arch function could be invoked in the test directly.
+
+[source,c]
+-------------------------------------------------------------------------------
+#include "tst_test.h"
+
+static void do_test(void)
+{
+	if (tst_on_arch("x86_64, i386")) {
+		/* code for x86_64, i386 */
+		...
+	} else if (tst_on_arch("ppc64")) {
+		/* code for ppc64 */
+		...
+	} else if (tst_on_arch("s390, s390x")) {
+		/* code for s390x */
+		...
+	}
+}
+
+static struct tst_test test = {
+	...
+	.test_all = do_test,
+	...
+}
+-------------------------------------------------------------------------------
+
 
 2.3 Writing a testcase in shell
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/tst_arch.h new file +16 -0
new file mode 100644
index 000000000..7bf0493ce
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright (c) 2019 Li Wang <[email protected]>
+ */
+
+#ifndef TST_ARCH_H__
+#define TST_ARCH_H__
+
+/*
+ * Check if test platform is in the given arch list. If yes return 1,
+ * otherwise return 0.
+ *
+ * @arch, NULL or vliad arch list
+ */
+int tst_on_arch(const char *arch);
+
+#endif /* TST_ARCH_H__ */
include/tst_test.h +6 -1
index 8bdf38482..cafcb1a89 100644
@@ -28,6 +28,7 @@
 #include "tst_atomic.h"
 #include "tst_kvercmp.h"
 #include "tst_clone.h"
+#include "tst_arch.h"
 #include "tst_kernel.h"
 #include "tst_minmax.h"
 #include "tst_get_bad_addr.h"
@@ -114,6 +115,8 @@ struct tst_test {
 
 	const char *min_kver;
 
+	const char *arch;
+
 	/* If set the test is compiled out */
 	const char *tconf_msg;
 
@@ -253,7 +256,6 @@ const char *tst_strstatus(int status);
 unsigned int tst_timeout_remaining(void);
 void tst_set_timeout(int timeout);
 
-
 /*
  * Returns path to the test temporary directory in a newly allocated buffer.
  */
@@ -265,6 +267,9 @@ static struct tst_test test;
 
 int main(int argc, char *argv[])
 {
+	if (!tst_on_arch(test.arch))
+		tst_brk(TCONF, "Test needs running on %s arch!", test.arch);
+
 	tst_run_tcases(argc, argv, &test);
 }
 
lib/tst_arch.c new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.17.0
author
#1 lore raw

RFC PATCH 2/3 max_map_count: taking use of tst_on_arch in testcase

We have two different ways to use tst_on_arch, first is to set .arch = "xxx, yyy" in test struct to limit testcase running on some specified arch, the second is calling it directly in test functions.

Demo:
1. ptrace07.c, cve-2017-17053.c, meltdown.c
2. max_map_count.c

---
 testcases/cve/cve-2017-17053.c               |  1 +
 testcases/cve/meltdown.c                     |  9 +----
 testcases/kernel/mem/tunable/max_map_count.c | 36 ++++++++++----------
 testcases/kernel/syscalls/ptrace/ptrace07.c  |  8 ++---
 4 files changed, 23 insertions(+), 31 deletions(-)
testcases/cve/cve-2017-17053.c +1 -0
index e01db3d4f..67bf4135f 100644
@@ -162,6 +162,7 @@ void run(void)
 }
 
 static struct tst_test test = {
+	.arch = "x86_64, i386",
 	.forks_child = 1,
 	.setup = setup,
 	.cleanup = cleanup,
testcases/cve/meltdown.c +1 -8
index a53ea9b8e..da35213ec 100644
@@ -20,8 +20,6 @@
 #include "config.h"
 #include "tst_test.h"
 
-#if defined(__x86_64__) || defined(__i386__)
-
 #include <stdio.h>
 #include <string.h>
 #include <signal.h>
@@ -382,15 +380,10 @@ static void cleanup(void)
 }
 
 static struct tst_test test = {
+	.arch = "x86_64, i386",
 	.needs_root = 1,
 	.setup = setup,
 	.test_all = run,
 	.cleanup = cleanup,
 	.min_kver = "2.6.32"
 };
-
-#else /* #if defined(__x86_64__) || defined(__i386__) */
-
-TST_TEST_TCONF("not x86_64 or i386");
-
-#endif /* #else #if defined(__x86_64__) || defined(__i386__) */
testcases/kernel/mem/tunable/max_map_count.c +18 -18
index 5b03a60ec..ae86288bf 100644
@@ -91,24 +91,24 @@ static bool filter_map(const char *line)
 	if (ret != 1)
 		return false;
 
-#if defined(__x86_64__) || defined(__x86__)
-	/* On x86, there's an old compat vsyscall page */
-	if (!strcmp(buf, "[vsyscall]"))
-		return true;
-#elif defined(__ia64__)
-	/* On ia64, the vdso is not a proper mapping */
-	if (!strcmp(buf, "[vdso]"))
-		return true;
-#elif defined(__arm__)
-	/* Skip it when run it in aarch64 */
-	if ((!strcmp(un.machine, "aarch64"))
-	|| (!strcmp(un.machine, "aarch64_be")))
-		return false;
-
-	/* Older arm kernels didn't label their vdso maps */
-	if (!strncmp(line, "ffff0000-ffff1000", 17))
-		return true;
-#endif
+	if (tst_on_arch("x86_64, x86")) {
+		/* On x86, there's an old compat vsyscall page */
+		if (!strcmp(buf, "[vsyscall]"))
+			return true;
+	} else if (tst_on_arch("ia64")) {
+		/* On ia64, the vdso is not a proper mapping */
+		if (!strcmp(buf, "[vdso]"))
+			return true;
+	} else if (tst_on_arch("arm")) {
+		/* Skip it when run it in aarch64 */
+		if ((!strcmp(un.machine, "aarch64"))
+				|| (!strcmp(un.machine, "aarch64_be")))
+			return false;
+
+		/* Older arm kernels didn't label their vdso maps */
+		if (!strncmp(line, "ffff0000-ffff1000", 17))
+			return true;
+	}
 
 	return false;
 }
testcases/kernel/syscalls/ptrace/ptrace07.c +3 -5
index 9cbaefc3f..67e47ce16 100644
@@ -60,13 +60,13 @@
 # define NT_X86_XSTATE 0x202
 #endif
 
-#ifdef __x86_64__
 static void check_regs_loop(uint32_t initval)
 {
 	const unsigned long num_iters = 1000000000;
 	uint32_t xmm0[4] = { initval, initval, initval, initval };
 	int status = 1;
 
+#ifdef __x86_64__
 	asm volatile("   movdqu %0, %%xmm0\n"
 		     "   mov %0, %%rbx\n"
 		     "1: dec %2\n"
@@ -80,6 +80,7 @@ static void check_regs_loop(uint32_t initval)
 		     "3:\n"
 		     : "+m" (xmm0), "+r" (status)
 		     : "r" (num_iters) : "rax", "rbx", "xmm0");
+#endif
 
 	if (status) {
 		tst_res(TFAIL,
@@ -188,10 +189,7 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
+	.arch = "x86_64",
 	.forks_child = 1,
 	.needs_checkpoints = 1,
 };
-
-#else /* !__x86_64__ */
-	TST_TEST_TCONF("this test is only supported on x86_64");
-#endif /* __x86_64__ */
Signature · 1 line
2.17.0
author
#2 lore raw

RFC PATCH 3/3 testcase: get rid of compiling errors

In reply to Li Wang
---
 configure.ac             | 1 +
 testcases/cve/meltdown.c | 5 +++++
 2 files changed, 6 insertions(+)
configure.ac +1 -0
index 5a3dc5b62..39b2d6da7 100644
@@ -58,6 +58,7 @@ AC_CHECK_HEADERS([ \
     sys/shm.h \
     sys/ustat.h \
     sys/xattr.h \
+    emmintrin.h \
 ])
 
 AC_CHECK_FUNCS([ \
testcases/cve/meltdown.c +5 -0
index da35213ec..e6e911fcc 100644
@@ -29,6 +29,7 @@
 #include <ctype.h>
 #include <sys/utsname.h>
 
+#ifdef HAVE_EMMINTRIN_H
 #include <emmintrin.h>
 
 #include "libtsc.h"
@@ -387,3 +388,7 @@ static struct tst_test test = {
 	.cleanup = cleanup,
 	.min_kver = "2.6.32"
 };
+
+#else /* HAVE_EMMINTRIN_H */
+	TST_TEST_TCONF("<emmintrin.h> is not supported");
+#endif
Signature · 1 line
2.17.0
#3 lore raw

RFC PATCH 1/3 lib: adding tst_on_arch function in library

In reply to Li Wang

Hi!

Quoted 5 lines

Testcases for specified arch should be limited on that only being supported platform to run, we now create a function tst_on_arch to achieve this new feature in LTP library. All you need to run a test on the expected arch is to set the '.arch' string in the 'struct tst_test' to choose the required arch list. e.g. '.arch = "x86_64, i386"'.

There is no point in adding the coma between the architectures, i.e. this should be just .arch = "x86_64 i386".

Quoted 56 lines

For more complicated usages such as customize your test code for a special arch, the tst_on_arch function could be invoked in the test directly.

---
 doc/test-writing-guidelines.txt | 54 +++++++++++++++++++++++++
 include/tst_arch.h              | 16 ++++++++
 include/tst_test.h              |  7 +++-
 lib/tst_arch.c                  | 71 +++++++++++++++++++++++++++++++++
 4 files changed, 147 insertions(+), 1 deletion(-)
 create mode 100644 include/tst_arch.h
 create mode 100644 lib/tst_arch.c
doc/test-writing-guidelines.txt +35 -0
index f1912dc12..10442d756 100644
@@ -1668,6 +1668,60 @@ sturct tst_test test = {
 };
 -------------------------------------------------------------------------------
 
+2.2.30 Testing on specified architecture
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Testcases for specified arch should be limited on that only being supported
+platform to run, we now create a function tst_on_arch to achieve this new
+feature in LTP library.  All you need to run a test on the expected arch is
+to set the '.arch' string in the 'struct tst_test' to choose the required
+arch list. e.g. '.arch = "x86_64, i386"'.
+
+[source,c]
+-------------------------------------------------------------------------------
+#include "tst_test.h"
+
+static void setup(void)
+{
+	...
+}
+
+static struct tst_test test = {
+	...
+	.setup = setup,
+	.arch = "x86_64, i386",
+	...
+}
+-------------------------------------------------------------------------------
+
+For more complicated usages such as customize your test code for a special
+arch, the tst_on_arch function could be invoked in the test directly.
+
+[source,c]
+-------------------------------------------------------------------------------
+#include "tst_test.h"
+
+static void do_test(void)
+{

doc/test-writing-guidelines.txt sturct tst_test test = { · line 1715

Quoted 10 lines
doc/test-writing-guidelines.txt +10 -0
+	if (tst_on_arch("x86_64, i386")) {
+		/* code for x86_64, i386 */
+		...
+	} else if (tst_on_arch("ppc64")) {
+		/* code for ppc64 */
+		...
+	} else if (tst_on_arch("s390, s390x")) {
+		/* code for s390x */
+		...
+	}

What is the point of the runtime detection here?

It's not like we can run s390x binary on i386, i.e. we know for which architecture we are compiling for at the compile time.

Quoted 64 lines
doc/test-writing-guidelines.txt +9 -0
+}
+
+static struct tst_test test = {
+	...
+	.test_all = do_test,
+	...
+}
+-------------------------------------------------------------------------------
+
 
 2.3 Writing a testcase in shell
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/tst_arch.h new file +16 -0
new file mode 100644
index 000000000..7bf0493ce
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright (c) 2019 Li Wang <[email protected]>
+ */
+
+#ifndef TST_ARCH_H__
+#define TST_ARCH_H__
+
+/*
+ * Check if test platform is in the given arch list. If yes return 1,
+ * otherwise return 0.
+ *
+ * @arch, NULL or vliad arch list
+ */
+int tst_on_arch(const char *arch);
+
+#endif /* TST_ARCH_H__ */
include/tst_test.h +3 -1
index 8bdf38482..cafcb1a89 100644
@@ -28,6 +28,7 @@
 #include "tst_atomic.h"
 #include "tst_kvercmp.h"
 #include "tst_clone.h"
+#include "tst_arch.h"
 #include "tst_kernel.h"
 #include "tst_minmax.h"
 #include "tst_get_bad_addr.h"
@@ -114,6 +115,8 @@ struct tst_test {
 
 	const char *min_kver;
 
+	const char *arch;
+
 	/* If set the test is compiled out */
 	const char *tconf_msg;
 
@@ -253,7 +256,6 @@ const char *tst_strstatus(int status);
 unsigned int tst_timeout_remaining(void);
 void tst_set_timeout(int timeout);
 
-
 /*
  * Returns path to the test temporary directory in a newly allocated buffer.
  */

include/tst_test.h static struct tst_test test; · line 274

Quoted 8 lines
include/tst_test.h +3 -0
@@ -265,6 +267,9 @@ static struct tst_test test;
 
 int main(int argc, char *argv[])
 {
+	if (!tst_on_arch(test.arch))
+		tst_brk(TCONF, "Test needs running on %s arch!", test.arch);
+
 	tst_run_tcases(argc, argv, &test);
 }

This may be a bit cleaner that compiling the test out, but will not save us from arch specific ifdefs completely so I'm not sure it's worth the trouble.

Quoted 64 lines
lib/tst_arch.c new file +58 -0
new file mode 100644
index 000000000..a9f2775b4
@@ -0,0 +1,71 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright (c) 2019 Li Wang <[email protected]>
+ */
+
+#include <string.h>
+
+#define TST_NO_DEFAULT_MAIN
+#include "tst_arch.h"
+#include "tst_test.h"
+
+static const char *const arch_type_list[] = {
+	"i386",
+	"x86",
+	"x86_64",
+	"ia64",
+	"ppc",
+	"ppc64",
+	"s390",
+	"s390x",
+	"arm",
+	"aarch64",
+	"sparc",
+	NULL
+};
+
+static int is_valid_arch(const char *arch)
+{
+	unsigned int i;
+
+	for (i = 0; arch_type_list[i]; i++) {
+		if (strstr(arch, arch_type_list[i]))
+			return 1;
+	}
+
+	return 0;
+}
+
+int tst_on_arch(const char *arch)
+{
+#if defined(__i386__)
+	char *tst_arch = "i386";
+#elif defined(__x86__)
+	char *tst_arch = "x86";
+#elif defined(__x86_64__)
+	char *tst_arch = "x86_64";
+#elif defined(__ia64__)
+	char *tst_arch = "ia64";
+#elif defined(__powerpc__)
+	char *tst_arch = "ppc";
+#elif defined(__powerpc64__)
+	char *tst_arch = "ppc64";
+#elif defined(__s390__)
+	char *tst_arch = "s390";
+#elif defined(__s390x__)
+	char *tst_arch = "s390x";
+#elif defined(__arm__)
+	char *tst_arch = "arm";
+#elif defined(__arch64__)

lib/tst_arch.c line 68

Quoted 10 lines
lib/tst_arch.c new file +10 -0
+	char *tst_arch = "aarch64";
+#elif defined(__sparc__)
+	char *tst_arch = "sparc";
+#endif
+
+	if (arch != NULL && !is_valid_arch(arch))
+		tst_brk(TBROK, "please set valid arches!");
+
+	if (arch == NULL || strstr(arch, tst_arch))
+		return 1;

Isn't using strstr() completely broken here?

Couple of the architecture names are prefixes of the 64bit variant, also validating the architecture by strstr() is kind of pointless, it will match any garbage that contains one of the substrings.

If nothing else we should strdup() the string and then loop over strtok().

Quoted 8 lines
lib/tst_arch.c new file +2 -0
+	return 0;
+}
Signature · 5 lines
2.17.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#4 lore raw

RFC PATCH 1/3 lib: adding tst_on_arch function in library

Quoted 12 lines · Cyril Hrubis

Hi!

Quoted 1 line

Testcases for specified arch should be limited on that only being

supported

Quoted 2 lines

platform to run, we now create a function tst_on_arch to achieve this new feature in LTP library. All you need to run a test on the expected arch

is

Quoted 2 lines

to set the '.arch' string in the 'struct tst_test' to choose the required arch list. e.g. '.arch = "x86_64, i386"'.

There is no point in adding the coma between the architectures, i.e. this should be just .arch = "x86_64 i386".

Agree.

Quoted 81 lines
Quoted 2 lines

For more complicated usages such as customize your test code for a

special

Quoted 12 lines

arch, the tst_on_arch function could be invoked in the test directly.

---
 doc/test-writing-guidelines.txt | 54 +++++++++++++++++++++++++
 include/tst_arch.h              | 16 ++++++++
 include/tst_test.h              |  7 +++-
 lib/tst_arch.c                  | 71 +++++++++++++++++++++++++++++++++
 4 files changed, 147 insertions(+), 1 deletion(-)
 create mode 100644 include/tst_arch.h
 create mode 100644 lib/tst_arch.c

Review

Quoted 1 line
(quoted diff) +0 -0

b/doc/test-writing-guidelines.txt

doc/test-writing-guidelines.txt sturct tst_test test = { · line 1669

Quoted 6 lines

index f1912dc12..10442d756 100644

doc/test-writing-guidelines.txt +0 -0
@@ -1668,6 +1668,60 @@ sturct tst_test test = {
 };
 

-------------------------------------------------------------------------------

doc/test-writing-guidelines.txt sturct tst_test test = {

Quoted 5 lines
doc/test-writing-guidelines.txt +4 -0
+2.2.30 Testing on specified architecture
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Testcases for specified arch should be limited on that only being

supported

doc/test-writing-guidelines.txt sturct tst_test test = { · line 1674

Quoted 1 line
doc/test-writing-guidelines.txt +1 -0
+platform to run, we now create a function tst_on_arch to achieve this

new

doc/test-writing-guidelines.txt sturct tst_test test = { · line 1675

Quoted 1 line
doc/test-writing-guidelines.txt +1 -0
+feature in LTP library.  All you need to run a test on the expected

arch is

doc/test-writing-guidelines.txt sturct tst_test test = { · line 1676

Quoted 1 line
doc/test-writing-guidelines.txt +1 -0
+to set the '.arch' string in the 'struct tst_test' to choose the

required

doc/test-writing-guidelines.txt sturct tst_test test = { · line 1725

Quoted 4 lines
doc/test-writing-guidelines.txt +3 -0
+arch list. e.g. '.arch = "x86_64, i386"'.
+
+[source,c]
 

+-------------------------------------------------------------------------------

Quoted 4 lines
doc/test-writing-guidelines.txt +4 -0
+#include "tst_test.h"
+
+static void setup(void)
+{

doc/test-writing-guidelines.txt sturct tst_test test = { · line 1725

Quoted 10 lines
doc/test-writing-guidelines.txt +9 -0
+     ...
+}
+
+static struct tst_test test = {
+     ...
+     .setup = setup,
+     .arch = "x86_64, i386",
+     ...
+}
 

+-------------------------------------------------------------------------------

doc/test-writing-guidelines.txt sturct tst_test test = { · line 1696

Quoted 2 lines
doc/test-writing-guidelines.txt +2 -0
+
+For more complicated usages such as customize your test code for a

special

doc/test-writing-guidelines.txt sturct tst_test test = { · line 1725

Quoted 4 lines
doc/test-writing-guidelines.txt +3 -0
+arch, the tst_on_arch function could be invoked in the test directly.
+
+[source,c]
 

+-------------------------------------------------------------------------------

Quoted 4 lines
doc/test-writing-guidelines.txt +4 -0
+#include "tst_test.h"
+
+static void do_test(void)
+{

doc/test-writing-guidelines.txt sturct tst_test test = { · line 1715

Quoted 10 lines
doc/test-writing-guidelines.txt +10 -0
+     if (tst_on_arch("x86_64, i386")) {
+             /* code for x86_64, i386 */
+             ...
+     } else if (tst_on_arch("ppc64")) {
+             /* code for ppc64 */
+             ...
+     } else if (tst_on_arch("s390, s390x")) {
+             /* code for s390x */
+             ...
+     }

What is the point of the runtime detection here?

Quoted 3 lines

It's not like we can run s390x binary on i386, i.e. we know for which architecture we are compiling for at the compile time.

You are right. But we still have some chance to do analysis at runtime, if you take a look at patch 2/3, e.g. to parse '/proc/<pid>/maps' in max_map_count.c can be done at runtime detection. That's what I thought we can export the tst_on_arch() as a global function.

Quoted 73 lines

doc/test-writing-guidelines.txt sturct tst_test test = { · line 1725

Quoted 9 lines
doc/test-writing-guidelines.txt +7 -0
+}
+
+static struct tst_test test = {
+     ...
+     .test_all = do_test,
+     ...
+}
 

+-------------------------------------------------------------------------------

Quoted 48 lines
doc/test-writing-guidelines.txt +1 -0
+
 
 2.3 Writing a testcase in shell
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/tst_arch.h new file +16 -0
new file mode 100644
index 000000000..7bf0493ce
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright (c) 2019 Li Wang <[email protected]>
+ */
+
+#ifndef TST_ARCH_H__
+#define TST_ARCH_H__
+
+/*
+ * Check if test platform is in the given arch list. If yes return 1,
+ * otherwise return 0.
+ *
+ * @arch, NULL or vliad arch list
+ */
+int tst_on_arch(const char *arch);
+
+#endif /* TST_ARCH_H__ */
include/tst_test.h +3 -0
index 8bdf38482..cafcb1a89 100644
@@ -28,6 +28,7 @@
 #include "tst_atomic.h"
 #include "tst_kvercmp.h"
 #include "tst_clone.h"
+#include "tst_arch.h"
 #include "tst_kernel.h"
 #include "tst_minmax.h"
 #include "tst_get_bad_addr.h"
@@ -114,6 +115,8 @@ struct tst_test {
 
      const char *min_kver;
 
+     const char *arch;
+
      /* If set the test is compiled out */
      const char *tconf_msg;
 

include/tst_test.h const char *tst_strstatus(int status); · line 260

Quoted 6 lines
include/tst_test.h +0 -1
@@ -253,7 +256,6 @@ const char *tst_strstatus(int status);
 unsigned int tst_timeout_remaining(void);
 void tst_set_timeout(int timeout);
 
-
 /*
  * Returns path to the test temporary directory in a newly allocated

buffer.

include/tst_test.h static struct tst_test test; · line 271

Quoted 7 lines
include/tst_test.h +2 -0
  */
@@ -265,6 +267,9 @@ static struct tst_test test;
 
 int main(int argc, char *argv[])
 {
+     if (!tst_on_arch(test.arch))
+             tst_brk(TCONF, "Test needs running on %s arch!",

test.arch);

Quoted 8 lines

include/tst_test.h static struct tst_test test; · line 274

Quoted 3 lines
include/tst_test.h +1 -0
+
      tst_run_tcases(argc, argv, &test);
 }

This may be a bit cleaner that compiling the test out, but will not save us from arch specific ifdefs completely so I'm not sure it's worth the trouble.

Indeed, I also realized that after signing off this patch, we can't replace ifdefs completely via a simple function, since it occurring in the compiling early phase. But anyway I roll out this for comments in case we could find an improved way to do better.

Quoted 78 lines
Quoted 65 lines
lib/tst_arch.c new file +58 -0
new file mode 100644
index 000000000..a9f2775b4
@@ -0,0 +1,71 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright (c) 2019 Li Wang <[email protected]>
+ */
+
+#include <string.h>
+
+#define TST_NO_DEFAULT_MAIN
+#include "tst_arch.h"
+#include "tst_test.h"
+
+static const char *const arch_type_list[] = {
+     "i386",
+     "x86",
+     "x86_64",
+     "ia64",
+     "ppc",
+     "ppc64",
+     "s390",
+     "s390x",
+     "arm",
+     "aarch64",
+     "sparc",
+     NULL
+};
+
+static int is_valid_arch(const char *arch)
+{
+     unsigned int i;
+
+     for (i = 0; arch_type_list[i]; i++) {
+             if (strstr(arch, arch_type_list[i]))
+                     return 1;
+     }
+
+     return 0;
+}
+
+int tst_on_arch(const char *arch)
+{
+#if defined(__i386__)
+     char *tst_arch = "i386";
+#elif defined(__x86__)
+     char *tst_arch = "x86";
+#elif defined(__x86_64__)
+     char *tst_arch = "x86_64";
+#elif defined(__ia64__)
+     char *tst_arch = "ia64";
+#elif defined(__powerpc__)
+     char *tst_arch = "ppc";
+#elif defined(__powerpc64__)
+     char *tst_arch = "ppc64";
+#elif defined(__s390__)
+     char *tst_arch = "s390";
+#elif defined(__s390x__)
+     char *tst_arch = "s390x";
+#elif defined(__arm__)
+     char *tst_arch = "arm";
+#elif defined(__arch64__)

lib/tst_arch.c line 68

Quoted 10 lines
lib/tst_arch.c new file +10 -0
+     char *tst_arch = "aarch64";
+#elif defined(__sparc__)
+     char *tst_arch = "sparc";
+#endif
+
+     if (arch != NULL && !is_valid_arch(arch))
+             tst_brk(TBROK, "please set valid arches!");
+
+     if (arch == NULL || strstr(arch, tst_arch))
+             return 1;

Isn't using strstr() completely broken here?

Quoted 4 lines

Couple of the architecture names are prefixes of the 64bit variant, also validating the architecture by strstr() is kind of pointless, it will match any garbage that contains one of the substrings.

Yes, that's true.

Quoted 3 lines

If nothing else we should strdup() the string and then loop over strtok().

Good suggestion!

Quoted 14 lines
Quoted 9 lines
lib/tst_arch.c new file +2 -1
+     return 0;
+}
--

2.17.0

--
Mailing list info: https://lists.linux.it/listinfo/ltp

--
Cyril Hrubis
[email protected]

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20190613/76c3022c/attachment-0001.html>
#5 lore raw

RFC PATCH 1/3 lib: adding tst_on_arch function in library

Hi!

Quoted 10 lines
Quoted 5 lines

What is the point of the runtime detection here?

It's not like we can run s390x binary on i386, i.e. we know for which architecture we are compiling for at the compile time.

You are right. But we still have some chance to do analysis at runtime, if you take a look at patch 2/3, e.g. to parse '/proc/<pid>/maps' in max_map_count.c can be done at runtime detection. That's what I thought we can export the tst_on_arch() as a global function.

Well that patch replaces the ifdefs with ifs. I don't think that it makes things singificantly better.

Quoted 81 lines
Quoted 72 lines

doc/test-writing-guidelines.txt sturct tst_test test = { · line 1722

Quoted 8 lines
doc/test-writing-guidelines.txt +7 -0
+}
+
+static struct tst_test test = {
+     ...
+     .test_all = do_test,
+     ...
+}

+-------------------------------------------------------------------------------

Quoted 48 lines
doc/test-writing-guidelines.txt +1 -0
+
 
 2.3 Writing a testcase in shell
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/tst_arch.h new file +16 -0
new file mode 100644
index 000000000..7bf0493ce
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright (c) 2019 Li Wang <[email protected]>
+ */
+
+#ifndef TST_ARCH_H__
+#define TST_ARCH_H__
+
+/*
+ * Check if test platform is in the given arch list. If yes return 1,
+ * otherwise return 0.
+ *
+ * @arch, NULL or vliad arch list
+ */
+int tst_on_arch(const char *arch);
+
+#endif /* TST_ARCH_H__ */
include/tst_test.h +3 -0
index 8bdf38482..cafcb1a89 100644
@@ -28,6 +28,7 @@
 #include "tst_atomic.h"
 #include "tst_kvercmp.h"
 #include "tst_clone.h"
+#include "tst_arch.h"
 #include "tst_kernel.h"
 #include "tst_minmax.h"
 #include "tst_get_bad_addr.h"
@@ -114,6 +115,8 @@ struct tst_test {
 
      const char *min_kver;
 
+     const char *arch;
+
      /* If set the test is compiled out */
      const char *tconf_msg;
 

include/tst_test.h const char *tst_strstatus(int status); · line 260

Quoted 6 lines
include/tst_test.h +0 -1
@@ -253,7 +256,6 @@ const char *tst_strstatus(int status);
 unsigned int tst_timeout_remaining(void);
 void tst_set_timeout(int timeout);
 
-
 /*
  * Returns path to the test temporary directory in a newly allocated

buffer.

include/tst_test.h static struct tst_test test; · line 271

Quoted 7 lines
include/tst_test.h +2 -0
  */
@@ -265,6 +267,9 @@ static struct tst_test test;
 
 int main(int argc, char *argv[])
 {
+     if (!tst_on_arch(test.arch))
+             tst_brk(TCONF, "Test needs running on %s arch!",

test.arch);

Quoted 8 lines

include/tst_test.h static struct tst_test test; · line 274

Quoted 3 lines
include/tst_test.h +1 -0
+
      tst_run_tcases(argc, argv, &test);
 }

This may be a bit cleaner that compiling the test out, but will not save us from arch specific ifdefs completely so I'm not sure it's worth the trouble.

Quoted 4 lines

Indeed, I also realized that after signing off this patch, we can't replace ifdefs completely via a simple function, since it occurring in the compiling early phase. But anyway I roll out this for comments in case we could find an improved way to do better.

Well, one thing I like is that the information about supported arch is actually part of the tst_test structure, which moves the information from a code to a metadata. The tst_on_arch() function does not seem to be useful to me.

Signature · 2 lines
Cyril Hrubis
[email protected]