Skip to contentSkip to conversation

ptrace07: Fix compilation when not on x86

PATCH In review Richard Palethorpe via ltp 5 participants 11 messages Oct 18, 2022 – Oct 20, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ptrace07: Fix compilation when not on x86

Strictly cpuid.h should not be included on non-x86, but this means each test author has to remember not to include it. Instead we can set a blank macro to allow compilation.

---

This time I created a PR to check if compilation succeeds:
https://github.com/linux-test-project/ltp/pull/984

 include/lapi/cpuid.h | 4 ++++
 1 file changed, 4 insertions(+)
include/lapi/cpuid.h +4 -0
index cd0567f92..20e977093 100644
@@ -16,11 +16,15 @@
  * have __cpuid_count().
  */
 #ifndef __cpuid_count
+#  if defined(__i386__) || defined(__x86_64__)
 #define __cpuid_count(level, count, a, b, c, d) ({			\
 	__asm__ __volatile__ ("cpuid\n\t"				\
 			      : "=a" (a), "=b" (b), "=c" (c), "=d" (d)	\
 			      : "0" (level), "2" (count));		\
 })
+#  else
+#define __cpuid_count(level, count, a, b, c, d)
+#  endif
 #endif
 
 #endif /* LAPI_CPUID_H__ */
Signature · 5 lines
2.36.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 2/2 ptrace07: Fix compilation by avoiding aligned_alloc

Use memalign instead because we live in the past.

---
 testcases/kernel/syscalls/ptrace/ptrace07.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/ptrace/ptrace07.c +2 -1
index 6bd2b1062..a60c2a49e 100644
@@ -35,6 +35,7 @@
 #include "config.h"
 #include "ptrace.h"
 #include "tst_test.h"
+#include "tst_safe_macros.h"
 #include "lapi/cpuid.h"
 
 #ifndef PTRACE_GETREGSET
@@ -95,7 +96,7 @@ static void do_test(void)
 	 * of the XSAVE/XRSTOR save area) required by enabled features in XCR0.
 	 */
 	__cpuid_count(CPUID_LEAF_XSTATE, ecx, eax, ebx, ecx, edx);
-	xstate = aligned_alloc(64, ebx);
+	xstate = SAFE_MEMALIGN(64, ebx);
 	struct iovec iov = { .iov_base = xstate, .iov_len = ebx };
 	int status;
 	bool okay;
Signature · 5 lines
2.36.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] [PATCH 2/2] ptrace07: Fix compilation by avoiding aligned_alloc

Hi Richard,

Quoted 21 lines · Richard Palethorpe

Use memalign instead because we live in the past.

---
 testcases/kernel/syscalls/ptrace/ptrace07.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/ptrace/ptrace07.c +1 -0
index 6bd2b1062..a60c2a49e 100644
@@ -35,6 +35,7 @@
 #include "config.h"
 #include "ptrace.h"
 #include "tst_test.h"
+#include "tst_safe_macros.h"
 #include "lapi/cpuid.h"
 
 #ifndef PTRACE_GETREGSET

testcases/kernel/syscalls/ptrace/ptrace07.c static void do_test(void) · line 102

Quoted 8 lines
testcases/kernel/syscalls/ptrace/ptrace07.c +1 -1
@@ -95,7 +96,7 @@ static void do_test(void)
 	 * of the XSAVE/XRSTOR save area) required by enabled features in XCR0.
 	 */
 	__cpuid_count(CPUID_LEAF_XSTATE, ecx, eax, ebx, ecx, edx);
-	xstate = aligned_alloc(64, ebx);
+	xstate = SAFE_MEMALIGN(64, ebx);
 	struct iovec iov = { .iov_base = xstate, .iov_len = ebx };
 	int status;
 	bool okay;

Yes, it's better for LTP compilation. Thanks for the patch!

I checked SAFE_MEMALIGN(), it will verify that the buffer is NULL or not. "

	rval = memalign(alignment, size);

	if (rval == NULL) {
		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
			"memalign() failed");
	}

"
So could you remove below unnecessary lines in ptrace07.c, thanks. "
- if (!xstate)
- tst_brk(TBROK, "aligned_alloc() failed for xstate buffer");
"

Thanks!
BR.

Quoted 3 lines
Signature · 1 line
2.36.1
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Re: [LTP] [PATCH 2/2] ptrace07: Fix compilation by avoiding aligned_alloc

Quoted 39 lines · Pengfei Xu

Hi Richard,

Review

Quoted 9 lines · Richard Palethorpe

Use memalign instead because we live in the past.

---
 testcases/kernel/syscalls/ptrace/ptrace07.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/testcases/kernel/syscalls/ptrace/ptrace07.c

testcases/kernel/syscalls/ptrace/ptrace07.c static void do_test(void) · line 96

Quoted 13 lines

index 6bd2b1062..a60c2a49e 100644

testcases/kernel/syscalls/ptrace/ptrace07.c +1 -0
@@ -35,6 +35,7 @@
 #include "config.h"
 #include "ptrace.h"
 #include "tst_test.h"
+#include "tst_safe_macros.h"
 #include "lapi/cpuid.h"
 
 #ifndef PTRACE_GETREGSET
@@ -95,7 +96,7 @@ static void do_test(void)
       * of the XSAVE/XRSTOR save area) required by enabled features in

XCR0.

testcases/kernel/syscalls/ptrace/ptrace07.c static void do_test(void) · line 102

Quoted 7 lines
testcases/kernel/syscalls/ptrace/ptrace07.c +1 -1
       */
      __cpuid_count(CPUID_LEAF_XSTATE, ecx, eax, ebx, ecx, edx);
-     xstate = aligned_alloc(64, ebx);
+     xstate = SAFE_MEMALIGN(64, ebx);
      struct iovec iov = { .iov_base = xstate, .iov_len = ebx };
      int status;
      bool okay;

Yes, it's better for LTP compilation. Thanks for the patch!

I checked SAFE_MEMALIGN(), it will verify that the buffer is NULL or not. "

Quoted 9 lines
        rval = memalign(alignment, size);

        if (rval == NULL) {
                tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
                        "memalign() failed");
        }

"
So could you remove below unnecessary lines in ptrace07.c, thanks.

I helped modify this and pushed it, thanks!

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Re: [LTP] [PATCH 2/2] ptrace07: Fix compilation by avoiding aligned_alloc

Hi Li Wang,

Quoted 51 lines · Li Wang
Quoted 39 lines · Pengfei Xu

Hi Richard,

Review

Quoted 9 lines · Richard Palethorpe

Use memalign instead because we live in the past.

---
 testcases/kernel/syscalls/ptrace/ptrace07.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/testcases/kernel/syscalls/ptrace/ptrace07.c

testcases/kernel/syscalls/ptrace/ptrace07.c static void do_test(void) · line 96

Quoted 13 lines

index 6bd2b1062..a60c2a49e 100644

testcases/kernel/syscalls/ptrace/ptrace07.c +1 -0
@@ -35,6 +35,7 @@
 #include "config.h"
 #include "ptrace.h"
 #include "tst_test.h"
+#include "tst_safe_macros.h"
 #include "lapi/cpuid.h"
 
 #ifndef PTRACE_GETREGSET
@@ -95,7 +96,7 @@ static void do_test(void)
       * of the XSAVE/XRSTOR save area) required by enabled features in

XCR0.

testcases/kernel/syscalls/ptrace/ptrace07.c static void do_test(void) · line 102

Quoted 7 lines
testcases/kernel/syscalls/ptrace/ptrace07.c +1 -1
       */
      __cpuid_count(CPUID_LEAF_XSTATE, ecx, eax, ebx, ecx, edx);
-     xstate = aligned_alloc(64, ebx);
+     xstate = SAFE_MEMALIGN(64, ebx);
      struct iovec iov = { .iov_base = xstate, .iov_len = ebx };
      int status;
      bool okay;

Yes, it's better for LTP compilation. Thanks for the patch!

I checked SAFE_MEMALIGN(), it will verify that the buffer is NULL or not. "

Quoted 9 lines
        rval = memalign(alignment, size);

        if (rval == NULL) {
                tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
                        "memalign() failed");
        }

"
So could you remove below unnecessary lines in ptrace07.c, thanks.

Quoted 1 line

I helped modify this and pushed it, thanks!

Great, thanks!
I tried that "SAFE_MEMALIGN(64, ebx);" modification in ptrace07. Ptrace07 works well and passed in previous reproduced server.

Thanks!
BR.

Quoted 4 lines
Signature · 3 lines
-- 
Regards,
Li Wang
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH 2/2] ptrace07: Fix compilation by avoiding aligned_alloc

Quoted 55 lines · Pengfei Xu

Hi Li Wang,

Quoted 25 lines · Li Wang
Quoted 23 lines · Pengfei Xu

Hi Richard,

Review

Quoted 9 lines · Richard Palethorpe

Use memalign instead because we live in the past.

---
 testcases/kernel/syscalls/ptrace/ptrace07.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/testcases/kernel/syscalls/ptrace/ptrace07.c

Quoted 10 lines

index 6bd2b1062..a60c2a49e 100644

testcases/kernel/syscalls/ptrace/ptrace07.c +1 -0
@@ -35,6 +35,7 @@
 #include "config.h"
 #include "ptrace.h"
 #include "tst_test.h"
+#include "tst_safe_macros.h"
 #include "lapi/cpuid.h"
 
Quoted 3 lines
Quoted 3 lines

testcases/kernel/syscalls/ptrace/ptrace07.c static void do_test(void) · line 96

Quoted 3 lines
testcases/kernel/syscalls/ptrace/ptrace07.c +0 -0
 #ifndef PTRACE_GETREGSET
@@ -95,7 +96,7 @@ static void do_test(void)
       * of the XSAVE/XRSTOR save area) required by enabled features

in

Quoted 9 lines
Quoted 9 lines

XCR0.

Quoted 7 lines
testcases/kernel/syscalls/ptrace/ptrace07.c +1 -1
       */
      __cpuid_count(CPUID_LEAF_XSTATE, ecx, eax, ebx, ecx, edx);
-     xstate = aligned_alloc(64, ebx);
+     xstate = SAFE_MEMALIGN(64, ebx);
      struct iovec iov = { .iov_base = xstate, .iov_len = ebx };
      int status;
      bool okay;
Quoted 3 lines
Quoted 3 lines

Yes, it's better for LTP compilation. Thanks for the patch!

I checked SAFE_MEMALIGN(), it will verify that the buffer is NULL or

not.

Quoted 11 lines
Quoted 10 lines

"

        rval = memalign(alignment, size);

        if (rval == NULL) {
                tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
                        "memalign() failed");
        }

"
So could you remove below unnecessary lines in ptrace07.c, thanks.

Quoted 5 lines
Quoted 1 line

I helped modify this and pushed it, thanks!

Great, thanks!
I tried that "SAFE_MEMALIGN(64, ebx);" modification in ptrace07. Ptrace07 works well and passed in previous reproduced server.

Cool~ thanks for your feedback.

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw
In reply to Richard Palethorpe via ltp
Quoted 6 lines · Richard Palethorpe via ltp

Strictly cpuid.h should not be included on non-x86, but this means each test author has to remember not to include it. Instead we can set a blank macro to allow compilation.

---

Hi,
defining an empty macro is a bad idea because it'll allow a broken test to be compiled and then it'll behave erratically for no apparent reason. It's better to get a build failure.

Reverting 1df4de06206b079f24dde7157d037b48727c743d is the best solution here. Building ptrace07 and similar arch-specific tests without a key piece of code does not make sense. The preprocessor arch checks should wrap around the whole file, not just a small non-portable bit that's crucial for the test to work.

Signature · 11 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw
Quoted 13 lines · Martin Doucha
Quoted 6 lines · Richard Palethorpe via ltp

Strictly cpuid.h should not be included on non-x86, but this means each test author has to remember not to include it. Instead we can set a blank macro to allow compilation.

---

Hi,
defining an empty macro is a bad idea because it'll allow a broken test to be compiled and then it'll behave erratically for no apparent reason. It's better to get a build failure.

Quoted 6 lines

Reverting 1df4de06206b079f24dde7157d037b48727c743d is the best solution here. Building ptrace07 and similar arch-specific tests without a key piece of code does not make sense. The preprocessor arch checks should wrap around the whole file, not just a small non-portable bit that's crucial for the test to work.

From what I know, one of the uses of "empty macro" is to conditionally include certain portions of a program. In ptrace07, it invokes that useless macro for compiling pass on non-x86 arch but does not allow execute it.

I don't see why that's crucial for a test, if we wrap around the whole file and
avoid it compiling on non-x86, isn't this essentially same as that?

The only distinction between them is partly or wholly skipping the key code compilation. or maybe I completely misunderstood this part.

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

Hello Li,

Quoted 25 lines · Li Wang

On Wed, Oct 19, 2022 at 5:30 PM Martin Doucha <[email protected]> wrote:

Quoted 6 lines · Richard Palethorpe via ltp

Strictly cpuid.h should not be included on non-x86, but this means each test author has to remember not to include it. Instead we can set a blank macro to allow compilation.

---

Hi,
defining an empty macro is a bad idea because it'll allow a broken test to be compiled and then it'll behave erratically for no apparent reason. It's better to get a build failure.

Reverting 1df4de06206b079f24dde7157d037b48727c743d is the best solution here. Building ptrace07 and similar arch-specific tests without a key piece of code does not make sense. The preprocessor arch checks should wrap around the whole file, not just a small non-portable bit that's crucial for the test to work.

From what I know, one of the uses of "empty macro" is to conditionally include certain portions of a program. In ptrace07, it invokes that useless macro for compiling pass on non-x86 arch but does not allow execute it.

Quoted 5 lines

I don't see why that's crucial for a test, if we wrap around the whole file and avoid it compiling on non-x86, isn't this essentially same as that?

The only distinction between them is partly or wholly skipping the key code compilation. or maybe I completely misunderstood this part.

Yes that's it. Since we decided to skip arches depending on what is tst_test instead of compiling out the whole test. Which we want to do because it provides meta-data.

I guess we could define the macro as ts_brk(TCONF, ...). However another problem I just thought of is that cpuid.h might exist on a non-x86 system. So we'll still get a compilation failure.

Ideally we want compilation to fail even on x86 if the correct guards are not in place and we include cpuid.h.

So we could use some macros like TST_ARCH_GUARD_X86 which is defined by TST_IF_X86 and undefined by TST_ELSE_X86/TST_ENDIF_X86. Then cpuid.h can check for that macro.

We could also define assembly in TST_ASM_X86 etc., although it's more obvious that won't compile on non x86.

Alternatively I could try doing something in Sparse, but the macro pass is maybe not as easy to hook into.

Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw
In reply to Li Wang
Quoted 13 lines · Li Wang

On Wed, Oct 19, 2022 at 5:30 PM Martin Doucha <[email protected] <mailto:[email protected]>> wrote:
Reverting 1df4de06206b079f24dde7157d037b48727c743d is the best solution here. Building ptrace07 and similar arch-specific tests without a key
piece of code does not make sense. The preprocessor arch checks should
wrap around the whole file, not just a small non-portable bit that's
crucial for the test to work.

From what I know, one of the uses of "empty macro" is to conditionally
include certain portions of a program. In ptrace07, it invokes that useless macro for compiling pass on non-x86 arch but does not allow execute it.

Quoted 5 lines

I don't see why that's crucial for a test, if we wrap around the whole file and avoid it compiling on non-x86, isn't this essentially same as that?

The only distinction between them is partly or wholly skipping the key code compilation. or maybe I completely misunderstood this part.

The code that would be generated by the non-empty version of the macro is crucial for rest of the program to work. Putting conditional build directives only around a few lines of code can cause bogus warnings about uninitialized variables and make it difficult to add more arch-specific code like the cpuid_count() macro. There's nothing wrong with writing tests like this:

#include "tst_test.h"

#ifdef __x86_64__
#include "lapi/cpuid.h"

void setup(void)

{
	...
}

void run(void)

{
	...
}

void cleanup(void)

{
	...
}
static struct tst_test test = {
	...
	.supported_archs = (const char *const []) {
		"x86_64",
		NULL
	},
};

#else /* __x86_64__ */
#define TST_TEST_TCONF("this test is only supported on x86_64") #endif /* __x86_64__ */

IIUC, the metadata parser will still read .supported_archs regardless of conditional build directives. And it'll prevent erratic test behavior in edge cases where the LTP library believes the code was compiled for the right architecture but the GCC preprocessor disagrees.

Signature · 11 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw

Hello,

Quoted 59 lines · Martin Doucha
Quoted 9 lines · Li Wang

On Wed, Oct 19, 2022 at 5:30 PM Martin Doucha <[email protected] <mailto:[email protected]>> wrote:
Reverting 1df4de06206b079f24dde7157d037b48727c743d is the best solution here. Building ptrace07 and similar arch-specific tests without a key piece of code does not make sense. The preprocessor arch checks should wrap around the whole file, not just a small non-portable bit that's crucial for the test to work.
From what I know, one of the uses of "empty macro" is to
conditionally

Quoted 8 lines

include certain portions of a program. In ptrace07, it invokes that useless macro for compiling pass on non-x86 arch but does not allow execute it. I don't see why that's crucial for a test, if we wrap around the whole file and
avoid it compiling on non-x86, isn't this essentially same as that? The only distinction between them is partly or wholly skipping the key
code compilation. or maybe I completely misunderstood this part.

The code that would be generated by the non-empty version of the macro is crucial for rest of the program to work. Putting conditional build directives only around a few lines of code can cause bogus warnings about uninitialized variables and make it difficult to add more arch-specific code like the cpuid_count() macro. There's nothing wrong with writing tests like this:

#include "tst_test.h"

#ifdef __x86_64__
#include "lapi/cpuid.h"

void setup(void)

{
	...
}

void run(void)

{
	...
}

void cleanup(void)

{
	...
}
static struct tst_test test = {
	...
	.supported_archs = (const char *const []) {
		"x86_64",
		NULL
	},
};

#else /* __x86_64__ */
#define TST_TEST_TCONF("this test is only supported on x86_64") #endif /* __x86_64__ */

Quoted 5 lines

IIUC, the metadata parser will still read .supported_archs regardless of conditional build directives. And it'll prevent erratic test behavior in edge cases where the LTP library believes the code was compiled for the right architecture but the GCC preprocessor disagrees.

Yes, this sounds reasonable and I will submit a patch for it (on Monday though).

My remaining concern is that people will include lapi/cpuid.h (or similar) outside of the ifdef and it will not be caught until it's merged into master.

Signature · 5 lines
Thank you,
Richard.

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