Skip to contentSkip to conversation

y2038: testsuite/smokey/y2038: testcase for mutex_timedlock64

PATCH Superseded Song Chen 2 participants 3 messages Jun 3, 2021

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Song Chen

author
#0 lore raw

PATCH v2 3/3 y2038: testsuite/smokey/y2038: testcase for mutex_timedlock64

add test case for mutex_timedlock64 in testsuite

---
v2:
1, new revision regarding review comments

---
 testsuite/smokey/y2038/syscall-tests.c | 136 +++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)
testsuite/smokey/y2038/syscall-tests.c +136 -0
index 0e6b84f..6f19350 100644
@@ -199,6 +199,138 @@ static int test_sc_cobalt_sem_timedwait64(void)
 	return 0;
 }
 
+static void *__mutex_timedlock64_thread(void *arg)
+{
+	pthread_mutex_t *lock = arg;
+	int ret;
+	int sr_nc = sc_cobalt_mutex_timedlock64;
+	struct xn_timespec64 ts64, ts_wu;
+	struct timespec ts_nat;
+
+	ret = clock_gettime(CLOCK_MONOTONIC, &ts_nat);
+	if (ret)
+		return NULL;
+
+	ts64.tv_sec = ts_nat.tv_sec;
+	ts64.tv_nsec = ts_nat.tv_nsec;
+	ts_add_ns(&ts64, 5000000);
+
+	ret = XENOMAI_SYSCALL2(sr_nc, lock, &ts64);
+	if (!smokey_assert(ret == -ETIMEDOUT))
+		return NULL;
+
+	ret = clock_gettime(CLOCK_MONOTONIC, &ts_nat);
+	if (ret)
+		return NULL;
+
+	ts_wu.tv_sec = ts_nat.tv_sec;
+	ts_wu.tv_nsec = ts_nat.tv_nsec;
+
+	if (ts_less(&ts_wu, &ts64))
+		smokey_warning("mutex_timedlock64 returned to early!\n"
+			       "Expected wakeup at: %lld sec %lld nsec\n"
+			       "Back at           : %lld sec %lld nsec\n",
+			       ts64.tv_sec, ts64.tv_nsec, ts_wu.tv_sec,
+			       ts_wu.tv_nsec);
+
+	return NULL;
+}
+
+static int __mutex_timedlock64(pthread_mutex_t *lock)
+{
+	int ret;
+	pthread_t tid;
+
+	ret = pthread_mutex_lock(lock);
+	if (ret)
+		return errno;
+
+	ret = pthread_create(&tid, NULL, __mutex_timedlock64_thread, lock);
+	if (ret)
+		return errno;
+
+	ret = pthread_join(tid, NULL);
+	if (ret)
+		return errno;
+
+	ret = pthread_mutex_unlock(lock);
+	if (ret)
+		return errno;
+
+	ret = pthread_mutex_destroy(lock);
+	if (ret)
+		return errno;
+
+	return 0;
+}
+
+static int test_sc_cobalt_mutex_timedlock64(void)
+{
+	int ret;
+	int sr_nc = sc_cobalt_mutex_timedlock64;
+	struct xn_timespec64 ts64;
+
+	pthread_mutexattr_t mattr;
+	pthread_mutex_t lock;
+
+	pthread_mutexattr_init(&mattr);
+	pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
+	pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_PRIVATE);
+	pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
+	ret = pthread_mutex_init(&lock, &mattr);
+	pthread_mutexattr_destroy(&mattr);
+
+	/* Make sure we don't crash because of NULL pointers */
+	ret = XENOMAI_SYSCALL2(sr_nc, NULL, NULL);
+	if (ret == -ENOSYS) {
+		smokey_note("mutex_timedlock64: skipped. (no kernel support)");
+		return 0; // Not implemented, nothing to test, success
+	}
+	if (!smokey_assert(ret == -EINVAL))
+		return ret ? ret : -EINVAL;
+
+	/* Timeout is never read by the kernel, so NULL should be OK */
+	ret = XENOMAI_SYSCALL2(sr_nc, &lock, NULL);
+	if (!smokey_assert(ret == -EFAULT))
+		return ret ? ret : -EINVAL;
+
+	/*
+	 * The mutex is already exhausted, so calling again will validate
+	 * the provided timeout now. Providing NULL has to deliver EFAULT
+	 */
+	ret = XENOMAI_SYSCALL2(sr_nc, &lock, NULL);
+	if (!smokey_assert(ret == -EFAULT))
+		return ret ? ret : -EINVAL;
+
+	/*
+	 * The mutex is already exhausted, so calling again will validate
+	 * the provided timeout now. Providing an invalid address has to deliver
+	 * EFAULT
+	 */
+	ret = XENOMAI_SYSCALL2(sr_nc, &lock, (void *)0xdeadbeefUL);
+	if (!smokey_assert(ret == -EFAULT))
+		return ret ? ret : -EINVAL;
+
+	/*
+	 * The mutex is still exhausted, calling again will validate the
+	 * timeout, providing an invalid timeout has to deliver EINVAL
+	 */
+	ts64.tv_sec = -1;
+	ret = XENOMAI_SYSCALL2(sr_nc, &lock, &ts64);
+	if (!smokey_assert(ret == -EINVAL))
+		return ret ? ret : -EINVAL;
+
+	/*
+	 * Providing a valid timeout, waiting for it to time out and check
+	 * that we didn't come back to early.
+	 */
+	ret = __mutex_timedlock64(&lock);
+	if (ret)
+		return errno;
+
+	return 0;
+}
+
 static int test_sc_cobalt_clock_gettime64(void)
 {
 	int ret;
@@ -430,5 +562,9 @@ static int run_y2038(struct smokey_test *t, int argc, char *const argv[])
 	if (ret)
 		return ret;
 
+	ret = test_sc_cobalt_mutex_timedlock64();
+	if (ret)
+		return ret;
+
 	return 0;
 }
Signature · 1 line
2.7.4
#1 lore raw

Hi Song!

Quoted 70 lines · Song Chen

add test case for mutex_timedlock64 in testsuite

---
v2:
1, new revision regarding review comments

---
 testsuite/smokey/y2038/syscall-tests.c | 136 +++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)
testsuite/smokey/y2038/syscall-tests.c +51 -0
index 0e6b84f..6f19350 100644
@@ -199,6 +199,138 @@ static int test_sc_cobalt_sem_timedwait64(void)
 	return 0;
 }
 
+static void *__mutex_timedlock64_thread(void *arg)
+{
+	pthread_mutex_t *lock = arg;
+	int ret;
+	int sr_nc = sc_cobalt_mutex_timedlock64;
+	struct xn_timespec64 ts64, ts_wu;
+	struct timespec ts_nat;
+
+	ret = clock_gettime(CLOCK_MONOTONIC, &ts_nat);
+	if (ret)
+		return NULL;
+
+	ts64.tv_sec = ts_nat.tv_sec;
+	ts64.tv_nsec = ts_nat.tv_nsec;
+	ts_add_ns(&ts64, 5000000);
+
+	ret = XENOMAI_SYSCALL2(sr_nc, lock, &ts64);
+	if (!smokey_assert(ret == -ETIMEDOUT))
+		return NULL;
+
+	ret = clock_gettime(CLOCK_MONOTONIC, &ts_nat);
+	if (ret)
+		return NULL;
+
+	ts_wu.tv_sec = ts_nat.tv_sec;
+	ts_wu.tv_nsec = ts_nat.tv_nsec;
+
+	if (ts_less(&ts_wu, &ts64))
+		smokey_warning("mutex_timedlock64 returned to early!\n"
+			       "Expected wakeup at: %lld sec %lld nsec\n"
+			       "Back at           : %lld sec %lld nsec\n",
+			       ts64.tv_sec, ts64.tv_nsec, ts_wu.tv_sec,
+			       ts_wu.tv_nsec);
+
+	return NULL;
+}
+
+static int __mutex_timedlock64(pthread_mutex_t *lock)
+{
+	int ret;
+	pthread_t tid;
+
+	ret = pthread_mutex_lock(lock);
+	if (ret)
+		return errno;
+
+	ret = pthread_create(&tid, NULL, __mutex_timedlock64_thread, lock);
+	if (ret)
+		return errno;
+
+	ret = pthread_join(tid, NULL);

testsuite/smokey/y2038/syscall-tests.c static int test_sc_cobalt_sem_timedwait64(void) · line 262

Quoted 10 lines
testsuite/smokey/y2038/syscall-tests.c +10 -0
+	if (ret)
+		return errno;
+
+	ret = pthread_mutex_unlock(lock);
+	if (ret)
+		return errno;
+
+	ret = pthread_mutex_destroy(lock);
+	if (ret)
+		return errno;

Here and all error handling paths above: Return -errno.

Quoted 23 lines
testsuite/smokey/y2038/syscall-tests.c +23 -0
+
+	return 0;
+}
+
+static int test_sc_cobalt_mutex_timedlock64(void)
+{
+	int ret;
+	int sr_nc = sc_cobalt_mutex_timedlock64;
+	struct xn_timespec64 ts64;
+
+	pthread_mutexattr_t mattr;
+	pthread_mutex_t lock;
+
+	pthread_mutexattr_init(&mattr);
+	pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
+	pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_PRIVATE);
+	pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
+	ret = pthread_mutex_init(&lock, &mattr);
+	pthread_mutexattr_destroy(&mattr);
+
+	/* Make sure we don't crash because of NULL pointers */
+	ret = XENOMAI_SYSCALL2(sr_nc, NULL, NULL);
+	if (ret == -ENOSYS) {

testsuite/smokey/y2038/syscall-tests.c static int test_sc_cobalt_sem_timedwait64(void) · line 295

Quoted 10 lines
testsuite/smokey/y2038/syscall-tests.c +10 -0
+		smokey_note("mutex_timedlock64: skipped. (no kernel support)");
+		return 0; // Not implemented, nothing to test, success
+	}
+	if (!smokey_assert(ret == -EINVAL))
+		return ret ? ret : -EINVAL;
+
+	/* Timeout is never read by the kernel, so NULL should be OK */
+	ret = XENOMAI_SYSCALL2(sr_nc, &lock, NULL);
+	if (!smokey_assert(ret == -EFAULT))
+		return ret ? ret : -EINVAL;

The test description and test implementation do not match here. NULL should be OK vs. we got -EFAULT.

The most important thing: Can I expect a patch for the POSIX violation to appear, or should I have a look?

For now: Expect ret == 0 (!ret) and if that is not the case trigger a warning ("POSIX violation detected") and continue instead of "return".

Quoted 24 lines
testsuite/smokey/y2038/syscall-tests.c +24 -0
+
+	/*
+	 * The mutex is already exhausted, so calling again will validate
+	 * the provided timeout now. Providing NULL has to deliver EFAULT
+	 */
+	ret = XENOMAI_SYSCALL2(sr_nc, &lock, NULL);
+	if (!smokey_assert(ret == -EFAULT))
+		return ret ? ret : -EINVAL;
+
+	/*
+	 * The mutex is already exhausted, so calling again will validate
+	 * the provided timeout now. Providing an invalid address has to deliver
+	 * EFAULT
+	 */
+	ret = XENOMAI_SYSCALL2(sr_nc, &lock, (void *)0xdeadbeefUL);
+	if (!smokey_assert(ret == -EFAULT))
+		return ret ? ret : -EINVAL;
+
+	/*
+	 * The mutex is still exhausted, calling again will validate the
+	 * timeout, providing an invalid timeout has to deliver EINVAL
+	 */
+	ts64.tv_sec = -1;
+	ret = XENOMAI_SYSCALL2(sr_nc, &lock, &ts64);

testsuite/smokey/y2038/syscall-tests.c static int test_sc_cobalt_sem_timedwait64(void) · line 329

Quoted 10 lines
testsuite/smokey/y2038/syscall-tests.c +10 -0
+	if (!smokey_assert(ret == -EINVAL))
+		return ret ? ret : -EINVAL;
+
+	/*
+	 * Providing a valid timeout, waiting for it to time out and check
+	 * that we didn't come back to early.
+	 */
+	ret = __mutex_timedlock64(&lock);
+	if (ret)
+		return errno;

Let's return ret here. -errno would be a possibility as well, but ret makes the code easier to read.

Quoted 18 lines
testsuite/smokey/y2038/syscall-tests.c +8 -0
+
+	return 0;
+}
+
 static int test_sc_cobalt_clock_gettime64(void)
 {
 	int ret;
@@ -430,5 +562,9 @@ static int run_y2038(struct smokey_test *t, int argc, char *const argv[])
 	if (ret)
 		return ret;
 
+	ret = test_sc_cobalt_mutex_timedlock64();
+	if (ret)
+		return ret;
+
 	return 0;
 }
Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux
#2 lore raw
Quoted 77 lines · Florian Bezdeka

Hi Song!

Quoted 74 lines · Song Chen

add test case for mutex_timedlock64 in testsuite

---
v2:
1, new revision regarding review comments

---
  testsuite/smokey/y2038/syscall-tests.c | 136 +++++++++++++++++++++++++++++++++
  1 file changed, 136 insertions(+)
testsuite/smokey/y2038/syscall-tests.c +55 -0
index 0e6b84f..6f19350 100644
@@ -199,6 +199,138 @@ static int test_sc_cobalt_sem_timedwait64(void)
  	return 0;
  }
 
+static void *__mutex_timedlock64_thread(void *arg)
+{
+	pthread_mutex_t *lock = arg;
+	int ret;
+	int sr_nc = sc_cobalt_mutex_timedlock64;
+	struct xn_timespec64 ts64, ts_wu;
+	struct timespec ts_nat;
+
+	ret = clock_gettime(CLOCK_MONOTONIC, &ts_nat);
+	if (ret)
+		return NULL;
+
+	ts64.tv_sec = ts_nat.tv_sec;
+	ts64.tv_nsec = ts_nat.tv_nsec;
+	ts_add_ns(&ts64, 5000000);
+
+	ret = XENOMAI_SYSCALL2(sr_nc, lock, &ts64);
+	if (!smokey_assert(ret == -ETIMEDOUT))
+		return NULL;
+
+	ret = clock_gettime(CLOCK_MONOTONIC, &ts_nat);
+	if (ret)
+		return NULL;
+
+	ts_wu.tv_sec = ts_nat.tv_sec;
+	ts_wu.tv_nsec = ts_nat.tv_nsec;
+
+	if (ts_less(&ts_wu, &ts64))
+		smokey_warning("mutex_timedlock64 returned to early!\n"
+			       "Expected wakeup at: %lld sec %lld nsec\n"
+			       "Back at           : %lld sec %lld nsec\n",
+			       ts64.tv_sec, ts64.tv_nsec, ts_wu.tv_sec,
+			       ts_wu.tv_nsec);
+
+	return NULL;
+}
+
+static int __mutex_timedlock64(pthread_mutex_t *lock)
+{
+	int ret;
+	pthread_t tid;
+
+	ret = pthread_mutex_lock(lock);
+	if (ret)
+		return errno;
+
+	ret = pthread_create(&tid, NULL, __mutex_timedlock64_thread, lock);
+	if (ret)
+		return errno;
+
+	ret = pthread_join(tid, NULL);
+	if (ret)
+		return errno;
+
+	ret = pthread_mutex_unlock(lock);
Quoted 8 lines

testsuite/smokey/y2038/syscall-tests.c static int test_sc_cobalt_sem_timedwait64(void) · line 262

Quoted 6 lines
testsuite/smokey/y2038/syscall-tests.c +6 -0
+	if (ret)
+		return errno;
+
+	ret = pthread_mutex_destroy(lock);
+	if (ret)
+		return errno;

Here and all error handling paths above: Return -errno.

done

Quoted 38 lines
Quoted 24 lines
testsuite/smokey/y2038/syscall-tests.c +23 -0
+
+	return 0;
+}
+
+static int test_sc_cobalt_mutex_timedlock64(void)
+{
+	int ret;
+	int sr_nc = sc_cobalt_mutex_timedlock64;
+	struct xn_timespec64 ts64;
+
+	pthread_mutexattr_t mattr;
+	pthread_mutex_t lock;
+
+	pthread_mutexattr_init(&mattr);
+	pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
+	pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_PRIVATE);
+	pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
+	ret = pthread_mutex_init(&lock, &mattr);
+	pthread_mutexattr_destroy(&mattr);
+
+	/* Make sure we don't crash because of NULL pointers */
+	ret = XENOMAI_SYSCALL2(sr_nc, NULL, NULL);
+	if (ret == -ENOSYS) {

testsuite/smokey/y2038/syscall-tests.c static int test_sc_cobalt_sem_timedwait64(void) · line 295

Quoted 10 lines
testsuite/smokey/y2038/syscall-tests.c +10 -0
+		smokey_note("mutex_timedlock64: skipped. (no kernel support)");
+		return 0; // Not implemented, nothing to test, success
+	}
+	if (!smokey_assert(ret == -EINVAL))
+		return ret ? ret : -EINVAL;
+
+	/* Timeout is never read by the kernel, so NULL should be OK */
+	ret = XENOMAI_SYSCALL2(sr_nc, &lock, NULL);
+	if (!smokey_assert(ret == -EFAULT))
+		return ret ? ret : -EINVAL;

The test description and test implementation do not match here. NULL should be OK vs. we got -EFAULT.

Quoted 5 lines

The most important thing: Can I expect a patch for the POSIX violation to appear, or should I have a look?

For now: Expect ret == 0 (!ret) and if that is not the case trigger a warning ("POSIX violation detected") and continue instead of "return".

Yes, thanks a lot for the explanation, it's clear to me now, ret should be null, but for some issue, expectation can't be met, however we don't want to terminate the test, then throw a warning.

/Song

Quoted 30 lines
Quoted 30 lines
testsuite/smokey/y2038/syscall-tests.c +29 -0
+
+	/*
+	 * The mutex is already exhausted, so calling again will validate
+	 * the provided timeout now. Providing NULL has to deliver EFAULT
+	 */
+	ret = XENOMAI_SYSCALL2(sr_nc, &lock, NULL);
+	if (!smokey_assert(ret == -EFAULT))
+		return ret ? ret : -EINVAL;
+
+	/*
+	 * The mutex is already exhausted, so calling again will validate
+	 * the provided timeout now. Providing an invalid address has to deliver
+	 * EFAULT
+	 */
+	ret = XENOMAI_SYSCALL2(sr_nc, &lock, (void *)0xdeadbeefUL);
+	if (!smokey_assert(ret == -EFAULT))
+		return ret ? ret : -EINVAL;
+
+	/*
+	 * The mutex is still exhausted, calling again will validate the
+	 * timeout, providing an invalid timeout has to deliver EINVAL
+	 */
+	ts64.tv_sec = -1;
+	ret = XENOMAI_SYSCALL2(sr_nc, &lock, &ts64);
+	if (!smokey_assert(ret == -EINVAL))
+		return ret ? ret : -EINVAL;
+
+	/*
+	 * Providing a valid timeout, waiting for it to time out and check
Quoted 8 lines

testsuite/smokey/y2038/syscall-tests.c static int test_sc_cobalt_sem_timedwait64(void) · line 329

Quoted 5 lines
testsuite/smokey/y2038/syscall-tests.c +5 -0
+	 * that we didn't come back to early.
+	 */
+	ret = __mutex_timedlock64(&lock);
+	if (ret)
+		return errno;

Let's return ret here. -errno would be a possibility as well, but ret makes the code easier to read.

done

Quoted 21 lines
Quoted 19 lines
testsuite/smokey/y2038/syscall-tests.c +8 -0
+
+	return 0;
+}
+
  static int test_sc_cobalt_clock_gettime64(void)
  {
  	int ret;
@@ -430,5 +562,9 @@ static int run_y2038(struct smokey_test *t, int argc, char *const argv[])
  	if (ret)
  		return ret;
 
+	ret = test_sc_cobalt_mutex_timedlock64();
+	if (ret)
+		return ret;
+
  	return 0;
  }