perf test: Fix a memory leak in PE file parsing test
Namhyung Kim
maintainerPATCH 1/5 perf test: Fix a memory leak in PE file parsing test
The get_argv_exec_path() returns a dynamically allocated string so it should be freed. Let's check the return value first so that it doesn't crash snprintf().
--- tools/perf/tests/pe-file-parsing.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
tools/perf/tests/pe-file-parsing.c +10 -3
@@ -74,19 +74,26 @@ static int test__pe_file_parsing(struct test_suite *test __maybe_unused, {struct stat st;char path_dir[PATH_MAX ]; -const char *const tests_dirs[] = { +static const char *const tests_dirs[] = {"./tools/perf/tests" ,"./tests" ,"./source/tests" , - path_dir, }; -snprintf (path_dir,PATH_MAX ,"%s/tests" ,get_argv_exec_path ()); +char *exec_path;for (size_t i =0 ; i <ARRAY_SIZE (tests_dirs); ++i) {if (!lstat (tests_dirs[i], &st))return run_dir (tests_dirs[i]); } + exec_path =get_argv_exec_path (); +TEST_ASSERT_VAL ("Failed to allocate exec_path" , exec_path); +snprintf (path_dir,PATH_MAX ,"%s/tests" , exec_path); +free (exec_path); + +if (!lstat (path_dir, &st)) +return run_dir (path_dir); +pr_debug ("cannot find the tests directory\n" );return TEST_SKIP ; }
Signature · 1 line
2.56.0.176.g3575e65682-goog
Namhyung Kim
maintainerPATCH 2/5 perf test: Make sure maps to have a machine
It's be easier if we could guarantee that maps always have a machine. IIUC this is true for all real use cases, but only the test code has an exception. Let's get rid of it and make sure they have machines.
--- tools/perf/tests/maps.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
tools/perf/tests/maps.c +13 -2
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <inttypes.h> +#include <unistd.h> #include <linux/compiler.h> #include <linux/kernel.h> #include "tests.h"
@@ -100,8 +101,12 @@ static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtest };struct map *map_kcore1, *map_kcore2, *map_kcore3;int ret; -struct maps *maps =maps__new (NULL ); +struct machine *machine =machine__new_live (/*host_env=*/ NULL , +/*kernel_maps=*/ false, +getpid ()); +struct maps *maps =maps__new (machine); +TEST_ASSERT_VAL ("failed to create machine" , machine);TEST_ASSERT_VAL ("failed to create maps" , maps);for (i =0 ; i <ARRAY_SIZE (bpf_progs); i++) {
@@ -159,6 +164,7 @@ static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtestmap__zput (map_kcore1);map__zput (map_kcore2);map__zput (map_kcore3); +machine__delete (machine);return TEST_OK ; }
@@ -189,8 +195,12 @@ static int test__maps__fixup_overlap_and_insert(struct test_suite *t __maybe_unustruct map *map_split, *map_eclipse;int ret;unsigned int i; -struct maps *maps =maps__new (NULL ); +struct machine *machine =machine__new_live (/*host_env=*/ NULL , +/*kernel_maps=*/ false, +getpid ()); +struct maps *maps =maps__new (machine); +TEST_ASSERT_VAL ("failed to create machine" , machine);TEST_ASSERT_VAL ("failed to create maps" , maps);for (i =0 ; i <ARRAY_SIZE (initial_maps); i++) {
@@ -230,6 +240,7 @@ static int test__maps__fixup_overlap_and_insert(struct test_suite *t __maybe_unuTEST_ASSERT_VAL ("eclipse check failed" , !ret);maps__zput (maps); +machine__delete (machine);return TEST_OK ; }
Signature · 1 line
2.56.0.176.g3575e65682-goog
[email protected]
AI reviewRe: [PATCH v1 2/5] perf test: Make sure maps to have a machine
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines
It's be easier if we could guarantee that maps always have a machine. IIUC this is true for all real use cases, but only the test code has an exception. Let's get rid of it and make sure they have machines.
Signed-off-by Namhyung Kim <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerRe: [PATCH v1 2/5] perf test: Make sure maps to have a machine
In reply to Namhyung KimQuoted 44 lines · Namhyung Kim
It's be easier if we could guarantee that maps always have a machine. IIUC this is true for all real use cases, but only the test code has an exception. Let's get rid of it and make sure they have machines.
Signed-off-by Namhyung Kim <[email protected]>--- tools/perf/tests/maps.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)tools/perf/tests/maps.c +7
-1@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <inttypes.h> +#include <unistd.h> #include <linux/compiler.h> #include <linux/kernel.h> #include "tests.h" @@ -100,8 +101,12 @@ static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtest };struct map *map_kcore1, *map_kcore2, *map_kcore3;int ret; -struct maps *maps =maps__new (NULL ); +struct machine *machine =machine__new_live (/*host_env=*/ NULL , +/*kernel_maps=*/ false, +getpid ()); +struct maps *maps =maps__new (machine); +TEST_ASSERT_VAL ("failed to create machine" , machine);TEST_ASSERT_VAL ("failed to create maps" , maps);for (i =0 ; i <ARRAY_SIZE (bpf_progs); i++) {@@ -159,6 +164,7 @@ static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtestmap__zput (map_kcore1);map__zput (map_kcore2);map__zput (map_kcore3); +machine__delete (machine);return TEST_OK ; }
tools/perf/tests/maps.c static int test__maps__fixup_overlap_and_insert(struct test_suite *t __maybe_unu · line 200
Quoted 7 lines
tools/perf/tests/maps.c +3
-1@@ -189,8 +195,12 @@ static int test__maps__fixup_overlap_and_insert(struct test_suite *t __maybe_unustruct map *map_split, *map_eclipse;int ret;unsigned int i; -struct maps *maps =maps__new (NULL ); +struct machine *machine =machine__new_live (/*host_env=*/ NULL , +/*kernel_maps=*/ false, +getpid ());
For completeness should we pass in a host_env like in builtin-top.c?
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-top.c?h=perf-tools-next#n1661
Thanks,
Ian
Quoted 17 lines
tools/perf/tests/maps.c +3
-1+struct maps *maps =maps__new (machine); +TEST_ASSERT_VAL ("failed to create machine" , machine);TEST_ASSERT_VAL ("failed to create maps" , maps);for (i =0 ; i <ARRAY_SIZE (initial_maps); i++) {@@ -230,6 +240,7 @@ static int test__maps__fixup_overlap_and_insert(struct test_suite *t __maybe_unuTEST_ASSERT_VAL ("eclipse check failed" , !ret);maps__zput (maps); +machine__delete (machine);return TEST_OK ; } --2.56.0.176.g3575e65682-goog
Namhyung Kim
maintainerRe: [PATCH v1 2/5] perf test: Make sure maps to have a machine
Quoted 44 lines · Ian Rogers
Quoted 43 lines · Namhyung Kim
It's be easier if we could guarantee that maps always have a machine. IIUC this is true for all real use cases, but only the test code has an exception. Let's get rid of it and make sure they have machines.
Signed-off-by Namhyung Kim <[email protected]>--- tools/perf/tests/maps.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)tools/perf/tests/maps.c +7
-1@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <inttypes.h> +#include <unistd.h> #include <linux/compiler.h> #include <linux/kernel.h> #include "tests.h" @@ -100,8 +101,12 @@ static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtest };struct map *map_kcore1, *map_kcore2, *map_kcore3;int ret; -struct maps *maps =maps__new (NULL ); +struct machine *machine =machine__new_live (/*host_env=*/ NULL , +/*kernel_maps=*/ false, +getpid ()); +struct maps *maps =maps__new (machine); +TEST_ASSERT_VAL ("failed to create machine" , machine);TEST_ASSERT_VAL ("failed to create maps" , maps);for (i =0 ; i <ARRAY_SIZE (bpf_progs); i++) {@@ -159,6 +164,7 @@ static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtestmap__zput (map_kcore1);map__zput (map_kcore2);map__zput (map_kcore3); +machine__delete (machine);return TEST_OK ; }
Quoted 11 lines
tools/perf/tests/maps.c static int test__maps__fixup_overlap_and_insert(struct test_suite *t __maybe_unu · line 200
Quoted 8 lines
tools/perf/tests/maps.c +3
-1@@ -189,8 +195,12 @@ static int test__maps__fixup_overlap_and_insert(struct test_suite *t __maybe_unustruct map *map_split, *map_eclipse;int ret;unsigned int i; -struct maps *maps =maps__new (NULL ); +struct machine *machine =machine__new_live (/*host_env=*/ NULL , +/*kernel_maps=*/ false, +getpid ());For completeness should we pass in a
host_envlike inbuiltin-top.c?
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-top.c?h=perf-tools-next#n1661
Sure, will add in v2.
Thanks,
Namhyung
Namhyung Kim
maintainerPATCH 3/5 perf tools: Remove NULL check for maps__machine()
In reply to Namhyung KimNow maps should have a machine, let's get rid of checks for the return value. There are already many places to assume it returns non-NULL, so having this kind of check could give confusion that it can return NULL (which is not true anymore).
--- tools/perf/util/dlfilter.c | 3 +-- tools/perf/util/event.c | 3 --- tools/perf/util/map.c | 9 +++------ tools/perf/util/python.c | 2 +- 4 files changed, 5 insertions(+), 12 deletions(-)
tools/perf/util/dlfilter.c +1 -2
@@ -233,8 +233,7 @@ static const __u8 *dlfilter__insn(void *ctx, __u32 *len)if (thread__maps (al->thread)) {struct machine *machine =maps__machine (thread__maps (al->thread)); -if (machine) -perf_sample__fetch_insn (d->sample, al->thread, machine); +perf_sample__fetch_insn (d->sample, al->thread, machine); } }
tools/perf/util/event.c +0 -3
@@ -706,9 +706,6 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr, al->cpumode = cpumode; al->filtered =0 ; -if (machine ==NULL ) -return NULL ; -if (cpumode ==PERF_RECORD_MISC_KERNEL && perf_host) { al->level ='k' ; maps =machine__kernel_maps (machine);
tools/perf/util/map.c +3 -6
@@ -538,13 +538,10 @@ u64 map__rip_2objdump(const struct map *map, u64 rip)*/ if (kmap &&is_entry_trampoline (kmap->name) && kmap->kmaps) {struct machine *machine =maps__machine (kmap->kmaps); +struct map *kernel_map =machine__kernel_map (machine); -if (machine) { -struct map *kernel_map =machine__kernel_map (machine); - -if (kernel_map) - map = kernel_map; - } +if (kernel_map) + map = kernel_map; }if (!dso__adjust_symbols (dso))
tools/perf/util/python.c +1 -1
@@ -4536,7 +4536,7 @@ static PyObject *pyrf_call_return__new(struct call_return *cr)if (cr->thread) {struct maps *maps =thread__maps (cr->thread); - pevent->machine_pid = maps &&maps__machine (maps) ?maps__machine (maps)->pid :0 ; + pevent->machine_pid = maps ?maps__machine (maps)->pid :0 ; pevent->pid =thread__pid (cr->thread); pevent->tid =thread__tid (cr->thread); }else {
Signature · 1 line
2.56.0.176.g3575e65682-goog
[email protected]
AI reviewRe: [PATCH v1 3/5] perf tools: Remove NULL check for maps__machine()
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines
Now maps should have a machine, let's get rid of checks for the return value. There are already many places to assume it returns non-NULL, so having this kind of check could give confusion that it can return NULL (which is not true anymore).
Signed-off-by Namhyung Kim <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Namhyung Kim
maintainerPATCH 4/5 perf symbol: Check result when reading section headers
In reply to Namhyung KimOtherwise it may access uninitialized section header struct.
--- tools/perf/util/symbol-elf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/util/symbol-elf.c +3 -1
@@ -196,13 +196,15 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,while ((sec =elf_nextscn (elf, sec)) !=NULL ) {char *str; -gelf_getshdr (sec, shp); +if (!gelf_getshdr (sec, shp)) + goto next; str =elf_strptr (elf, ep->e_shstrndx, shp->sh_name);if (str && !strcmp (name, str)) {if (idx) *idx = cnt;return sec; } +next: ++cnt; }
Signature · 1 line
2.56.0.176.g3575e65682-goog
[email protected]
AI reviewRe: [PATCH v1 4/5] perf symbol: Check result when reading section headers
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines
Otherwise it may access uninitialized section header struct.
Reported-by Sashiko <[email protected]>Signed-off-by Namhyung Kim <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Namhyung Kim
maintainerPATCH 5/5 perf symbol: Ensure dso__set_is_64_bit() for libbfd
In reply to Namhyung KimOtherwise it can confuse cross-platform analysis.
--- tools/perf/util/libbfd.c | 1 + 1 file changed, 1 insertion(+)
tools/perf/util/libbfd.c +1 -0
@@ -462,6 +462,7 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile)symbols__fixup_end (dso__symbols (dso), false);symbols__fixup_duplicate (dso__symbols (dso));dso__set_adjust_symbols (dso, true); +dso__set_is_64_bit (dso,bfd_get_arch_size (abfd) ==64 ); err =dso__has_symbols (dso); out_free:
Signature · 1 line
2.56.0.176.g3575e65682-goog
[email protected]
AI reviewRe: [PATCH v1 5/5] perf symbol: Ensure dso__set_is_64_bit() for libbfd
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf symbol: bfd_get_arch_size() always returns -1 in dso__load_bfd_symbols() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf symbol: bfd_get_arch_size() always returns -1 in dso__load_bfd_symbols() --
commit 6d3220ac244766831d73c524c0b8f5184af28a7b
Author: Namhyung Kim <[email protected]>
perf symbol: Ensure dso__set_is_64_bit() for libbfd
This commit calls dso__set_is_64_bit() using bfd_get_arch_size() when loading symbols via libbfd in dso__load_bfd_symbols().
tools/perf/util/libbfd.c int dso__load_bfd_symbols(struct dso *dso, const char *debugfile) · line 465
Quoted 9 lines
tools/perf/util/libbfd.c +1
-0@@ -462,6 +462,7 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile)symbols__fixup_end (dso__symbols (dso), false);symbols__fixup_duplicate (dso__symbols (dso));dso__set_adjust_symbols (dso, true); +dso__set_is_64_bit (dso,bfd_get_arch_size (abfd) ==64 );
[Severity: Medium]
Will bfd_get_arch_size(abfd) always return -1 here?
Earlier in tools/perf/util/libbfd.c:dso__load_bfd_symbols(), ELF binaries are rejected before reaching this point:
if (bfd_get_flavour(abfd) == bfd_target_elf_flavour) goto out_close;
In GNU binutils libbfd, bfd_get_arch_size() only checks ELF targets and returns -1 for any other BFD flavour:
if (abfd->xvec->flavour == bfd_target_elf_flavour) return get_elf_backend_data (abfd)->s->arch_size; return -1;
Because abfd is guaranteed to be non-ELF here (such as a 64-bit PE/COFF binary), bfd_get_arch_size(abfd) == 64 always evaluates to false and unconditionally sets dso->is_64_bit to false.
Should this use bfd_arch_bits_per_address(abfd) == 64 instead so that the address size is queried across all BFD flavours?
Quoted 3 lines
tools/perf/util/libbfd.c +0
-0err =dso__has_symbols (dso); out_free:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Namhyung Kim
maintainerRe: [PATCH v1 5/5] perf symbol: Ensure dso__set_is_64_bit() for libbfd
Quoted 38 lines · On Sun, Oct 18, 2026 at 04:44:26PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf symbol:bfd_get_arch_size()always returns -1 indso__load_bfd_symbols()--commit
6d3220ac244766831d73c524c0b8f5184af28a7b
Author: Namhyung Kim <[email protected]>perf symbol: Ensure
dso__set_is_64_bit()for libbfdThis commit calls
dso__set_is_64_bit()usingbfd_get_arch_size()when loading symbols via libbfd indso__load_bfd_symbols().tools/perf/util/libbfd.c int dso__load_bfd_symbols(struct dso *dso, const char *debugfile) · line 465
Quoted 9 lines
tools/perf/util/libbfd.c +1
-0@@ -462,6 +462,7 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile)symbols__fixup_end (dso__symbols (dso), false);symbols__fixup_duplicate (dso__symbols (dso));dso__set_adjust_symbols (dso, true); +dso__set_is_64_bit (dso,bfd_get_arch_size (abfd) ==64 );[Severity: Medium]
Willbfd_get_arch_size(abfd)always return -1 here?Earlier in tools/perf/util/libbfd.c:dso__load_bfd_symbols(), ELF binaries are rejected before reaching this point:
if (bfd_get_flavour(abfd) == bfd_target_elf_flavour) goto out_close;In GNU binutils libbfd,
bfd_get_arch_size()only checks ELF targets and returns -1 for any other BFD flavour:if (abfd->xvec->flavour == bfd_target_elf_flavour) return get_elf_backend_data (abfd)->s->arch_size; return -1;
Quoted 6 lines
Because abfd is guaranteed to be non-ELF here (such as a 64-bit PE/COFF binary),
bfd_get_arch_size(abfd)== 64 always evaluates to false and unconditionally setsdso->is_64_bitto false.Should this use
bfd_arch_bits_per_address(abfd)== 64 instead so that the address size is queried across all BFD flavours?
Looks good, will fix.
Thanks,
Namhyung
Quoted 7 lines
Quoted 4 lines
tools/perf/util/libbfd.c +0
-0err =dso__has_symbols (dso); out_free:Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 7 lines
The
get_argv_exec_path()returns a dynamically allocated string so it should be freed. Let's check the return value first so that it doesn't crashsnprintf().Reported-by Sashiko <[email protected]>Signed-off-by Namhyung Kim <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1