diff mbox series

[v4,10/10] selftests/harness: Fix TEST_F()'s exit codes

Message ID 20240502210926.145539-11-mic@digikod.net (mailing list archive)
State New
Headers show
Series Fix Kselftest's vfork() side effects | expand

Commit Message

Mickaël Salaün May 2, 2024, 9:09 p.m. UTC
Always forward grandchild's exit codes to its parent.  This fixes
KVM_ONE_VCPU_TEST().

Initial patch written by Sean Christopherson [1].

Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: Mark Brown <broonie@kernel.org>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Will Drewry <wad@chromium.org>
Link: https://lore.kernel.org/r/ZjPelW6-AbtYvslu@google.com [1]
Fixes: 0710a1a73fb4 ("selftests/harness: Merge TEST_F_FORK() into TEST_F()")
Signed-off-by: Mickaël Salaün <mic@digikod.net>
Link: https://lore.kernel.org/r/20240502210926.145539-11-mic@digikod.net
---

Changes since v3:
* New patch mainly from Sean Christopherson.
---
 tools/testing/selftests/kselftest_harness.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

Comments

Sean Christopherson May 2, 2024, 10:45 p.m. UTC | #1
On Thu, May 02, 2024, Mickaël Salaün wrote:
> @@ -462,8 +462,10 @@ static inline pid_t clone3_vfork(void)
>  		munmap(teardown, sizeof(*teardown)); \
>  		if (self && fixture_name##_teardown_parent) \
>  			munmap(self, sizeof(*self)); \
> -		if (!WIFEXITED(status) && WIFSIGNALED(status)) \
> -			/* Forward signal to __wait_for_test(). */ \
> +		/* Forward exit codes and signals to __wait_for_test(). */ \
> +		if (WIFEXITED(status)) \
> +			_exit(_metadata->exit_code); \

This needs to be:

		if (WIFEXITED(status)) \
			_exit(WEXITSTATUS(status)); \

otherwise existing tests that communicate FAIL/SKIP via exit() continue to yield
exit(0) and thus false passes.

If that conflicts with tests that want to communicate via _metadata->exit_code,
then maybe this?

		if (WIFEXITED(status)) \
			_exit(WEXITSTATUS(status) ?: _metadata->exit_code); \

Or I suppose _metadata->exit_code could have priority, but that seems weird to
me, e.g. if a test sets exit_code and then explodes, it seems like the explosion
should be reported.

> +		if (WIFSIGNALED(status)) \
>  			kill(getpid(), WTERMSIG(status)); \
>  		__test_check_assert(_metadata); \
>  	} \
> -- 
> 2.45.0
>
Mickaël Salaün May 3, 2024, 7:51 a.m. UTC | #2
On Thu, May 02, 2024 at 03:45:22PM GMT, Sean Christopherson wrote:
> On Thu, May 02, 2024, Mickaël Salaün wrote:
> > @@ -462,8 +462,10 @@ static inline pid_t clone3_vfork(void)
> >  		munmap(teardown, sizeof(*teardown)); \
> >  		if (self && fixture_name##_teardown_parent) \
> >  			munmap(self, sizeof(*self)); \
> > -		if (!WIFEXITED(status) && WIFSIGNALED(status)) \
> > -			/* Forward signal to __wait_for_test(). */ \
> > +		/* Forward exit codes and signals to __wait_for_test(). */ \
> > +		if (WIFEXITED(status)) \
> > +			_exit(_metadata->exit_code); \
> 
> This needs to be:
> 
> 		if (WIFEXITED(status)) \
> 			_exit(WEXITSTATUS(status)); \
> 
> otherwise existing tests that communicate FAIL/SKIP via exit() continue to yield
> exit(0) and thus false passes.

Yes of course.

> 
> If that conflicts with tests that want to communicate via _metadata->exit_code,
> then maybe this?
> 
> 		if (WIFEXITED(status)) \
> 			_exit(WEXITSTATUS(status) ?: _metadata->exit_code); \

I prefer this approach handling failed expectations in the fixture
teardown too.

However, the direct call to _exit() doesn't handle failed asserts.  I'll
fix that.


> 
> Or I suppose _metadata->exit_code could have priority, but that seems weird to
> me, e.g. if a test sets exit_code and then explodes, it seems like the explosion
> should be reported.
> 
> > +		if (WIFSIGNALED(status)) \
> >  			kill(getpid(), WTERMSIG(status)); \
> >  		__test_check_assert(_metadata); \
> >  	} \
> > -- 
> > 2.45.0
> > 
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
index d3837a3a584e..77d4fac8d0c0 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -442,7 +442,7 @@  static inline pid_t clone3_vfork(void)
 				fixture_name##_setup(_metadata, self, variant->data); \
 				/* Let setup failure terminate early. */ \
 				if (_metadata->exit_code) \
-					_exit(0); \
+					_exit(_metadata->exit_code); \
 				_metadata->setup_completed = true; \
 				fixture_name##_##test_name(_metadata, self, variant->data); \
 			} else if (child < 0 || child != waitpid(child, &status, 0)) { \
@@ -454,7 +454,7 @@  static inline pid_t clone3_vfork(void)
 			if (_metadata->setup_completed && !fixture_name##_teardown_parent && \
 					__sync_bool_compare_and_swap(teardown, false, true)) \
 				fixture_name##_teardown(_metadata, self, variant->data); \
-			_exit(0); \
+			_exit(_metadata->exit_code); \
 		} \
 		if (_metadata->setup_completed && fixture_name##_teardown_parent && \
 				__sync_bool_compare_and_swap(teardown, false, true)) \
@@ -462,8 +462,10 @@  static inline pid_t clone3_vfork(void)
 		munmap(teardown, sizeof(*teardown)); \
 		if (self && fixture_name##_teardown_parent) \
 			munmap(self, sizeof(*self)); \
-		if (!WIFEXITED(status) && WIFSIGNALED(status)) \
-			/* Forward signal to __wait_for_test(). */ \
+		/* Forward exit codes and signals to __wait_for_test(). */ \
+		if (WIFEXITED(status)) \
+			_exit(_metadata->exit_code); \
+		if (WIFSIGNALED(status)) \
 			kill(getpid(), WTERMSIG(status)); \
 		__test_check_assert(_metadata); \
 	} \