Skip to contentSkip to conversation

Add TST_ASSERT_SYSCALL*() macros

PATCH Superseded Martin Doucha 2 participants 3 messages Mar 5, 2020 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Add TST_ASSERT_SYSCALL*() macros

These macros take care of the standard return value checking boilerplate in cases where the test cannot continue after error.

- TST_ASSERT_SYSCALL() calls tst_brk() if retval != 0
- TST_ASSERT_SYSCALL_FD() calls tst_brk() if retval < 0

---

Small convenience patch that'll simplify both test development and TST_SAFE_*() library function generation.

 include/tst_test.h | 47 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
include/tst_test.h +47 -0
index 8508c2e38..65a5f05b8 100644
@@ -281,6 +281,53 @@ extern void *TST_RET_PTR;
 		TST_ERR = errno; \
 	} while (0)
 
+/* assert that syscall returned only 0 and nothing else */
+#define TST_ASSERT_SYSCALL(SCALL) \
+	TST_ASSERT_SYSCALL_IMPL(SCALL, __FILE__, __LINE__)
+
+#define TST_ASSERT_SYSCALL_IMPL(SCALL, FILENAME, LINENO) \
+	({ \
+		int _tst_ret; \
+		errno = 0; \
+		_tst_ret = SCALL; \
+		if (_tst_ret == -1) { \
+			int _tst_ttype = errno == ENOTSUP ? TCONF : TBROK; \
+			tst_brk(_tst_ttype | TERRNO, "%s:%d " # SCALL \
+				" failed", FILENAME, LINENO); \
+		} \
+		if (_tst_ret != 0) { \
+			tst_brk(TBROK | TERRNO, "%s:%d " # SCALL \
+				" returned invalid value %d", FILENAME, \
+				LINENO, _tst_ret); \
+		} \
+		_tst_ret; \
+	})
+
+/*
+ * assert that syscall returned any non-negative value (e.g. valid file
+ * descriptor)
+ */
+#define TST_ASSERT_SYSCALL_FD(SCALL) \
+	TST_ASSERT_SYSCALL_FD_IMPL(SCALL, __FILE__, __LINE__)
+
+#define TST_ASSERT_SYSCALL_FD_IMPL(SCALL, FILENAME, LINENO) \
+	({ \
+		int _tst_ret; \
+		errno = 0; \
+		_tst_ret = SCALL; \
+		if (_tst_ret == -1) { \
+			int _tst_ttype = errno == ENOTSUP ? TCONF : TBROK; \
+			tst_brk(_tst_ttype | TERRNO, "%s:%d " # SCALL \
+				" failed", FILENAME, LINENO); \
+		} \
+		if (_tst_ret < 0) { \
+			tst_brk(TBROK | TERRNO, "%s:%d " # SCALL \
+				" returned invalid value %d", FILENAME, \
+				LINENO, _tst_ret); \
+		} \
+		_tst_ret; \
+	})
+
 /*
  * Functions to convert ERRNO to its name and SIGNAL to its name.
  */
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 Reimplement TST_SAFE_TIMERFD_*() using TST_ASSERT_SYSCALL*()

Example usage of the TST_ASSERT_SYSCALL*() macros.

---
 lib/tst_safe_timerfd.c | 35 ++++++-----------------------------
 1 file changed, 6 insertions(+), 29 deletions(-)
lib/tst_safe_timerfd.c +6 -29
index ffe7b2ef7..18da82184 100644
@@ -9,34 +9,18 @@
 #define TST_NO_DEFAULT_MAIN
 #include "tst_test.h"
 
-#define TTYPE (errno == ENOTSUP ? TCONF : TBROK)
-
 int safe_timerfd_create(const char *file, const int lineno,
 				      int clockid, int flags)
 {
-	int fd;
-
-	fd = timerfd_create(clockid, flags);
-	if (fd < 0) {
-		tst_brk(TTYPE | TERRNO, "%s:%d timerfd_create(%s) failed",
-			file, lineno, tst_clock_name(clockid));
-	}
-
-	return fd;
+	return TST_ASSERT_SYSCALL_FD_IMPL(timerfd_create(clockid, flags), file,
+		lineno);
 }
 
 int safe_timerfd_gettime(const char *file, const int lineno,
 				int fd, struct itimerspec *curr_value)
 {
-	int rval;
-
-	rval = timerfd_gettime(fd, curr_value);
-	if (rval != 0) {
-		tst_brk(TTYPE | TERRNO, "%s:%d timerfd_gettime() failed",
-			file, lineno);
-	}
-
-	return rval;
+	return TST_ASSERT_SYSCALL_IMPL(timerfd_gettime(fd, curr_value), file,
+		lineno);
 }
 
 int safe_timerfd_settime(const char *file, const int lineno,
@@ -44,13 +28,6 @@ int safe_timerfd_settime(const char *file, const int lineno,
 				const struct itimerspec *new_value,
 				struct itimerspec *old_value)
 {
-	int rval;
-
-	rval = timerfd_settime(fd, flags, new_value, old_value);
-	if (rval != 0) {
-		tst_brk(TTYPE | TERRNO, "%s:%d timerfd_settime() failed",
-			file, lineno);
-	}
-
-	return rval;
+	return TST_ASSERT_SYSCALL_IMPL(timerfd_settime(fd, flags, new_value,
+		old_value), file, lineno);
 }
Signature · 1 line
2.25.1
#2 lore raw

PATCH 1/2 Add TST_ASSERT_SYSCALL*() macros

In reply to Martin Doucha

Hi!

include/tst_test.h extern void *TST_RET_PTR; · line 295

Quoted 12 lines
include/tst_test.h +12 -0
+/* assert that syscall returned only 0 and nothing else */
+#define TST_ASSERT_SYSCALL(SCALL) \
+	TST_ASSERT_SYSCALL_IMPL(SCALL, __FILE__, __LINE__)
+
+#define TST_ASSERT_SYSCALL_IMPL(SCALL, FILENAME, LINENO) \
+	({ \
+		int _tst_ret; \
+		errno = 0; \
+		_tst_ret = SCALL; \
+		if (_tst_ret == -1) { \
+			int _tst_ttype = errno == ENOTSUP ? TCONF : TBROK; \
+			tst_brk(_tst_ttype | TERRNO, "%s:%d " # SCALL \

^
I do not think that is reasonable to simply stringify the syscall, we are adding pretty printers for the syscall parameters and this change is backward.

These two patches are actually removing the pretty printer for the clock id for the timerfd_create(). I do not like that.

Quoted 44 lines
include/tst_test.h +35 -0
+				" failed", FILENAME, LINENO); \
+		} \
+		if (_tst_ret != 0) { \
+			tst_brk(TBROK | TERRNO, "%s:%d " # SCALL \
+				" returned invalid value %d", FILENAME, \
+				LINENO, _tst_ret); \
+		} \
+		_tst_ret; \
+	})
+
+/*
+ * assert that syscall returned any non-negative value (e.g. valid file
+ * descriptor)
+ */
+#define TST_ASSERT_SYSCALL_FD(SCALL) \
+	TST_ASSERT_SYSCALL_FD_IMPL(SCALL, __FILE__, __LINE__)
+
+#define TST_ASSERT_SYSCALL_FD_IMPL(SCALL, FILENAME, LINENO) \
+	({ \
+		int _tst_ret; \
+		errno = 0; \
+		_tst_ret = SCALL; \
+		if (_tst_ret == -1) { \
+			int _tst_ttype = errno == ENOTSUP ? TCONF : TBROK; \
+			tst_brk(_tst_ttype | TERRNO, "%s:%d " # SCALL \
+				" failed", FILENAME, LINENO); \
+		} \
+		if (_tst_ret < 0) { \
+			tst_brk(TBROK | TERRNO, "%s:%d " # SCALL \
+				" returned invalid value %d", FILENAME, \
+				LINENO, _tst_ret); \
+		} \
+		_tst_ret; \
+	})
+
 /*
  * Functions to convert ERRNO to its name and SIGNAL to its name.
  */
Signature · 5 lines
2.25.1


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