Skip to contentSkip to conversation

scripts/coccinelle: Add headers to lib checks and fix macro fix

PATCH New Richard Palethorpe 4 participants 6 messages Jun 21, 2021 – Aug 10, 2021 Coccinelle/Semantic Patches (SmPL)

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 scripts/coccinelle: Add headers to lib checks and fix macro fix

We have to explicitly add the header directory.

Also allow the TEST macro fix to replace multiple variables.

---
 scripts/coccinelle/libltp-test-macro.cocci |  4 +++-
 scripts/coccinelle/run-spatch.sh           | 10 ++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
scripts/coccinelle/libltp-test-macro.cocci +3 -1
index 7563d23aa..937d6c94f 100644
@@ -24,7 +24,7 @@ virtual fix
 
  TEST(...)
 
- ...
+ <...
 
 (
 - TST_RET
@@ -37,6 +37,8 @@ virtual fix
 + TERRNO
 )
 
+ ...>
+
 // Replace TEST in all functions where it occurs only at the start. It
 // is slightly complicated by adding a newline if a statement appears
 // on the line after TEST(). It is not clear to me what the rules are
scripts/coccinelle/run-spatch.sh +10 -0
index e8e6f47d8..978998cd1 100755
@@ -25,6 +25,12 @@ libltp_spatch() {
 	       -D fix \
 	       --include-headers \
 	       $*
+	spatch --dir include \
+	       --use-gitgrep \
+	       --in-place \
+	       -D fix \
+	       --include-headers \
+	       $*
     else
 	spatch --dir lib \
 	       --ignore lib/parse_opts.c \
@@ -33,6 +39,10 @@ libltp_spatch() {
 	       --use-gitgrep \
 	       --include-headers \
 	       $*
+	spatch --dir include \
+	       --use-gitgrep \
+	       --include-headers \
+	       $*
     fi
 }
 
Signature · 1 line
2.31.1
#1 lore raw

PATCH 2/3 API: Remove TEST macro usage from library headers

Technically the headers are part of the test translation unit. However we think of them as being part of the API. So we should also remove TEST from here.

---
 include/lapi/clone.h             |  6 ++++--
 include/lapi/fsmount.h           | 10 ++++++----
 include/lapi/init_module.h       |  6 ++++--
 include/lapi/io_uring.h          |  9 +++++----
 include/lapi/name_to_handle_at.h |  7 ++++---
 include/lapi/openat2.h           |  6 ++++--
 6 files changed, 27 insertions(+), 17 deletions(-)
include/lapi/clone.h +4 -2
index 81db443c9..5bf51d90e 100644
@@ -39,10 +39,12 @@ static inline int clone3(struct clone_args *args, size_t size)
 
 static inline void clone3_supported_by_kernel(void)
 {
+	long ret;
+
 	if ((tst_kvercmp(5, 3, 0)) < 0) {
 		/* Check if the syscall is backported on an older kernel */
-		TEST(syscall(__NR_clone3, NULL, 0));
-		if (TST_RET == -1 && TST_ERR == ENOSYS)
+		ret = syscall(__NR_clone3, NULL, 0);
+		if (ret == -1 && errno == ENOSYS)
 			tst_brk(TCONF, "Test not supported on kernel version < v5.3");
 	}
 }
include/lapi/fsmount.h +6 -4
index d6ebed9b4..dc39c8791 100644
@@ -133,12 +133,14 @@ enum fsconfig_command {
 
 static inline void fsopen_supported_by_kernel(void)
 {
+	long ret;
+
 	if ((tst_kvercmp(5, 2, 0)) < 0) {
 		/* Check if the syscall is backported on an older kernel */
-		TEST(syscall(__NR_fsopen, NULL, 0));
-		if (TST_RET != -1)
-			SAFE_CLOSE(TST_RET);
-		else if (TST_ERR == ENOSYS)
+		ret = syscall(__NR_fsopen, NULL, 0);
+		if (ret != -1)
+			SAFE_CLOSE(ret);
+		else if (errno == ENOSYS)
 			tst_brk(TCONF, "Test not supported on kernel version < v5.2");
 	}
 }
include/lapi/init_module.h +4 -2
index 14eaabee6..48464a2e4 100644
@@ -24,10 +24,12 @@ static inline int finit_module(int fd, const char *param_values, int flags)
 
 static inline void finit_module_supported_by_kernel(void)
 {
+       long ret;
+
        if ((tst_kvercmp(3, 8, 0)) < 0) {
                /* Check if the syscall is backported on an older kernel */
-               TEST(syscall(__NR_finit_module, 0, "", 0));
-               if (TST_RET == -1 && TST_ERR == ENOSYS)
+               ret = syscall(__NR_finit_module, 0, "", 0);
+               if (ret == -1 && errno == ENOSYS)
                        tst_brk(TCONF, "Test not supported on kernel version < v3.8");
        }
 }
include/lapi/io_uring.h +5 -4
index 897ed7c64..e5c879951 100644
@@ -296,11 +296,12 @@ static inline int io_uring_enter(int fd, unsigned int to_submit,
 
 static inline void io_uring_setup_supported_by_kernel(void)
 {
+	long ret;
 	if ((tst_kvercmp(5, 1, 0)) < 0) {
-		TEST(syscall(__NR_io_uring_setup, NULL, 0));
-		if (TST_RET != -1)
-			SAFE_CLOSE(TST_RET);
-		else if (TST_ERR == ENOSYS)
+		ret = syscall(__NR_io_uring_setup, NULL, 0);
+		if (ret != -1)
+			SAFE_CLOSE(ret);
+		else if (errno == ENOSYS)
 			tst_brk(TCONF,
 				"Test not supported on kernel version < v5.1");
 	}
include/lapi/name_to_handle_at.h +4 -3
index 275db4ae0..3f081f1c0 100644
@@ -34,6 +34,7 @@ static inline int open_by_handle_at(int mount_fd, struct file_handle *handle,
 static inline struct file_handle *
 allocate_file_handle(int dfd, const char *pathname)
 {
+	long ret;
 	struct file_handle fh = {}, *fhp;
 	int mount_id;
 
@@ -41,9 +42,9 @@ allocate_file_handle(int dfd, const char *pathname)
 	 * Make an initial call to name_to_handle_at() to discover the size
 	 * required for the file handle.
 	 */
-	TEST(name_to_handle_at(dfd, pathname, &fh, &mount_id, 0));
-	if (TST_RET != -1 || TST_ERR != EOVERFLOW) {
-		tst_res(TFAIL | TTERRNO,
+	ret = name_to_handle_at(dfd, pathname, &fh, &mount_id, 0);
+	if (ret != -1 || errno != EOVERFLOW) {
+		tst_res(TFAIL | TERRNO,
 			"name_to_handle_at() should fail with EOVERFLOW");
 		return NULL;
 	}
include/lapi/openat2.h +4 -2
index d4154c26e..46804a441 100644
@@ -62,10 +62,12 @@ struct open_how_pad {
 
 static inline void openat2_supported_by_kernel(void)
 {
+	long ret;
+
 	if ((tst_kvercmp(5, 6, 0)) < 0) {
 		/* Check if the syscall is backported on an older kernel */
-		TEST(syscall(__NR_openat2, -1, NULL, NULL, 0));
-		if (TST_RET == -1 && TST_ERR == ENOSYS)
+		ret = syscall(__NR_openat2, -1, NULL, NULL, 0);
+		if (ret == -1 && errno == ENOSYS)
 			tst_brk(TCONF, "Test not supported on kernel version < v5.6");
 	}
 }
Signature · 1 line
2.31.1
#2 lore raw

PATCH 3/3 API: Cause GCC/Clang to blow up when TEST is used in the library

In reply to Richard Palethorpe

As we define LTPLIB on library code, the preprocessor itself can be used to detect when the TEST[_VOID] macros are expanded in the library.

If an API header is not included by any library translation units. Then this will not work. This is true at least for Fuzzy Sync.

---
 include/tst_test_macros.h | 5 +++++
 1 file changed, 5 insertions(+)
include/tst_test_macros.h +5 -0
index 89dfe5a31..9017fd042 100644
@@ -6,6 +6,10 @@
 #ifndef TST_TEST_MACROS_H__
 #define TST_TEST_MACROS_H__
 
+#if LTPLIB
+#define TEST(SCALL) _Pragma("GCC error \"Do not use TEST macro in library\"")
+#define TEST_VOID(SCALL) _Pragma("GCC error \"Do not use TEST_VOID macro in library\"")
+#else
 #define TEST(SCALL) \
 	do { \
 		errno = 0; \
@@ -19,6 +23,7 @@
 		SCALL; \
 		TST_ERR = errno; \
 	} while (0)
+#endif
 
 extern long TST_RET;
 extern int TST_ERR;
Signature · 1 line
2.31.1
#3 lore raw

PATCH 3/3 API: Cause GCC/Clang to blow up when TEST is used in the library

Hi Richie,

include/tst_test_macros.h line 11

Quoted 3 lines
include/tst_test_macros.h +3 -0
+#if LTPLIB
+#define TEST(SCALL) _Pragma("GCC error \"Do not use TEST macro in library\"")
+#define TEST_VOID(SCALL) _Pragma("GCC error \"Do not use TEST_VOID macro in library\"")

It'd be nice to document "Do not use TEST() macro in library" in LTP Library API Writing Guidelines [1].

Kind regards,
Petr

[1] https://github.com/linux-test-project/ltp/wiki/LTP-Library-API-Writing-Guidelines

#4 lore raw

PATCH 3/3 API: Cause GCC/Clang to blow up when TEST is used in the library

Hello,

Quoted 8 lines · Petr Vorel

Hi Richie,

include/tst_test_macros.h line 11

Quoted 3 lines
include/tst_test_macros.h +3 -0
+#if LTPLIB
+#define TEST(SCALL) _Pragma("GCC error \"Do not use TEST macro in library\"")
+#define TEST_VOID(SCALL) _Pragma("GCC error \"Do not use TEST_VOID macro in library\"")

It'd be nice to document "Do not use TEST() macro in library" in LTP Library API Writing Guidelines [1].

Yes, I will submit this in the Sparse patch set today.

Quoted 5 lines

Kind regards,
Petr

[1] https://github.com/linux-test-project/ltp/wiki/LTP-Library-API-Writing-Guidelines

Signature · 2 lines
Thank you,
Richard.