Skip to contentSkip to conversation

Fix segfaults related to missing BTF support

PATCH In review Arnaldo Carvalho de Melo 3 participants 12 messages Nov 19, 2024

lore
Share & tools
lore
#0 lore raw

cover Fix segfaults related to missing BTF support

Sorry for the top post: Only the cover letter ended up reaching the mailing list, so resending to have it there :-\

Hi,

While looking for reports to fix before release 1.28 I got to
Matthias reports about segfaults in systems where BTF isn't present, so I introduced a regression test and infrastructure to allow testing handling such a system, please take a look.

Thanks,

- Arnaldo

Arnaldo Carvalho de Melo (5):
core: Add method to get the vmlinux BTF filename, allow overriding it via env var tests default_vmlinux_btf: Introduce test for using BTF by default
pahole: Honour exclusive BTF loading
tests default_vmlinux_btf: Cover the no args segfault too
core, libctf: Check if constructor arguments are NULL before using them

 dwarves.c                    | 33 ++++++++++++++++++++++++++++++---
 dwarves.h                    |  2 ++
 libctf.c                     |  3 +++
 man-pages/pahole.1           |  4 ++++
 pahole.c                     | 11 +++++++++--
 tests/default_vmlinux_btf.sh | 32 ++++++++++++++++++++++++++++++++
 6 files changed, 80 insertions(+), 5 deletions(-)
 create mode 100755 tests/default_vmlinux_btf.sh
Signature · 1 line
2.47.0
#1 lore raw

PATCH 1/5 core: Add method to get the vmlinux BTF filename, allow overriding it via env var

From: Arnaldo Carvalho de Melo <[email protected]>

In systems where BTF isn't available there were reports that the simplest pahole call, without any args, segfaults.

To have a proper test before fixing this problem, allow overriding the /sys/kernel/btf/vmlinux filename, so that even in systems with BTF we an point it to a invalid location, making pahole think that there is no BTF available and thus fallback to something that currently segfaults.

Using it:

  $ pahole list_head
  struct list_head {
  	struct list_head *         next;                 /*     0     8 */
  	struct list_head *         prev;                 /*     8     8 */
  	/* size: 16, cachelines: 1, members: 2 */
  	/* last cacheline: 16 bytes */
  };
  $
  $ PAHOLE_VMLINUX_BTF_FILENAME=foobar pahole list_head
  Segmentation fault (core dumped)
  $
Cc Alan Maguire <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Eduard Zingerman <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
---
 dwarves.c          | 17 +++++++++++++++--
 dwarves.h          |  2 ++
 man-pages/pahole.1 |  4 ++++
 pahole.c           |  2 +-
 4 files changed, 22 insertions(+), 3 deletions(-)
dwarves.c +15 -2
index 14ba4f0c6dd7f90f..5b51b55191177e21 100644
@@ -2807,6 +2807,19 @@ static int filename__sprintf_build_id(const char *pathname, char *sbuild_id)
 static int vmlinux_path__nr_entries;
 static char **vmlinux_path;
 
+const char *vmlinux_path__btf_filename(void)
+{
+	static const char *vmlinux_btf;
+
+	if (vmlinux_btf == NULL) {
+		vmlinux_btf = getenv("PAHOLE_VMLINUX_BTF_FILENAME");
+		if (vmlinux_btf == NULL)
+			vmlinux_btf = "/sys/kernel/btf/vmlinux";
+	}
+
+	return vmlinux_btf;
+}
+
 static void vmlinux_path__exit(void)
 {
 	while (--vmlinux_path__nr_entries >= 0)
@@ -2933,7 +2946,7 @@ static int cus__load_running_kernel(struct cus *cus, struct conf_load *conf)
 	int err = 0;
 
 	if ((!conf || conf->format_path == NULL || strncmp(conf->format_path, "btf", 3) == 0) &&
-	    access("/sys/kernel/btf/vmlinux", R_OK) == 0) {
+	    access(vmlinux_path__btf_filename(), R_OK) == 0) {
 		int loader = debugging_formats__loader("btf");
 		if (loader == -1)
 			goto try_elf;
@@ -2941,7 +2954,7 @@ static int cus__load_running_kernel(struct cus *cus, struct conf_load *conf)
 		if (conf && conf->conf_fprintf)
 			conf->conf_fprintf->has_alignment_info = debug_fmt_table[loader]->has_alignment_info;
 
-		if (debug_fmt_table[loader]->load_file(cus, conf, "/sys/kernel/btf/vmlinux") == 0)
+		if (debug_fmt_table[loader]->load_file(cus, conf, vmlinux_path__btf_filename()) == 0)
 			return 0;
 	}
 try_elf:
dwarves.h +2 -0
index 49988f1cf9d45be8..1cb0d629f5265523 100644
@@ -1628,6 +1628,8 @@ void dwarves__resolve_cacheline_size(const struct conf_load *conf, uint16_t user
 
 const char *dwarf_tag_name(const uint32_t tag);
 
+const char *vmlinux_path__btf_filename(void);
+
 const char *vmlinux_path__find_running_kernel(void);
 
 struct argp_state;
man-pages/pahole.1 +4 -0
index bd28259b65429acc..39e7b465c64e7e2c 100644
@@ -75,6 +75,10 @@ struct list_head {
 };
 $
 .fi
+It is possible to override the /sys/kernel/btf/vmlinux file location by setting
+the PAHOLE_VMLINUX_BTF_FILENAME environment variable. This may be useful for
+testing, scripting when using a different BTF for vmlinux. Used in the pahole
+regression tests.
 
 If BTF is not present and no file is passed, then a vmlinux that matches the
 build-id for the running kernel will be looked up in the usual places,
pahole.c +1 -1
index 55d04cf82a3da683..b94cb1a979a6923d 100644
@@ -3754,7 +3754,7 @@ try_sole_arg_as_class_names:
 		if (filename &&
 		    strstarts(filename, "/sys/kernel/btf/") &&
 		    strstr(filename, "/vmlinux") == NULL) {
-			base_btf_file = "/sys/kernel/btf/vmlinux";
+			base_btf_file = vmlinux_path__btf_filename();
 			conf_load.base_btf = btf__parse(base_btf_file, NULL);
 			if (libbpf_get_error(conf_load.base_btf)) {
 				fprintf(stderr, "Failed to parse base BTF '%s': %ld\n",
Signature · 1 line
2.47.0
#2 lore raw

PATCH 2/5 tests default_vmlinux_btf: Introduce test for using BTF by default

In reply to Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

On a system without any debugging info or when one specifies BTF as the only BTF info desired but no BTF is available (or invalidated using PAHOLE_VMLINUX_BTF_FILENAME to an invalid/non-existent file), we're getting a segfault:

  root@x1:/home/acme/git/pahole# export PAHOLE_VMLINUX_BTF_FILENAME=non-existent
  root@x1:/home/acme/git/pahole# pahole --running_kernel_vmlinux
  pahole: couldn't find a vmlinux that matches the running kernel
  HINT: Maybe you're inside a container or missing a debuginfo package?
  root@x1:/home/acme/git/pahole# pahole
  Segmentation fault (core dumped)
  root@x1:/home/acme/git/pahole#

So add a test that checks for that before we fix it:

  root@x1:/home/acme/git/pahole# tests/default_vmlinux_btf.sh
  Default BTF on a system without BTF: FAILED
  root@x1:/home/acme/git/pahole#
Cc Alan Maguire <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Eduard Zingerman <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
---
 tests/default_vmlinux_btf.sh | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100755 tests/default_vmlinux_btf.sh
tests/default_vmlinux_btf.sh new file +21 -0
new file mode 100755
index 0000000000000000..a9effa2d6d37e0ee
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+echo -n "Default BTF on a system without BTF: "
+
+ulimit -c 0
+
+# To suppress the "Segmentation fault core dumped" message in bash we
+# pipe it to some other command, if it segfaults it will not produce any
+# lines and thus we can infer from the number of lines that the segfault
+# took place, tricky, but couldn't find any other way to check this
+# while suppressing the core dumped message. -acme
+
+nr_lines=$(PAHOLE_VMLINUX_BTF_FILENAME=foobar pahole -F btf list_head 2>&1 | wc -l)
+
+if [ $nr_lines -eq 0 ] ; then
+	echo "FAILED"
+	exit 1
+fi
+
+echo "Ok"
+exit 0
Signature · 1 line
2.47.0
#3 lore raw

PATCH 3/5 pahole: Honour exclusive BTF loading

In reply to Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

When we specify 'btf' and BTF info is available in the system, in the /sys/kernel/btf/vmlinux file, pahole will find it and use it to get the type information asked for:

  root@x1:~# perf trace -o /tmp/output -e openat pahole -F btf list_head ; tail -1 /tmp/output
  struct list_head {
  	struct list_head *         next;                 /*     0     8 */
  	struct list_head *         prev;                 /*     8     8 */
  	/* size: 16, cachelines: 1, members: 2 */
  	/* last cacheline: 16 bytes */
  };
       1.579 ( 0.010 ms): pahole/764777 openat(dfd: CWD, filename: "/sys/kernel/btf/vmlinux", flags: RDONLY|CLOEXEC) = 3

root@x1:~#

But, if the system doesn't have BTF info, which we now can simulate by using an environment variable it is tryng DWARF after failing for BTF, which isn't what the user asked for having specified just btf in the -F pahole command line:

  root@x1:~# export PAHOLE_VMLINUX_BTF_FILENAME=non-existent
  root@x1:~# perf trace -o /tmp/output -e openat,access pahole -F btf list_head ; tail -12 /tmp/output
  struct list_head {
  	struct list_head *         next;                 /*     0     8 */
  	struct list_head *         prev;                 /*     8     8 */
  	/* size: 16, cachelines: 1, members: 2 */
  	/* last cacheline: 16 bytes */
  };

1.643 ( 0.003 ms): pahole/765220 access(filename: "list_head", mode: R) = -1 ENOENT (No such file or directory)
1.658 ( 0.002 ms): pahole/765220 access(filename: "non-existent", mode: R) = -1 ENOENT (No such file or directory)
1.697 ( 0.018 ms): pahole/765220 openat(dfd: CWD, filename: "/sys/kernel/notes") = 3
1.801 ( 0.004 ms): pahole/765220 openat(dfd: CWD, filename: "vmlinux") = -1 ENOENT (No such file or directory)
1.807 ( 0.005 ms): pahole/765220 openat(dfd: CWD, filename: "/boot/vmlinux") = -1 ENOENT (No such file or directory)
1.927 ( 0.005 ms): pahole/765220 openat(dfd: CWD, filename: "/usr/lib/debug/lib/modules/6.11.6-200.fc40.x86_64/vmlinux", flags: RDONLY|CLOEXEC) = 3 root@x1:~#

So honour the request and fails when just BTF is asked and no BTF file is found:

  # export PAHOLE_VMLINUX_BTF_FILENAME=non-existent
  # pahole -F btf list_head
  pahole: couldn't find any btf debug information on this system.
  #
Cc Alan Maguire <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Eduard Zingerman <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
---
 dwarves.c | 15 +++++++++++++--
 pahole.c  |  9 ++++++++-
 2 files changed, 21 insertions(+), 3 deletions(-)
dwarves.c +13 -2
index 5b51b55191177e21..459311e64c8b45b2 100644
@@ -2944,9 +2944,17 @@ const char *vmlinux_path__find_running_kernel(void)
 static int cus__load_running_kernel(struct cus *cus, struct conf_load *conf)
 {
 	int err = 0;
+	bool btf_only = false;
 
-	if ((!conf || conf->format_path == NULL || strncmp(conf->format_path, "btf", 3) == 0) &&
-	    access(vmlinux_path__btf_filename(), R_OK) == 0) {
+	if (!conf || conf->format_path == NULL)
+		goto try_btf;
+
+	if (!strstr(conf->format_path, "btf"))
+		goto try_elf;
+
+	btf_only = strcmp(conf->format_path, "btf") == 0;
+try_btf:
+	if (access(vmlinux_path__btf_filename(), R_OK) == 0) {
 		int loader = debugging_formats__loader("btf");
 		if (loader == -1)
 			goto try_elf;
@@ -2958,6 +2966,9 @@ static int cus__load_running_kernel(struct cus *cus, struct conf_load *conf)
 			return 0;
 	}
 try_elf:
+	if (btf_only)
+		return -1;
+
 	elf_version(EV_CURRENT);
 	vmlinux_path__init();
 
pahole.c +8 -1
index b94cb1a979a6923d..fa5d8c707bd4717d 100644
@@ -3782,7 +3782,14 @@ try_sole_arg_as_class_names:
 			remaining = argc;
 			goto try_sole_arg_as_class_names;
 		}
-		cus__fprintf_load_files_err(cus, "pahole", argv + remaining, err, stderr);
+		if (argv[remaining] != NULL) {
+			cus__fprintf_load_files_err(cus, "pahole", argv + remaining, err, stderr);
+		} else {
+			fprintf(stderr, "pahole: couldn't find any%s%s debug information on this system.\n",
+					conf_load.format_path ? " " : "",
+					conf_load.format_path ?: "");
+		}
+
 		goto out_cus_delete;
 	}
 
Signature · 1 line
2.47.0
Reviewed-by Tested-by on v1 3/5
#4 lore raw

Re: [PATCH 3/5] pahole: Honour exclusive BTF loading

Quoted 48 lines · Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

When we specify 'btf' and BTF info is available in the system, in the /sys/kernel/btf/vmlinux file, pahole will find it and use it to get the type information asked for:

  root@x1:~# perf trace -o /tmp/output -e openat pahole -F btf list_head ; tail -1 /tmp/output
  struct list_head {
  	struct list_head *         next;                 /*     0     8 */
  	struct list_head *         prev;                 /*     8     8 */
  	/* size: 16, cachelines: 1, members: 2 */
  	/* last cacheline: 16 bytes */
  };
       1.579 ( 0.010 ms): pahole/764777 openat(dfd: CWD, filename: "/sys/kernel/btf/vmlinux", flags: RDONLY|CLOEXEC) = 3

root@x1:~#

But, if the system doesn't have BTF info, which we now can simulate by using an environment variable it is tryng DWARF after failing for BTF, which isn't what the user asked for having specified just btf in the -F pahole command line:

  root@x1:~# export PAHOLE_VMLINUX_BTF_FILENAME=non-existent
  root@x1:~# perf trace -o /tmp/output -e openat,access pahole -F btf list_head ; tail -12 /tmp/output
  struct list_head {
  	struct list_head *         next;                 /*     0     8 */
  	struct list_head *         prev;                 /*     8     8 */
  	/* size: 16, cachelines: 1, members: 2 */
  	/* last cacheline: 16 bytes */
  };

1.643 ( 0.003 ms): pahole/765220 access(filename: "list_head", mode: R) = -1 ENOENT (No such file or directory)
1.658 ( 0.002 ms): pahole/765220 access(filename: "non-existent", mode: R) = -1 ENOENT (No such file or directory)
1.697 ( 0.018 ms): pahole/765220 openat(dfd: CWD, filename: "/sys/kernel/notes") = 3
1.801 ( 0.004 ms): pahole/765220 openat(dfd: CWD, filename: "vmlinux") = -1 ENOENT (No such file or directory)
1.807 ( 0.005 ms): pahole/765220 openat(dfd: CWD, filename: "/boot/vmlinux") = -1 ENOENT (No such file or directory)
1.927 ( 0.005 ms): pahole/765220 openat(dfd: CWD, filename: "/usr/lib/debug/lib/modules/6.11.6-200.fc40.x86_64/vmlinux", flags: RDONLY|CLOEXEC) = 3 root@x1:~#

So honour the request and fails when just BTF is asked and no BTF file is found:

  # export PAHOLE_VMLINUX_BTF_FILENAME=non-existent
  # pahole -F btf list_head
  pahole: couldn't find any btf debug information on this system.
  #
Quoted 8 lines
Cc Alan Maguire <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Eduard Zingerman <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>

For the series (since some of my replies didn't reach the list, apologies)

#5 lore raw

Re: [PATCH 3/5] pahole: Honour exclusive BTF loading

Quoted 62 lines · Alan Maguire
Quoted 48 lines · Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

When we specify 'btf' and BTF info is available in the system, in the /sys/kernel/btf/vmlinux file, pahole will find it and use it to get the type information asked for:

  root@x1:~# perf trace -o /tmp/output -e openat pahole -F btf list_head ; tail -1 /tmp/output
  struct list_head {
  	struct list_head *         next;                 /*     0     8 */
  	struct list_head *         prev;                 /*     8     8 */
  	/* size: 16, cachelines: 1, members: 2 */
  	/* last cacheline: 16 bytes */
  };
       1.579 ( 0.010 ms): pahole/764777 openat(dfd: CWD, filename: "/sys/kernel/btf/vmlinux", flags: RDONLY|CLOEXEC) = 3

root@x1:~#

But, if the system doesn't have BTF info, which we now can simulate by using an environment variable it is tryng DWARF after failing for BTF, which isn't what the user asked for having specified just btf in the -F pahole command line:

  root@x1:~# export PAHOLE_VMLINUX_BTF_FILENAME=non-existent
  root@x1:~# perf trace -o /tmp/output -e openat,access pahole -F btf list_head ; tail -12 /tmp/output
  struct list_head {
  	struct list_head *         next;                 /*     0     8 */
  	struct list_head *         prev;                 /*     8     8 */
  	/* size: 16, cachelines: 1, members: 2 */
  	/* last cacheline: 16 bytes */
  };

1.643 ( 0.003 ms): pahole/765220 access(filename: "list_head", mode: R) = -1 ENOENT (No such file or directory)
1.658 ( 0.002 ms): pahole/765220 access(filename: "non-existent", mode: R) = -1 ENOENT (No such file or directory)
1.697 ( 0.018 ms): pahole/765220 openat(dfd: CWD, filename: "/sys/kernel/notes") = 3
1.801 ( 0.004 ms): pahole/765220 openat(dfd: CWD, filename: "vmlinux") = -1 ENOENT (No such file or directory)
1.807 ( 0.005 ms): pahole/765220 openat(dfd: CWD, filename: "/boot/vmlinux") = -1 ENOENT (No such file or directory)
1.927 ( 0.005 ms): pahole/765220 openat(dfd: CWD, filename: "/usr/lib/debug/lib/modules/6.11.6-200.fc40.x86_64/vmlinux", flags: RDONLY|CLOEXEC) = 3 root@x1:~#

So honour the request and fails when just BTF is asked and no BTF file is found:

  # export PAHOLE_VMLINUX_BTF_FILENAME=non-existent
  # pahole -F btf list_head
  pahole: couldn't find any btf debug information on this system.
  #
Quoted 8 lines
Cc Alan Maguire <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Eduard Zingerman <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>

For the series (since some of my replies didn't reach the list, apologies)

#6 lore raw

Re: [PATCH 3/5] pahole: Honour exclusive BTF loading

In reply to Alan Maguire
Quoted 58 lines · Alan Maguire
Quoted 48 lines · Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

When we specify 'btf' and BTF info is available in the system, in the /sys/kernel/btf/vmlinux file, pahole will find it and use it to get the type information asked for:

  root@x1:~# perf trace -o /tmp/output -e openat pahole -F btf list_head ; tail -1 /tmp/output
  struct list_head {
  	struct list_head *         next;                 /*     0     8 */
  	struct list_head *         prev;                 /*     8     8 */
  	/* size: 16, cachelines: 1, members: 2 */
  	/* last cacheline: 16 bytes */
  };
       1.579 ( 0.010 ms): pahole/764777 openat(dfd: CWD, filename: "/sys/kernel/btf/vmlinux", flags: RDONLY|CLOEXEC) = 3

root@x1:~#

But, if the system doesn't have BTF info, which we now can simulate by using an environment variable it is tryng DWARF after failing for BTF, which isn't what the user asked for having specified just btf in the -F pahole command line:

  root@x1:~# export PAHOLE_VMLINUX_BTF_FILENAME=non-existent
  root@x1:~# perf trace -o /tmp/output -e openat,access pahole -F btf list_head ; tail -12 /tmp/output
  struct list_head {
  	struct list_head *         next;                 /*     0     8 */
  	struct list_head *         prev;                 /*     8     8 */
  	/* size: 16, cachelines: 1, members: 2 */
  	/* last cacheline: 16 bytes */
  };

1.643 ( 0.003 ms): pahole/765220 access(filename: "list_head", mode: R) = -1 ENOENT (No such file or directory)
1.658 ( 0.002 ms): pahole/765220 access(filename: "non-existent", mode: R) = -1 ENOENT (No such file or directory)
1.697 ( 0.018 ms): pahole/765220 openat(dfd: CWD, filename: "/sys/kernel/notes") = 3
1.801 ( 0.004 ms): pahole/765220 openat(dfd: CWD, filename: "vmlinux") = -1 ENOENT (No such file or directory)
1.807 ( 0.005 ms): pahole/765220 openat(dfd: CWD, filename: "/boot/vmlinux") = -1 ENOENT (No such file or directory)
1.927 ( 0.005 ms): pahole/765220 openat(dfd: CWD, filename: "/usr/lib/debug/lib/modules/6.11.6-200.fc40.x86_64/vmlinux", flags: RDONLY|CLOEXEC) = 3 root@x1:~#

So honour the request and fails when just BTF is asked and no BTF file is found:

  # export PAHOLE_VMLINUX_BTF_FILENAME=non-existent
  # pahole -F btf list_head
  pahole: couldn't find any btf debug information on this system.
  #
Quoted 8 lines
Cc Alan Maguire <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Eduard Zingerman <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Quoted 4 lines

For the series (since some of my replies didn't reach the list, apologies)

Thanks, added to all csets,

- Arnaldo

#7 lore raw

Re: [PATCH 3/5] pahole: Honour exclusive BTF loading

In reply to Arnaldo Carvalho de Melo

On Tue, 2024-11-19 at 10:40 -0300, Arnaldo Carvalho de Melo wrote:

[...]

I tried the following test on top of this patch-set:

----- 8< ----------------------------------------
#!/bin/bash

cat <<EOF | clang -o btf-and-dwarf.o -c -x c -g - || exit 1 struct test_val { int i; } g;
EOF
pahole --btf_encode btf-and-dwarf.o || exit 1
# Note: has to be llvm-strip, otherwise --no-strip-all is not available
llvm-strip -o only-dwarf.o --no-strip-all -R .BTF -R .BTF.ext btf-and-dwarf.o
llvm-strip -o only-btf.o --no-strip-all --strip-debug btf-and-dwarf.o

for fmt in none btf dwarf dwarf,btf; do

    for file in no-such-file only-btf.o only-dwarf.o btf-and-dwarf.o; do
        if [ "$fmt" == "none" ]; then
            cmd="pahole"
        else
            cmd="pahole -F ${fmt}"
        fi
        printf -- "------ PAHOLE_VMLINUX_BTF_FILENAME=%-20s %-20s ------\n" "${file}" "${cmd}"
        PAHOLE_VMLINUX_BTF_FILENAME=$file $cmd 2>&1 | grep -P '(test_val|pahole:)'
    done

done
---------------------------------------- >8 -----

And there are a few surprising (to me) results, namely:

------ PAHOLE_VMLINUX_BTF_FILENAME=only-dwarf.o pahole ------
pahole: couldn't find any debug information on this system.
...
------ PAHOLE_VMLINUX_BTF_FILENAME=only-dwarf.o pahole -F dwarf ------
pahole: couldn't find any debug information on this system.
...
------ PAHOLE_VMLINUX_BTF_FILENAME=btf-and-dwarf.o pahole -F dwarf ------
pahole: couldn't find any debug information on this system.
...
------ PAHOLE_VMLINUX_BTF_FILENAME=only-dwarf.o pahole -F dwarf,btf ------
pahole: couldn't find any debug information on this system.
...

It looks like DWARF is never loaded.
llvm-dwarfdump shows that DWARF is indeed present in only-dwarf.o and btf-and-dwarf.o. Is this an expected behaviour?

#8 lore raw

Re: [PATCH 3/5] pahole: Honour exclusive BTF loading

Quoted 27 lines · Eduard Zingerman

On Tue, 2024-11-19 at 10:40 -0300, Arnaldo Carvalho de Melo wrote:

[...]

I tried the following test on top of this patch-set:

----- 8< ----------------------------------------
#!/bin/bash

cat <<EOF | clang -o btf-and-dwarf.o -c -x c -g - || exit 1 struct test_val { int i; } g;
EOF
pahole --btf_encode btf-and-dwarf.o || exit 1
# Note: has to be llvm-strip, otherwise --no-strip-all is not available
llvm-strip -o only-dwarf.o --no-strip-all -R .BTF -R .BTF.ext btf-and-dwarf.o
llvm-strip -o only-btf.o --no-strip-all --strip-debug btf-and-dwarf.o

for fmt in none btf dwarf dwarf,btf; do

    for file in no-such-file only-btf.o only-dwarf.o btf-and-dwarf.o; do
        if [ "$fmt" == "none" ]; then
            cmd="pahole"
        else
            cmd="pahole -F ${fmt}"
        fi
        printf -- "------ PAHOLE_VMLINUX_BTF_FILENAME=%-20s %-20s ------\n" "${file}" "${cmd}"
        PAHOLE_VMLINUX_BTF_FILENAME=$file $cmd 2>&1 | grep -P '(test_val|pahole:)'
    done
Quoted 1 line

done

this is a nice test, maybe we could adapt it for inclusion. more below..

Quoted 17 lines

---------------------------------------- >8 -----

And there are a few surprising (to me) results, namely:

------ PAHOLE_VMLINUX_BTF_FILENAME=only-dwarf.o pahole ------
pahole: couldn't find any debug information on this system.
...
------ PAHOLE_VMLINUX_BTF_FILENAME=only-dwarf.o pahole -F dwarf ------
pahole: couldn't find any debug information on this system.
...
------ PAHOLE_VMLINUX_BTF_FILENAME=btf-and-dwarf.o pahole -F dwarf ------
pahole: couldn't find any debug information on this system.
...
------ PAHOLE_VMLINUX_BTF_FILENAME=only-dwarf.o pahole -F dwarf,btf ------
pahole: couldn't find any debug information on this system.
...

Quoted 5 lines

It looks like DWARF is never loaded.
llvm-dwarfdump shows that DWARF is indeed present in only-dwarf.o and btf-and-dwarf.o. Is this an expected behaviour?

So my read of the current intent is for PAHOLE_VMLINUX_BTF_FILENAME to provide an override path for BTF debug info only. In the above cases we know where to find BTF but don't go on to assume there is DWARF there too. I think this behaviour would be more surprising if the variable was called something more generic like PAHOLE_VMLINUX_DEBUGINFO_FILENAME - i.e. not specific to debug info type - but that's just my take.

We could certainly make the override mechanism more generic to other debuginfo types, but I think that could be done in follow-ups. What do you think? Thanks!

Alan

#9 lore raw

Re: [PATCH 3/5] pahole: Honour exclusive BTF loading

On Tue, 2024-11-19 at 22:28 +0000, Alan Maguire wrote:

[...]

Quoted 6 lines

So my read of the current intent is for PAHOLE_VMLINUX_BTF_FILENAME to provide an override path for BTF debug info only. In the above cases we know where to find BTF but don't go on to assume there is DWARF there too. I think this behaviour would be more surprising if the variable was called something more generic like PAHOLE_VMLINUX_DEBUGINFO_FILENAME - i.e. not specific to debug info type - but that's just my take.

Oh, right...
My bad, sorry for the noise.

#10 lore raw

PATCH 4/5 tests default_vmlinux_btf: Cover the no args segfault too

In reply to Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

  root@x1:/home/acme/git/pahole# pahole --running_kernel_vmlinux
  pahole: couldn't find a vmlinux that matches the running kernel
  HINT: Maybe you're inside a container or missing a debuginfo package?
  root@x1:/home/acme/git/pahole# tests/default_vmlinux_btf.sh
  Default BTF on a system without BTF: FAILED
  root@x1:/home/acme/git/pahole# pahole
  Segmentation fault (core dumped)
  root@x1:/home/acme/git/pahole#

Now to fix this one as well.

Cc Alan Maguire <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Eduard Zingerman <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
---
 tests/default_vmlinux_btf.sh | 11 +++++++++++
 1 file changed, 11 insertions(+)
tests/default_vmlinux_btf.sh +11 -0
index a9effa2d6d37e0ee..a855ca7587380934 100755
@@ -17,5 +17,16 @@ if [ $nr_lines -eq 0 ] ; then
 	exit 1
 fi
 
+# There is also the case where no debugging info is available, be it DWARF of
+# BTF and it segfaults when calling just 'pahole', with no args, so check for
+# that as well
+#
+nr_lines=$(PAHOLE_VMLINUX_BTF_FILENAME=foobar pahole 2>&1 | wc -l)
+
+if [ $nr_lines -eq 0 ] ; then
+	echo "FAILED"
+	exit 1
+fi
+
 echo "Ok"
 exit 0
Signature · 1 line
2.47.0
#11 lore raw

PATCH 5/5 core, libctf: Check if constructor arguments are NULL before using them

In reply to Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

  # pahole
  Segmentation fault (core dumped)
  #

Now we have:

  # ls -la bla
  ls: cannot access 'bla': No such file or directory
  # export PAHOLE_VMLINUX_BTF_FILENAME=bla
  # pahole
  pahole: couldn't find any debug information on this system.
  # pahole list_head
  pahole: couldn't find any debug information on this system.
  # pahole -F btf list_head
  pahole: couldn't find any btf debug information on this system.
  # pahole -F dwarf list_head
  pahole: couldn't find any dwarf debug information on this system.
  # tests/default_vmlinux_btf.sh
  Default BTF on a system without BTF: Ok
  #
Cc Alan Maguire <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Eduard Zingerman <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
---
 dwarves.c | 3 +++
 libctf.c  | 3 +++
 2 files changed, 6 insertions(+)
dwarves.c +3 -0
index 459311e64c8b45b2..ae512b9f46ddcc0a 100644
@@ -777,6 +777,9 @@ struct cu *cu__new(const char *name, uint8_t addr_size,
 		if (cu->use_obstack)
 			obstack_init(&cu->obstack);
 
+		if (name == NULL || filename == NULL)
+			goto out_free;
+
 		cu->name = strdup(name);
 		if (cu->name == NULL)
 			goto out_free;
libctf.c +3 -0
index edb34dbf10de09e7..0641b10586527a6e 100644
@@ -179,6 +179,9 @@ struct ctf *ctf__new(const char *filename, Elf *elf)
 	struct ctf *ctf = zalloc(sizeof(*ctf));
 
 	if (ctf != NULL) {
+		if (filename == NULL)
+			goto out_delete;
+
 		ctf->filename = strdup(filename);
 		if (ctf->filename == NULL)
 			goto out_delete;
Signature · 1 line
2.47.0