coredump01: New core_pattern specifiers test
PATCH v5 1/1 coredump01: New core_pattern specifiers test
From: Andrea Cervesato <[email protected]>
LTP has no coverage for the core_pattern specifiers expansion, neither for the pipe flavor of core_pattern, where the kernel spawns a user space helper and writes the core dump into its standard input.
The new test verifies that %e, %p and %s are expanded both in the core file name and in the arguments of a piped helper, checking also that the helper really receives an ELF core dump.
Root is required to rewrite the system wide core_pattern, which is restored by the test library on all exit paths.
---
Add a simple coredump01 test that simulate a coredump and verify that it actually works. This is a small coverage that is missing inside LTP testing suite.
---
Changes in v5:
- resolve path from root for helper
- Link to v4: https://lore.kernel.org/[email protected]
Changes in v4:
- read back core_pattern after writing and abort with TBROK on mismatch
- read CONFIG_STATIC_USERMODEHELPER once in setup() and cache the result
- remove namespace visibility scaffolding and skip logic from verify_pipe_pattern()
- resolve helper path with tst_get_path() instead of /proc/self/exe readlink
- Link to v3: https://lore.kernel.org/[email protected]
Changes in v3:
- check for CONFIG_COREDUMP=y
- Link to v2: https://lore.kernel.org/[email protected]
Changes in v2:
- fix CI agent spotted errors
- Link to v1: https://lore.kernel.org/[email protected]
--- runtest/kernel_misc | 1 + testcases/kernel/Makefile | 1 + testcases/kernel/coredump/.gitignore | 2 + testcases/kernel/coredump/Makefile | 7 + testcases/kernel/coredump/coredump01.c | 186 ++++++++++++++++++++++++++ testcases/kernel/coredump/coredump01_helper.c | 66 +++++++++ 6 files changed, 263 insertions(+)
runtest/kernel_misc +1 -0
@@ -17,3 +17,4 @@ zram02 zram02.sh zram03 zram03 umip_basic_test umip_basic_test aslr01 aslr01 +coredump01 coredump01
testcases/kernel/Makefile +1 -0
@@ -19,6 +19,7 @@ SUBDIRS += lib SUBDIRS += connectors \ containers \ controllers \ + coredump \ crypto \ device-drivers \ firmware \
testcases/kernel/coredump/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +/coredump01 +/coredump01_helper
testcases/kernel/coredump/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2026 Linux Test Project + +top_srcdir ?= ../../.. + +include $(top_srcdir)/include/mk/testcases.mk +include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/coredump/coredump01.c new file +186 -0
186 lines not on this page · raw message
testcases/kernel/coredump/coredump01_helper.c new file +66 -0
66 lines not on this page · raw message
---
base-commit: 12724413534a6d4160ff9694ba6f09daa4ccb6bd
change-id: 20260820-coredump-22fc731bde08
Best regards,
Signature · 5 lines
Andrea Cervesato <[email protected]> -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Andrea,
Quoted 1 line · Andrea Cervesato
coredump01: New
core_patternspecifiers test
Verdict - Reviewed
---
Note:
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Andrea,
Quoted 5 lines
testcases/kernel/coredump/coredump01.c new file +5
-0+static void verify_file_pattern (void ) +{ +char dump[PATH_MAX +32 ]; +pid_t pid; +
testcases/kernel/coredump/coredump01.c line 102
Quoted 10 lines
testcases/kernel/coredump/coredump01.c new file +10
-0+tst_res (TINFO ,"Testing file core_pattern" ); + +set_pattern ("%s/core.%%e.%%p.%%s" , cwd); + + pid =crash_child (); + +snprintf (dump,sizeof (dump),"%s/core.coredump01.%d.%d" , cwd, pid,SIGABRT ); + +TST_EXP_PASS (access (dump,F_OK ),"core.%%e.%%p.%%s expanded to core.coredump01.%d.%d" , + pid,SIGABRT );
Generally this checks is really nice, but it'd be better to add more checks for the core dump file?
e.g. file size > 0, ELF magic valid, and type == ET_CORE.
FYI:
+static int verify_core_dump(const char *path, long long expected_size) +{
+ int fd; + struct stat st; + unsigned char magic[SELFMAG]; + Elf64_Ehdr ehdr;
+
+ if (access(path, F_OK) != 0) {
+ tst_res(TFAIL, "Core dump file %s does not exist", path);
+ return -1;
+ }
+ tst_res(TPASS, "Core dump file %s exists", path);
+
+ SAFE_STAT(path, &st);
+ TST_EXP_EXPR(st.st_size > 0, "core file size = %ld bytes", st.st_size);
+
+ if (expected_size > 0 && st.st_size != expected_size) {
+ tst_res(TFAIL, "Core dump size mismatch: expected %lld bytes, got %ld",
+ expected_size, st.st_size);
+ return -1;
+ }
+
+ fd = SAFE_OPEN(path, O_RDONLY, 0); + SAFE_READ(0, fd, magic, SELFMAG); + SAFE_CLOSE(fd);
+
+ TST_EXP_EXPR(!memcmp(magic, ELFMAG, 4),
+ "core file has valid ELF magic");
+
+ fd = SAFE_OPEN(path, O_RDONLY, 0); + SAFE_READ(0, fd, &ehdr, sizeof(ehdr)); + SAFE_READ(0, fd, &ehdr, sizeof(ehdr)); + SAFE_CLOSE(fd);
+
+ TST_EXP_EXPR(ehdr.e_type == ET_CORE,
+ "ELF type is ET_CORE (got %d)", ehdr.e_type);
+
+ return 0;
+}
+
static void verify_file_pattern(void)
{
char dump[PATH_MAX + 32];@@ -100,11 +140,14 @@ static void verify_file_pattern(void)
TST_EXP_PASS(access(dump, F_OK), "core.%%e.%%p.%%s expanded to core.coredump01.%d.%d",
pid, SIGABRT);+
+ verify_core_dump(dump, -1);
}
static void verify_pipe_pattern(void)
{
char res[PATH_MAX + 32], exe[PATH_MAX];
+ char dump[PATH_MAX + 32];
int pid_seen, sig_seen, elf;
long long bytes;
pid_t pid;
@@ -121,6 +164,7 @@ static void verify_pipe_pattern(void)
pid = crash_child();
snprintf(res, sizeof(res), "%s/res.%d", cwd, pid);+ snprintf(dump, sizeof(dump), "%s/res.%d.core", cwd, pid);
/* the kernel spawns the helper asynchronously */
if (TST_RETRY_FN_EXP_BACKOFF(access(res, F_OK), TST_RETVAL_EQ0, HELPER_TIMEOUT)) {
@@ -135,6 +179,8 @@ static void verify_pipe_pattern(void)
TST_EXP_EQ_LI(pid_seen, pid);
TST_EXP_EQ_LI(sig_seen, SIGABRT);
TST_EXP_EXPR(elf && bytes > 0, "%s read %lli bytes of ELF core dump", HELPER, bytes);+
+ verify_core_dump(dump, bytes);
}
Quoted 23 lines
testcases/kernel/coredump/coredump01.c new file +23
-0+static void verify_pipe_pattern (void ) +{ +char res[PATH_MAX +32 ], exe[PATH_MAX ]; +int pid_seen, sig_seen, elf; +long long bytes; +pid_t pid; + +if (static_usermodehelper) { +tst_res (TCONF ,"CONFIG_STATIC_USERMODEHELPER is enabled, skipping pipe core_pattern" ); +return ; + } + +tst_res (TINFO ,"Testing pipe core_pattern" ); + +set_pattern ("|%s %%e %%p %%s %s/res.%%p" , helper_path, cwd); + + pid =crash_child (); + +snprintf (res,sizeof (res),"%s/res.%d" , cwd, pid); + +/* the kernel spawns the helper asynchronously */ +if (TST_RETRY_FN_EXP_BACKOFF (access (res,F_OK ),TST_RETVAL_EQ0 ,HELPER_TIMEOUT )) { +tst_res (TFAIL ,"%s did not report any core dump" ,HELPER );
testcases/kernel/coredump/coredump01.c line 137
Quoted 10 lines
testcases/kernel/coredump/coredump01.c new file +10
-0+return ; + } + +SAFE_FILE_SCANF (res,"exe=%15s pid=%d sig=%d bytes=%lld elf=%d" , + exe, &pid_seen, &sig_seen, &bytes, &elf); + +TST_EXP_EQ_STR (exe,"coredump01" ); +TST_EXP_EQ_LI (pid_seen, pid); +TST_EXP_EQ_LI (sig_seen,SIGABRT ); +TST_EXP_EXPR (elf && bytes >0 ,"%s read %lli bytes of ELF core dump" ,HELPER , bytes);
Here as well.
Also we need do something in the helper to save dump file.
Quoted 1 line
+}
Signature · 5 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Li,
Quoted 7 lines
testcases/kernel/coredump/coredump01.c line 102
Quoted 2 lines
testcases/kernel/coredump/coredump01.c new file +2
-0+TST_EXP_PASS (access (dump,F_OK ),"core.%%e.%%p.%%s expanded to core.coredump01.%d.%d" , + pid,SIGABRT );Generally this checks is really nice, but it'd be better to add more checks for the core dump file?
e.g. file size > 0, ELF magic valid, and type ==
ET_CORE.
this is already done, but we can introduce ELF type check (read below).
Review
Quoted 9 lines
FYI:
(quoted diff) +6
-0+static int verify_core_dump(const char *path, long long expected_size) +{ + int fd; + struct stat st; + unsigned char magic[SELFMAG]; + Elf64_Ehdr ehdr;
Ehdr is not portable on 32 bit systems breaking our testing, so I wouldn't use it in this case. We need Elf32_Half which is in both 32bit and 64bit.
Quoted 16 lines
(quoted diff) +16
-0+ + if (access(path, F_OK) != 0) { + tst_res(TFAIL, "Core dump file %s does not exist", path); + return -1; + } + tst_res(TPASS, "Core dump file %s exists", path); + + SAFE_STAT(path, &st); + TST_EXP_EXPR(st.st_size > 0, "core file size = %ld bytes", st.st_size); + + if (expected_size > 0 && st.st_size != expected_size) { + tst_res(TFAIL, "Core dump size mismatch: expected %lld bytes, got %ld", + expected_size, st.st_size); + return -1; + } +
Review
Quoted 10 lines
(quoted diff) +10
-0+ fd = SAFE_OPEN(path, O_RDONLY, 0); + SAFE_READ(0, fd, magic, SELFMAG); + SAFE_CLOSE(fd); + + TST_EXP_EXPR(!memcmp(magic, ELFMAG, 4), + "core file has valid ELF magic"); + + fd = SAFE_OPEN(path, O_RDONLY, 0); + SAFE_READ(0, fd, &ehdr, sizeof(ehdr)); + SAFE_READ(0, fd, &ehdr, sizeof(ehdr));
double read? I guess it's an error
I will send a new version with some of the proposed checks
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 6 lines
Quoted 4 lines
Generally this checks is really nice, but it'd be better to add more checks for the core dump file?
e.g. file size > 0, ELF magic valid, and type ==
ET_CORE.this is already done, but we can introduce ELF type check (read below).
+1
Quoted 10 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+static int verify_core_dump(const char *path, long long expected_size) +{ + int fd; + struct stat st; + unsigned char magic[SELFMAG]; + Elf64_Ehdr ehdr;Ehdr is not portable on 32 bit systems breaking our testing, so I wouldn't use it in this case. We need
Elf32_Halfwhich is in both 32bit and 64bit.
+1
Quoted 5 lines
Quoted 5 lines
(quoted diff) +5
-0+ + fd = SAFE_OPEN(path, O_RDONLY, 0); + SAFE_READ(0, fd, magic, SELFMAG); + SAFE_CLOSE(fd); +
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ TST_EXP_EXPR(!memcmp(magic, ELFMAG, 4), + "core file has valid ELF magic"); + + fd = SAFE_OPEN(path, O_RDONLY, 0); + SAFE_READ(0, fd, &ehdr, sizeof(ehdr)); + SAFE_READ(0, fd, &ehdr, sizeof(ehdr));double read? I guess it's an error
Oh sorry, that's a duplicate from the vim y&p.
Quoted 1 line
I will send a new version with some of the proposed checks
Thanks!
Signature · 5 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp