Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH] selftests/harness: Prevent infinite loop due to Assert in FIXTURE_TEARDOWN
@ 2024-03-14 22:43 Shengyu Li
  2024-03-24 21:44 ` Shengyu Li
  2024-03-26 19:28 ` [PATCH] " Shuah Khan
  0 siblings, 2 replies; 5+ messages in thread
From: Shengyu Li @ 2024-03-14 22:43 UTC (permalink / raw)
  To: shuah; +Cc: luto, linux-kselftest, linux-kernel, wad, Shengyu Li

This patch addresses an issue in the selftests/harness where an assertion within FIXTURE_TEARDOWN could trigger an infinite loop. The problem arises because the teardown procedure is meant to execute once, but the presence of failing assertions (ASSERT_EQ(0, 1)) leads to repeated attempts to execute teardown due to the long jump mechanism used by the harness for handling assertions.

To resolve this, the patch ensures that the teardown process runs only once, regardless of assertion outcomes, preventing the infinite loop and allowing tests to fail.

Signed-off-by: Shengyu Li <shengyu.li.evgeny@gmail.com>
---
 tools/testing/selftests/kselftest_harness.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
index 4fd735e48ee7..230d62884885 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -383,6 +383,7 @@
 		FIXTURE_DATA(fixture_name) self; \
 		pid_t child = 1; \
 		int status = 0; \
+		bool jmp = false; \
 		memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
 		if (setjmp(_metadata->env) == 0) { \
 			/* Use the same _metadata. */ \
@@ -399,8 +400,10 @@
 				_metadata->exit_code = KSFT_FAIL; \
 			} \
 		} \
+		else \
+			jmp = true; \
 		if (child == 0) { \
-			if (_metadata->setup_completed && !_metadata->teardown_parent) \
+			if (_metadata->setup_completed && !_metadata->teardown_parent && !jmp) \
 				fixture_name##_teardown(_metadata, &self, variant->data); \
 			_exit(0); \
 		} \
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH] selftests/harness: Prevent infinite loop due to Assert in FIXTURE_TEARDOWN
  2024-03-14 22:43 [PATCH] selftests/harness: Prevent infinite loop due to Assert in FIXTURE_TEARDOWN Shengyu Li
@ 2024-03-24 21:44 ` Shengyu Li
  2024-03-26 21:13   ` [PATCH v2] " Shengyu Li
  2024-03-26 19:28 ` [PATCH] " Shuah Khan
  1 sibling, 1 reply; 5+ messages in thread
From: Shengyu Li @ 2024-03-24 21:44 UTC (permalink / raw)
  To: shuah; +Cc: luto, linux-kselftest, linux-kernel, wad, Shengyu Li

This patch addresses an issue in the selftests/harness where an assertion within FIXTURE_TEARDOWN could trigger an infinite loop. 
The problem arises because the teardown procedure is meant to execute once, 
but the presence of failing assertions (ASSERT_EQ(0, 1)) 
leads to repeated attempts to execute teardown due to the long jump mechanism used by the harness for handling assertions.

To resolve this, the patch ensures that the teardown process runs only once, regardless of assertion outcomes, 
preventing the infinite loop and allowing tests to fail.

A simple test demo, also this is related to the issue mentioned in this patch
https://patchwork.kernel.org/project/linux-kselftest/patch/e2ba3f8c-80e6-477d-9cea-1c9af820e0ed@alu.unizg.hr/

#include "kselftest_harness.h"

FIXTURE(f)
{
	int fd;
};

FIXTURE_SETUP(f)
{
	self->fd = 0;
}

FIXTURE_TEARDOWN(f)
{
	TH_LOG("TEARDOWN");
	ASSERT_EQ(0, 1);
	self->fd = -1;
}

TEST_F(f, open_close)
{
	ASSERT_NE(self->fd, 1);
}

TEST_HARNESS_MAIN


Signed-off-by: Shengyu Li <shengyu.li.evgeny@gmail.com>
---
 tools/testing/selftests/kselftest_harness.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
index 4fd735e48ee7..230d62884885 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -383,6 +383,7 @@
 		FIXTURE_DATA(fixture_name) self; \
 		pid_t child = 1; \
 		int status = 0; \
+		bool jmp = false; \
 		memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
 		if (setjmp(_metadata->env) == 0) { \
 			/* Use the same _metadata. */ \
@@ -399,8 +400,10 @@
 				_metadata->exit_code = KSFT_FAIL; \
 			} \
 		} \
+		else \
+			jmp = true; \
 		if (child == 0) { \
-			if (_metadata->setup_completed && !_metadata->teardown_parent) \
+			if (_metadata->setup_completed && !_metadata->teardown_parent && !jmp) \
 				fixture_name##_teardown(_metadata, &self, variant->data); \
 			_exit(0); \
 		} \
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] selftests/harness: Prevent infinite loop due to Assert in FIXTURE_TEARDOWN
  2024-03-14 22:43 [PATCH] selftests/harness: Prevent infinite loop due to Assert in FIXTURE_TEARDOWN Shengyu Li
  2024-03-24 21:44 ` Shengyu Li
@ 2024-03-26 19:28 ` Shuah Khan
  1 sibling, 0 replies; 5+ messages in thread
From: Shuah Khan @ 2024-03-26 19:28 UTC (permalink / raw)
  To: Shengyu Li, shuah; +Cc: luto, linux-kselftest, linux-kernel, wad, Shuah Khan

On 3/14/24 16:43, Shengyu Li wrote:
> This patch addresses an issue in the selftests/harness where an assertion within FIXTURE_TEARDOWN could trigger an infinite loop. The problem arises because the teardown procedure is meant to execute once, but the presence of failing assertions (ASSERT_EQ(0, 1)) leads to repeated attempts to execute teardown due to the long jump mechanism used by the harness for handling assertions.
> 
> To resolve this, the patch ensures that the teardown process runs only once, regardless of assertion outcomes, preventing the infinite loop and allowing tests to fail.
> 

Please make sure change log is formatted with 70-75 character
long lines.

Please include information on how you found this problem.

Send v2 with that change.

> Signed-off-by: Shengyu Li <shengyu.li.evgeny@gmail.com>
> ---
>   tools/testing/selftests/kselftest_harness.h | 5 ++++-
>   1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
> index 4fd735e48ee7..230d62884885 100644
> --- a/tools/testing/selftests/kselftest_harness.h
> +++ b/tools/testing/selftests/kselftest_harness.h
> @@ -383,6 +383,7 @@
>   		FIXTURE_DATA(fixture_name) self; \
>   		pid_t child = 1; \
>   		int status = 0; \
> +		bool jmp = false; \
>   		memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
>   		if (setjmp(_metadata->env) == 0) { \
>   			/* Use the same _metadata. */ \
> @@ -399,8 +400,10 @@
>   				_metadata->exit_code = KSFT_FAIL; \
>   			} \
>   		} \
> +		else \
> +			jmp = true; \
>   		if (child == 0) { \
> -			if (_metadata->setup_completed && !_metadata->teardown_parent) \
> +			if (_metadata->setup_completed && !_metadata->teardown_parent && !jmp) \
>   				fixture_name##_teardown(_metadata, &self, variant->data); \
>   			_exit(0); \
>   		} \

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] selftests/harness: Prevent infinite loop due to Assert in FIXTURE_TEARDOWN
  2024-03-24 21:44 ` Shengyu Li
@ 2024-03-26 21:13   ` Shengyu Li
  2024-04-04 16:47     ` Shuah Khan
  0 siblings, 1 reply; 5+ messages in thread
From: Shengyu Li @ 2024-03-26 21:13 UTC (permalink / raw)
  To: shuah; +Cc: linux-kernel, linux-kselftest, luto, wad, Shengyu Li

This patch addresses an issue in the selftests/harness where an 
assertion within FIXTURE_TEARDOWN could trigger an infinite loop. 
The problem arises because the teardown procedure is meant to 
execute once, but the presence of failing assertions (ASSERT_EQ(0, 1)) 
leads to repeated attempts to execute teardown due to 
the long jump mechanism used by the harness for handling assertions.

To resolve this, the patch ensures that the teardown process 
runs only once, regardless of assertion outcomes, preventing 
the infinite loop and allowing tests to fail.

A simple test demo(test.c):
 #include "kselftest_harness.h"

FIXTURE(f)
{
	int fd;
};

FIXTURE_SETUP(f)
{
	self->fd = 0;
}

FIXTURE_TEARDOWN(f)
{
	TH_LOG("TEARDOWN");
	ASSERT_EQ(0, 1);
	self->fd = -1;
}

TEST_F(f, open_close)
{
	ASSERT_NE(self->fd, 1);
}

TEST_HARNESS_MAIN

will always output the following output due to a dead loop until timeout:
 # test.c:15:open_close:TEARDOWN
 # test.c:16:open_close:Expected 0 (0) == 1 (1)
 # test.c:15:open_close:TEARDOWN
 # test.c:16:open_close:Expected 0 (0) == 1 (1)
 ...

But here's what we should and expect to get:
 TAP version 13
 1..1
 # Starting 1 tests from 2 test cases.
 #  RUN           f.open_close ...
 # test.c:15:open_close:TEARDOWN
 # test.c:16:open_close:Expected 0 (0) == 1 (1)
 # open_close: Test terminated by assertion
 #          FAIL  f.open_close
 not ok 1 f.open_close
 # FAILED: 0 / 1 tests passed.
 # Totals: pass:0 fail:1 xfail:0 xpass:0 skip:0 error:0

also this is related to the issue mentioned in this patch
https://patchwork.kernel.org/project/linux-kselftest/patch/e2ba3f8c-80e6-477d-9cea-1c9af820e0ed@alu.unizg.hr/



Signed-off-by: Shengyu Li <shengyu.li.evgeny@gmail.com>
---
 tools/testing/selftests/kselftest_harness.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
index 4fd735e48ee7..230d62884885 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -383,6 +383,7 @@
 		FIXTURE_DATA(fixture_name) self; \
 		pid_t child = 1; \
 		int status = 0; \
+		bool jmp = false; \
 		memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
 		if (setjmp(_metadata->env) == 0) { \
 			/* Use the same _metadata. */ \
@@ -399,8 +400,10 @@
 				_metadata->exit_code = KSFT_FAIL; \
 			} \
 		} \
+		else \
+			jmp = true; \
 		if (child == 0) { \
-			if (_metadata->setup_completed && !_metadata->teardown_parent) \
+			if (_metadata->setup_completed && !_metadata->teardown_parent && !jmp) \
 				fixture_name##_teardown(_metadata, &self, variant->data); \
 			_exit(0); \
 		} \
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] selftests/harness: Prevent infinite loop due to Assert in FIXTURE_TEARDOWN
  2024-03-26 21:13   ` [PATCH v2] " Shengyu Li
@ 2024-04-04 16:47     ` Shuah Khan
  0 siblings, 0 replies; 5+ messages in thread
From: Shuah Khan @ 2024-04-04 16:47 UTC (permalink / raw)
  To: Shengyu Li, shuah, Mirsad Todorovac, Jason Gunthorpe,
	Joao Martins
  Cc: linux-kernel, linux-kselftest, luto, wad, Shuah Khan

On 3/26/24 15:13, Shengyu Li wrote:
> This patch addresses an issue in the selftests/harness where an
> assertion within FIXTURE_TEARDOWN could trigger an infinite loop.
> The problem arises because the teardown procedure is meant to
> execute once, but the presence of failing assertions (ASSERT_EQ(0, 1))
> leads to repeated attempts to execute teardown due to
> the long jump mechanism used by the harness for handling assertions.
> 
> To resolve this, the patch ensures that the teardown process
> runs only once, regardless of assertion outcomes, preventing
> the infinite loop and allowing tests to fail.
> 
> A simple test demo(test.c):
>   #include "kselftest_harness.h"
> 
> FIXTURE(f)
> {
> 	int fd;
> };
> 
> FIXTURE_SETUP(f)
> {
> 	self->fd = 0;
> }
> 
> FIXTURE_TEARDOWN(f)
> {
> 	TH_LOG("TEARDOWN");
> 	ASSERT_EQ(0, 1);
> 	self->fd = -1;
> }
> 
> TEST_F(f, open_close)
> {
> 	ASSERT_NE(self->fd, 1);
> }
> 
> TEST_HARNESS_MAIN
> 
> will always output the following output due to a dead loop until timeout:
>   # test.c:15:open_close:TEARDOWN
>   # test.c:16:open_close:Expected 0 (0) == 1 (1)
>   # test.c:15:open_close:TEARDOWN
>   # test.c:16:open_close:Expected 0 (0) == 1 (1)
>   ...
> 
> But here's what we should and expect to get:
>   TAP version 13
>   1..1
>   # Starting 1 tests from 2 test cases.
>   #  RUN           f.open_close ...
>   # test.c:15:open_close:TEARDOWN
>   # test.c:16:open_close:Expected 0 (0) == 1 (1)
>   # open_close: Test terminated by assertion
>   #          FAIL  f.open_close
>   not ok 1 f.open_close
>   # FAILED: 0 / 1 tests passed.
>   # Totals: pass:0 fail:1 xfail:0 xpass:0 skip:0 error:0
> 
> also this is related to the issue mentioned in this patch
> https://patchwork.kernel.org/project/linux-kselftest/patch/e2ba3f8c-80e6-477d-9cea-1c9af820e0ed@alu.unizg.hr/
> 

Adding people who participated in the above thread discussion.
Please try this patch out to see if this fixes the problem
with iommu test.

> 
> 
> Signed-off-by: Shengyu Li <shengyu.li.evgeny@gmail.com>

Than you for finding and fixing this. Applied now to linux-kselftes fixes
branch for the next rc.

thanks,
-- Shuah




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-04-04 16:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-14 22:43 [PATCH] selftests/harness: Prevent infinite loop due to Assert in FIXTURE_TEARDOWN Shengyu Li
2024-03-24 21:44 ` Shengyu Li
2024-03-26 21:13   ` [PATCH v2] " Shengyu Li
2024-04-04 16:47     ` Shuah Khan
2024-03-26 19:28 ` [PATCH] " Shuah Khan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox