Skip to contentSkip to conversation

selftests: coredump: Some bug fixes

PATCH Superseded Nam Cao 2 participants 6 messages Mar 31, 2025 – Apr 1, 2025 Kselftest

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Nam Cao

author
#0 lore raw

cover selftests: coredump: Some bug fixes

Hi,

While trying the coredump test on qemu-system-riscv64, I observed test failures for various reasons.

This series makes the test works on qemu-system-riscv64.

Best regards,
Nam

Nam Cao (3):
selftests: coredump: Properly initialize pointer
selftests: coredump: Use waitpid() instead of busy-wait
selftests: coredump: Raise timeout to 2 minutes

 tools/testing/selftests/coredump/stackdump      |  6 +-----
 .../testing/selftests/coredump/stackdump_test.c | 17 +++++++++--------
 2 files changed, 10 insertions(+), 13 deletions(-)
Signature · 1 line
2.39.5
author
#1 lore raw

PATCH 1/3 selftests: coredump: Properly initialize pointer

The buffer pointer "line" is not initialized. This pointer is passed to getline().

It can still work if the stack is zero-initialized, because getline() can work with a NULL pointer as buffer.

But this is obviously broken. This bug shows up while running the test on a riscv64 machine.

Fix it by properly initializing the pointer.

Fixes 15858da53542 ("selftests: coredump: Add stackdump test")
---
 tools/testing/selftests/coredump/stackdump_test.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/coredump/stackdump_test.c +2 -0
index 137b2364a082..1dc54e128586 100644
@@ -100,6 +100,8 @@ TEST_F(coredump, stackdump)
 	FILE *file;
 	pid_t pid;
 
+	line = NULL;
+
 	/*
 	 * Step 1: Setup core_pattern so that the stackdump script is executed when the child
 	 * process crashes
Signature · 1 line
2.39.5
author
#3 lore raw

PATCH 2/3 selftests: coredump: Use waitpid() instead of busy-wait

In reply to Nam Cao

The test waits for coredump to finish by busy-waiting for the stackdump_values file to be created. The maximum wait time is 10 seconds.

This doesn't work for slow machine (qemu-system-riscv64), because coredump takes longer.

Switch to use waitpid().

With this, the stack_values file doesn't need to be atomically written by coredump anymore, therefore simplify the stackdump script.

Fixes 15858da53542 ("selftests: coredump: Add stackdump test")
---
 tools/testing/selftests/coredump/stackdump        |  6 +-----
 tools/testing/selftests/coredump/stackdump_test.c | 13 ++++++-------
 2 files changed, 7 insertions(+), 12 deletions(-)
tools/testing/selftests/coredump/stackdump +1 -5
index 96714ce42d12..ad487fd5ff15 100755
@@ -4,11 +4,7 @@
 CRASH_PROGRAM_ID=$1
 STACKDUMP_FILE=$2
 
-TMP=$(mktemp)
-
 for t in /proc/$CRASH_PROGRAM_ID/task/*; do
 	tid=$(basename $t)
-	cat /proc/$tid/stat | awk '{print $29}' >> $TMP
+	cat /proc/$tid/stat | awk '{print $29}' >> $STACKDUMP_FILE
 done
-
-mv $TMP $STACKDUMP_FILE
tools/testing/selftests/coredump/stackdump_test.c +6 -7
index 1dc54e128586..733feaa0f895 100644
@@ -96,7 +96,7 @@ TEST_F(coredump, stackdump)
 	char *test_dir, *line;
 	size_t line_length;
 	char buf[PATH_MAX];
-	int ret, i;
+	int ret, i, status;
 	FILE *file;
 	pid_t pid;
 
@@ -131,12 +131,11 @@ TEST_F(coredump, stackdump)
 	/*
 	 * Step 3: Wait for the stackdump script to write the stack pointers to the stackdump file
 	 */
-	for (i = 0; i < 10; ++i) {
-		file = fopen(STACKDUMP_FILE, "r");
-		if (file)
-			break;
-		sleep(1);
-	}
+	waitpid(pid, &status, 0);
+	ASSERT_TRUE(WIFSIGNALED(status));
+	ASSERT_TRUE(WCOREDUMP(status));
+
+	file = fopen(STACKDUMP_FILE, "r");
 	ASSERT_NE(file, NULL);
 
 	/* Step 4: Make sure all stack pointer values are non-zero */
Signature · 1 line
2.39.5
author
#5 lore raw

PATCH 3/3 selftests: coredump: Raise timeout to 2 minutes

In reply to Nam Cao

The test's runtime (nearly 20s) is dangerously close to the limit (30s) on qemu-system-riscv64:

$ time ./stackdump_test > /dev/null
real	0m19.210s
user	0m0.077s
sys	0m0.359s

There could be machines slower than qemu-system-riscv64. Therefore raise the test timeout to 2 minutes to be safe.

Fixes 15858da53542 ("selftests: coredump: Add stackdump test")
---
 tools/testing/selftests/coredump/stackdump_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/coredump/stackdump_test.c +1 -1
index 733feaa0f895..b1924acf02af 100644
@@ -89,7 +89,7 @@ FIXTURE_TEARDOWN(coredump)
 	fprintf(stderr, "Failed to cleanup stackdump test: %s\n", reason);
 }
 
-TEST_F(coredump, stackdump)
+TEST_F_TIMEOUT(coredump, stackdump, 120)
 {
 	struct sigaction action = {};
 	unsigned long long stack;
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH 1/3] selftests: coredump: Properly initialize pointer

In reply to Nam Cao
Quoted 2 lines · Nam Cao

The buffer pointer "line" is not initialized. This pointer is passed to getline().

Ouch.

Quoted 19 lines

It can still work if the stack is zero-initialized, because getline() can work with a NULL pointer as buffer.

But this is obviously broken. This bug shows up while running the test on a riscv64 machine.

Fix it by properly initializing the pointer.

Fixes 15858da53542 ("selftests: coredump: Add stackdump test")
---
 tools/testing/selftests/coredump/stackdump_test.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/coredump/stackdump_test.c +0 -0
index 137b2364a082..1dc54e128586 100644

tools/testing/selftests/coredump/stackdump_test.c TEST_F(coredump, stackdump) · line 103

Quoted 4 lines
tools/testing/selftests/coredump/stackdump_test.c +1 -0
@@ -100,6 +100,8 @@ TEST_F(coredump, stackdump)
 	FILE *file;
 	pid_t pid;
 
+	line = NULL;

The syntax of getline(3) is quite interesting, since it allocates/reallocates/uses the lineptr as needed and possibly requires the application to free the data. I recommend moving the initialization down to the getline() call and also add the corresponding free().

Something like this:

tools/testing/selftests/coredump/stackdump_test.c +2 -0
index 137b2364a082..c23cf95c3f6d 100644
@@ -138,10 +138,12 @@ TEST_F(coredump, stackdump)
 	ASSERT_NE(file, NULL);
 
 	/* Step 4: Make sure all stack pointer values are non-zero */
+	line = NULL;
 	for (i = 0; -1 != getline(&line, &line_length, file); ++i) {
 		stack = strtoull(line, NULL, 10);
 		ASSERT_NE(stack, 0);
 	}
+	free(line);
 
 	ASSERT_EQ(i, 1 + NUM_THREAD_SPAWN);
 

Because of how getline() works, technically your patch is good enough. But we should probably excercise more precision in the use of getline() so as to set a good example.

John Ogness

#4 lore raw

Re: [PATCH 2/3] selftests: coredump: Use waitpid() instead of busy-wait

In reply to Nam Cao
Quoted 7 lines · Nam Cao

The test waits for coredump to finish by busy-waiting for the stackdump_values file to be created. The maximum wait time is 10 seconds.

This doesn't work for slow machine (qemu-system-riscv64), because coredump takes longer.

Switch to use waitpid().

Note that you are now assuming that returning from waitpid() means that:

1. the coredumping has completed

and

2. the STACKDUMP_FILE with all its contents are visible to the parent process

Quoted 17 lines

With this, the stack_values file doesn't need to be atomically written by coredump anymore, therefore simplify the stackdump script.

Fixes 15858da53542 ("selftests: coredump: Add stackdump test")
---
 tools/testing/selftests/coredump/stackdump        |  6 +-----
 tools/testing/selftests/coredump/stackdump_test.c | 13 ++++++-------
 2 files changed, 7 insertions(+), 12 deletions(-)
tools/testing/selftests/coredump/stackdump +0 -0
index 96714ce42d12..ad487fd5ff15 100755
@@ -4,11 +4,7 @@
 CRASH_PROGRAM_ID=$1
 STACKDUMP_FILE=$2

tools/testing/selftests/coredump/stackdump line 14

Quoted 10 lines
tools/testing/selftests/coredump/stackdump +1 -5
 
-TMP=$(mktemp)
-
 for t in /proc/$CRASH_PROGRAM_ID/task/*; do
 	tid=$(basename $t)
-	cat /proc/$tid/stat | awk '{print $29}' >> $TMP
+	cat /proc/$tid/stat | awk '{print $29}' >> $STACKDUMP_FILE
 done
-
-mv $TMP $STACKDUMP_FILE

I would leave this as it was. Then the availability of STACKDUMP_FILE means the full contents are available.

Quoted 16 lines
tools/testing/selftests/coredump/stackdump_test.c +1 -1
index 1dc54e128586..733feaa0f895 100644
@@ -96,7 +96,7 @@ TEST_F(coredump, stackdump)
 	char *test_dir, *line;
 	size_t line_length;
 	char buf[PATH_MAX];
-	int ret, i;
+	int ret, i, status;
 	FILE *file;
 	pid_t pid;
 
@@ -131,12 +131,11 @@ TEST_F(coredump, stackdump)
 	/*
 	 * Step 3: Wait for the stackdump script to write the stack pointers to the stackdump file

tools/testing/selftests/coredump/stackdump_test.c TEST_F(coredump, stackdump) · line 136

Quoted 10 lines
tools/testing/selftests/coredump/stackdump_test.c +3 -6
 	 */
-	for (i = 0; i < 10; ++i) {
-		file = fopen(STACKDUMP_FILE, "r");
-		if (file)
-			break;
-		sleep(1);
-	}
+	waitpid(pid, &status, 0);
+	ASSERT_TRUE(WIFSIGNALED(status));
+	ASSERT_TRUE(WCOREDUMP(status));

Why not just put these 3 lines above the for-loop? So you would wait for the process to end, then go into the 20-second timeout loop waiting for STACKDUMP_FILE to show up.

John Ogness