perf tests: Fix incorrect free and false TEST_OK result
PATCH 1/2 perf tests: Fix incorrect free and false TEST_OK result
Commit cc1121ab9687d660cc02f50b1a4974112f87a8e6 ('perf machine: Fix machine.vmlinux_maps to make sure to clear the old one') reveals a bug in 'perf test' that in all test cases which use setup_fake_machine() incorrectly call free() when failure, because all users of setup_fake_machine() use static allocated 'machines' structure, but setup_fake_machine() calls machine__delete() which try to free() it.
If a normal user try those test cases this problem can be seen:
$ cat /proc/sys/kernel/kptr_restrict 1 $ ./perf test 'hist' 15: Test matching and linking multiple hists :*** Error in `./perf': munmap_chunk(): invalid pointer: 0x00007ffd6e900090 *** ======= Backtrace: ========= /lib64/libc.so.6(+0x6eeef)[0x7fcec97e1eef] /lib64/libc.so.6(+0x78cae)[0x7fcec97ebcae] ./perf(setup_fake_machine+0x1cd)[0x4721ad] ./perf(test__hists_link+0xbf)[0x472d3f] ./perf[0x4648df] ./perf(cmd_test+0x589)[0x464ec9] ./perf[0x47fd11] ./perf(main+0x5f6)[0x432b96] /lib64/libc.so.6(__libc_start_main+0xf5)[0x7fcec9794bd5] ./perf[0x432cc5] ======= Memory map: ======== ... 25: Test filtering hist entries :*** Error in `./perf': munmap_chunk(): invalid pointer: 0x00007ffd6e900080 *** ======= Backtrace: ========= /lib64/libc.so.6(+0x6eeef)[0x7fcec97e1eef] /lib64/libc.so.6(+0x78cae)[0x7fcec97ebcae] ./perf(setup_fake_machine+0x1cd)[0x4721ad] ./perf(test__hists_filter+0xc6)[0x4730f6] ./perf[0x4648df] ./perf(cmd_test+0x589)[0x464ec9] ./perf[0x47fd11] ./perf(main+0x5f6)[0x432b96] /lib64/libc.so.6(__libc_start_main+0xf5)[0x7fcec9794bd5] ./perf[0x432cc5] ======= Memory map: ======== ... ...
Actually, all users of 'machines__init()' should avoid calling machine__delete(). We can further add a flag in machine structure to enforce this restriction.
After changing machine__delete() to machine__exit() another problem arises:
$ ./perf test 'hist' 15: Test matching and linking multiple hists : Ok 25: Test filtering hist entries : Ok 28: Test output sorting of hist entries : Ok 29: Test cumulation of child hist entries : Ok
The result is not true:
$ ./perf test -v 'hist' 15: Test matching and linking multiple hists : --- start --- test child forked, pid 17657 Not enough memory for machine setup Not enough memory for machine setup test child finished with 0 ---- end ---- Test matching and linking multiple hists: Ok 25: Test filtering hist entries : --- start --- test child forked, pid 17658 Not enough memory for machine setup Not enough memory for machine setup test child finished with 0 ---- end ---- Test filtering hist entries: Ok 28: Test output sorting of hist entries : --- start --- test child forked, pid 17659 Not enough memory for machine setup Not enough memory for machine setup test child finished with 0 ---- end ---- Test output sorting of hist entries: Ok 29: Test cumulation of child hist entries : --- start --- test child forked, pid 17660 Not enough memory for machine setup Not enough memory for machine setup test child finished with 0 ---- end ---- Test cumulation of child hist entries: Ok
Because the test body is not executed at all.
The reason is that *ALL* hists test cases forget to reset err after using it to hold an error code:
err = TEST_FAIL;
...
err = parse_events(evlist, "cpu-clock", NULL);
if (err)
goto out;
/* err is already 0 here */
...
machine = setup_fake_machine(&machines);
if (!machine)
goto out; ...
out:
...
return err;
This patch ensure err is reset.
In case when kptr_restrict prevent normal user get kernel address, this test should be skipped, not fail. This patch use linux/err.h to store error code in return value of setup_fake_machine(), and let 'EACCES' to indicate this problem.
Also, the debug message 'Not enough memory for machine setup' is not true and should be fixed.
Here is the final result:
$ ./perf test 'hist' 15: Test matching and linking multiple hists : Skip 25: Test filtering hist entries : Skip 28: Test output sorting of hist entries : Skip 29: Test cumulation of child hist entries : Skip
$ ./perf test -v 'hist' 15: Test matching and linking multiple hists : --- start --- test child forked, pid 20177 Failed to create kernel maps Hint: Check /proc/sys/kernel/kptr_restrict. Failed for machine setup test child finished with -2 ---- end ---- Test matching and linking multiple hists: Skip 25: Test filtering hist entries ...
$ sudo ./perf test hist 15: Test matching and linking multiple hists : Ok 25: Test filtering hist entries : Ok 28: Test output sorting of hist entries : Ok 29: Test cumulation of child hist entries : Ok
--- tools/perf/tests/hists_common.c | 15 ++++++++++----- tools/perf/tests/hists_common.h | 1 + tools/perf/tests/hists_cumulate.c | 6 +++++- tools/perf/tests/hists_filter.c | 6 +++++- tools/perf/tests/hists_link.c | 6 +++++- tools/perf/tests/hists_output.c | 6 +++++- 6 files changed, 31 insertions(+), 9 deletions(-)
tools/perf/tests/hists_common.c +10 -5
@@ -81,14 +81,19 @@ struct machine *setup_fake_machine(struct machines *machines) {struct machine *machine =machines__find (machines,HOST_KERNEL_ID );size_t i; +int err = -ENOMEM ;if (machine ==NULL ) {pr_debug ("Not enough memory for machine setup\n" ); -return NULL ; +return ERR_PTR (-ENOMEM ); }if (machine__create_kernel_maps (machine)) { -pr_debug ("Not enough memory for machine setup\n" ); +pr_debug ("Failed to create kernel maps\n" ); +if (symbol_conf.kptr_restrict) { +pr_debug ("Hint: Check /proc/sys/kernel/kptr_restrict.\n" ); + err = -EACCES ; + } goto out; }
@@ -153,10 +158,10 @@ struct machine *setup_fake_machine(struct machines *machines)return machine; out: -pr_debug ("Not enough memory for machine setup\n" ); +pr_debug ("Failed for machine setup\n" );machine__delete_threads (machine); -machine__delete (machine); -return NULL ; +machine__exit (machine); +return ERR_PTR (err); }void print_hists_in (struct hists *hists)
tools/perf/tests/hists_common.h +1 -0
@@ -1,5 +1,6 @@#ifndef __PERF_TESTS__HISTS_COMMON_H__#define __PERF_TESTS__HISTS_COMMON_H__ +#include <linux/err.h> struct machine ;struct machines ;
tools/perf/tests/hists_cumulate.c +5 -1
@@ -706,13 +706,17 @@ int test__hists_cumulate(int subtest __maybe_unused) err =parse_events (evlist,"cpu-clock" ,NULL );if (err) goto out; + err =TEST_FAIL ;machines__init (&machines);/* setup threads/dso/map/symbols also */ machine =setup_fake_machine (&machines); -if (!machine) +if (IS_ERR (machine)) { +if (PTR_ERR (machine) == -EACCES ) + err =TEST_SKIP ; goto out; + }if (verbose >1 )machine__fprintf (machine, stderr);
tools/perf/tests/hists_filter.c +5 -1
@@ -120,6 +120,7 @@ int test__hists_filter(int subtest __maybe_unused) err =parse_events (evlist,"task-clock" ,NULL );if (err) goto out; + err =TEST_FAIL ;/* default sort order (comm,dso,sym) will be used */ if (setup_sorting () <0 )
@@ -129,8 +130,11 @@ int test__hists_filter(int subtest __maybe_unused)/* setup threads/dso/map/symbols also */ machine =setup_fake_machine (&machines); -if (!machine) +if (IS_ERR (machine)) { +if (PTR_ERR (machine) == -EACCES ) + err =TEST_SKIP ; goto out; + }if (verbose >1 )machine__fprintf (machine, stderr);
tools/perf/tests/hists_link.c +5 -1
@@ -293,6 +293,7 @@ int test__hists_link(int subtest __maybe_unused)if (err) goto out; + err =TEST_FAIL ;/* default sort order (comm,dso,sym) will be used */ if (setup_sorting () <0 ) goto out;
@@ -301,8 +302,11 @@ int test__hists_link(int subtest __maybe_unused)/* setup threads/dso/map/symbols also */ machine =setup_fake_machine (&machines); -if (!machine) +if (IS_ERR (machine)) { +if (PTR_ERR (machine) == -EACCES ) + err =TEST_SKIP ; goto out; + }if (verbose >1 )machine__fprintf (machine, stderr);
tools/perf/tests/hists_output.c +5 -1
@@ -597,13 +597,17 @@ int test__hists_output(int subtest __maybe_unused) err =parse_events (evlist,"cpu-clock" ,NULL );if (err) goto out; + err =TEST_FAIL ;machines__init (&machines);/* setup threads/dso/map/symbols also */ machine =setup_fake_machine (&machines); -if (!machine) +if (IS_ERR (machine)) { +if (PTR_ERR (machine) == -EACCES ) + err =TEST_SKIP ; goto out; + }if (verbose >1 )machine__fprintf (machine, stderr);
Signature · 1 line
1.8.3.4
PATCH 2/2 perf tools: Prevent calling machine__delete() on non-allocated machine
To prevent futher commits calling machine__delete() on non-allocated 'struct machine' (which would cause memory corruption), this patch enforces machine__init(), record whether a machine structure is dynamically allocated or not, and warn if machine__delete() is called on incorrect object.
--- tools/perf/tests/vmlinux-kallsyms.c | 4 ++-- tools/perf/util/machine.c | 14 +++++++++----- tools/perf/util/machine.h | 3 ++- 3 files changed, 13 insertions(+), 8 deletions(-)
tools/perf/tests/vmlinux-kallsyms.c +2 -2
@@ -35,8 +35,8 @@ int test__vmlinux_matches_kallsyms(int subtest __maybe_unused)* Init the machines that will hold kernel, modules obtained from * both vmlinux + .ko files and from /proc/kallsyms split by modules. */ -machine__init (&kallsyms,"" ,HOST_KERNEL_ID ); -machine__init (&vmlinux,"" ,HOST_KERNEL_ID ); +machine__init (&kallsyms,"" ,HOST_KERNEL_ID , false); +machine__init (&vmlinux,"" ,HOST_KERNEL_ID , false);/* * Step 2:
tools/perf/util/machine.c +9 -5
@@ -23,7 +23,7 @@ static void dsos__init(struct dsos *dsos)pthread_rwlock_init (&dsos->lock,NULL ); } -int machine__init (struct machine *machine,const char *root_dir,pid_t pid) +int machine__init (struct machine *machine,const char *root_dir,pid_t pid,bool allocated) {map_groups__init (&machine->kmaps, machine);RB_CLEAR_NODE (&machine->rb_node);
@@ -64,6 +64,7 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid) } machine->current_tid =NULL ; + machine->allocated = allocated;return 0 ; }
@@ -73,7 +74,7 @@ struct machine *machine__new_host(void)struct machine *machine =malloc (sizeof (*machine));if (machine !=NULL ) { -machine__init (machine,"" ,HOST_KERNEL_ID ); +machine__init (machine,"" ,HOST_KERNEL_ID , true);if (machine__create_kernel_maps (machine) <0 ) goto out_delete;
@@ -136,12 +137,15 @@ void machine__exit(struct machine *machine)void machine__delete (struct machine *machine) {machine__exit (machine); -free (machine); +if (!machine->allocated) +free (machine); +else +pr_warning ("WARNING: delete a non-allocated machine. Skip.\n" ); }void machines__init (struct machines *machines) { -machine__init (&machines->host,"" ,HOST_KERNEL_ID ); +machine__init (&machines->host,"" ,HOST_KERNEL_ID , false); machines->guests =RB_ROOT ; machines->symbol_filter =NULL ; }
@@ -162,7 +166,7 @@ struct machine *machines__add(struct machines *machines, pid_t pid,if (machine ==NULL )return NULL ; -if (machine__init (machine, root_dir, pid) !=0 ) { +if (machine__init (machine, root_dir, pid, true) !=0 ) {free (machine);return NULL ; }
tools/perf/util/machine.h +2 -1
@@ -28,6 +28,7 @@ struct machine {pid_t pid;u16 id_hdr_size;bool comm_exec; +bool allocated;char *root_dir;struct rb_root threads;pthread_rwlock_t threads_lock;
@@ -131,7 +132,7 @@ void machines__set_symbol_filter(struct machines *machines,void machines__set_comm_exec (struct machines *machines,bool comm_exec);struct machine *machine__new_host (void ); -int machine__init (struct machine *machine,const char *root_dir,pid_t pid); +int machine__init (struct machine *machine,const char *root_dir,pid_t pid,bool allocated);void machine__exit (struct machine *machine);void machine__delete_threads (struct machine *machine);void machine__delete (struct machine *machine);
Signature · 1 line
1.8.3.4
Re: [PATCH 2/2] perf tools: Prevent calling machine__delete() on non-allocated machine
Quoted 63 lines · Wang Nan
To prevent futher commits calling
machine__delete()on non-allocated 'struct machine' (which would cause memory corruption), this patch enforcesmachine__init(), record whether a machine structure is dynamically allocated or not, and warn ifmachine__delete()is called on incorrect object.Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Jiri Olsa <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>--- tools/perf/tests/vmlinux-kallsyms.c | 4 ++-- tools/perf/util/machine.c | 14 +++++++++----- tools/perf/util/machine.h | 3 ++- 3 files changed, 13 insertions(+), 8 deletions(-)tools/perf/tests/vmlinux-kallsyms.c +2
-2@@ -35,8 +35,8 @@ int test__vmlinux_matches_kallsyms(int subtest __maybe_unused)* Init the machines that will hold kernel, modules obtained from * both vmlinux + .ko files and from /proc/kallsyms split by modules. */ -machine__init (&kallsyms,"" ,HOST_KERNEL_ID ); -machine__init (&vmlinux,"" ,HOST_KERNEL_ID ); +machine__init (&kallsyms,"" ,HOST_KERNEL_ID , false); +machine__init (&vmlinux,"" ,HOST_KERNEL_ID , false);/* * Step 2: tools/perf/util/machine.c +3
-2@@ -23,7 +23,7 @@ static void dsos__init(struct dsos *dsos)pthread_rwlock_init (&dsos->lock,NULL ); } -int machine__init (struct machine *machine,const char *root_dir,pid_t pid) +int machine__init (struct machine *machine,const char *root_dir,pid_t pid,bool allocated) {map_groups__init (&machine->kmaps, machine);RB_CLEAR_NODE (&machine->rb_node);@@ -64,6 +64,7 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid) } machine->current_tid =NULL ; + machine->allocated = allocated;return 0 ; }@@ -73,7 +74,7 @@ struct machine *machine__new_host(void)struct machine *machine =malloc (sizeof (*machine));if (machine !=NULL ) { -machine__init (machine,"" ,HOST_KERNEL_ID ); +machine__init (machine,"" ,HOST_KERNEL_ID , true);if (machine__create_kernel_maps (machine) <0 ) goto out_delete;
tools/perf/util/machine.c void machine__exit(struct machine *machine) · line 140
Quoted 5 lines
tools/perf/util/machine.c +1
-1@@ -136,12 +137,15 @@ void machine__exit(struct machine *machine)void machine__delete (struct machine *machine) {machine__exit (machine); -free (machine); +if (!machine->allocated)
Sorry, should be
if (machine->allocated)
Will send v2.
Thank you.
PATCH 2/2 perf tools: Prevent calling machine__delete() on non-allocated machine
In reply to Wang NanTo prevent futher commits calling machine__delete() on non-allocated 'struct machine' (which would cause memory corruption), this patch enforces machine__init(), record whether a machine structure is dynamically allocated or not, and warn if machine__delete() is called on incorrect object.
---
v1 -> v2: Remove incorrect '!'.
--- tools/perf/tests/vmlinux-kallsyms.c | 4 ++-- tools/perf/util/machine.c | 14 +++++++++----- tools/perf/util/machine.h | 3 ++- 3 files changed, 13 insertions(+), 8 deletions(-)
tools/perf/tests/vmlinux-kallsyms.c +2 -2
@@ -35,8 +35,8 @@ int test__vmlinux_matches_kallsyms(int subtest __maybe_unused)* Init the machines that will hold kernel, modules obtained from * both vmlinux + .ko files and from /proc/kallsyms split by modules. */ -machine__init (&kallsyms,"" ,HOST_KERNEL_ID ); -machine__init (&vmlinux,"" ,HOST_KERNEL_ID ); +machine__init (&kallsyms,"" ,HOST_KERNEL_ID , false); +machine__init (&vmlinux,"" ,HOST_KERNEL_ID , false);/* * Step 2:
tools/perf/util/machine.c +9 -5
@@ -23,7 +23,7 @@ static void dsos__init(struct dsos *dsos)pthread_rwlock_init (&dsos->lock,NULL ); } -int machine__init (struct machine *machine,const char *root_dir,pid_t pid) +int machine__init (struct machine *machine,const char *root_dir,pid_t pid,bool allocated) {map_groups__init (&machine->kmaps, machine);RB_CLEAR_NODE (&machine->rb_node);
@@ -64,6 +64,7 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid) } machine->current_tid =NULL ; + machine->allocated = allocated;return 0 ; }
@@ -73,7 +74,7 @@ struct machine *machine__new_host(void)struct machine *machine =malloc (sizeof (*machine));if (machine !=NULL ) { -machine__init (machine,"" ,HOST_KERNEL_ID ); +machine__init (machine,"" ,HOST_KERNEL_ID , true);if (machine__create_kernel_maps (machine) <0 ) goto out_delete;
@@ -136,12 +137,15 @@ void machine__exit(struct machine *machine)void machine__delete (struct machine *machine) {machine__exit (machine); -free (machine); +if (machine->allocated) +free (machine); +else +pr_warning ("WARNING: delete a non-allocated machine. Skip.\n" ); }void machines__init (struct machines *machines) { -machine__init (&machines->host,"" ,HOST_KERNEL_ID ); +machine__init (&machines->host,"" ,HOST_KERNEL_ID , false); machines->guests =RB_ROOT ; machines->symbol_filter =NULL ; }
@@ -162,7 +166,7 @@ struct machine *machines__add(struct machines *machines, pid_t pid,if (machine ==NULL )return NULL ; -if (machine__init (machine, root_dir, pid) !=0 ) { +if (machine__init (machine, root_dir, pid, true) !=0 ) {free (machine);return NULL ; }
tools/perf/util/machine.h +2 -1
@@ -28,6 +28,7 @@ struct machine {pid_t pid;u16 id_hdr_size;bool comm_exec; +bool allocated;char *root_dir;struct rb_root threads;pthread_rwlock_t threads_lock;
@@ -131,7 +132,7 @@ void machines__set_symbol_filter(struct machines *machines,void machines__set_comm_exec (struct machines *machines,bool comm_exec);struct machine *machine__new_host (void ); -int machine__init (struct machine *machine,const char *root_dir,pid_t pid); +int machine__init (struct machine *machine,const char *root_dir,pid_t pid,bool allocated);void machine__exit (struct machine *machine);void machine__delete_threads (struct machine *machine);void machine__delete (struct machine *machine);
Signature · 1 line
1.8.3.4
RE: [PATCH 2/2 (v2)] perf tools: Prevent calling machine__delete() on non-allocated machine
Hi Wang,
Quoted 7 lines
From: Wang Nan [mailto:[email protected]]
To prevent futher commits calling
machine__delete()on non-allocated 'struct machine' (which would cause memory corruption), this patch enforcesmachine__init(), record whether a machine structure is dynamically allocated or not, and warn ifmachine__delete()is called on incorrect object.
Looks good to me :D
Thanks!
Quoted 110 lines
Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Jiri Olsa <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>---
v1 -> v2: Remove incorrect '!'.
---
tools/perf/tests/vmlinux-kallsyms.c | 4 ++-- tools/perf/util/machine.c | 14 +++++++++----- tools/perf/util/machine.h | 3 ++- 3 files changed, 13 insertions(+), 8 deletions(-)tools/perf/tests/vmlinux-kallsyms.c +2
-2@@ -35,8 +35,8 @@ int test__vmlinux_matches_kallsyms(int subtest __maybe_unused)* Init the machines that will hold kernel, modules obtained from * both vmlinux + .ko files and from /proc/kallsyms split by modules. */ -machine__init (&kallsyms,"" ,HOST_KERNEL_ID ); -machine__init (&vmlinux,"" ,HOST_KERNEL_ID ); +machine__init (&kallsyms,"" ,HOST_KERNEL_ID , false); +machine__init (&vmlinux,"" ,HOST_KERNEL_ID , false);/* * Step 2: tools/perf/util/machine.c +9
-5@@ -23,7 +23,7 @@ static void dsos__init(struct dsos *dsos)pthread_rwlock_init (&dsos->lock,NULL ); } -int machine__init (struct machine *machine,const char *root_dir,pid_t pid) +int machine__init (struct machine *machine,const char *root_dir,pid_t pid,bool allocated) {map_groups__init (&machine->kmaps, machine);RB_CLEAR_NODE (&machine->rb_node);@@ -64,6 +64,7 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid) } machine->current_tid =NULL ; + machine->allocated = allocated;return 0 ; }@@ -73,7 +74,7 @@ struct machine *machine__new_host(void)struct machine *machine =malloc (sizeof (*machine));if (machine !=NULL ) { -machine__init (machine,"" ,HOST_KERNEL_ID ); +machine__init (machine,"" ,HOST_KERNEL_ID , true);if (machine__create_kernel_maps (machine) <0 ) goto out_delete;@@ -136,12 +137,15 @@ void machine__exit(struct machine *machine)void machine__delete (struct machine *machine) {machine__exit (machine); -free (machine); +if (machine->allocated) +free (machine); +else +pr_warning ("WARNING: delete a non-allocated machine. Skip.\n" ); }void machines__init (struct machines *machines) { -machine__init (&machines->host,"" ,HOST_KERNEL_ID ); +machine__init (&machines->host,"" ,HOST_KERNEL_ID , false); machines->guests =RB_ROOT ; machines->symbol_filter =NULL ; }@@ -162,7 +166,7 @@ struct machine *machines__add(struct machines *machines, pid_t pid,if (machine ==NULL )return NULL ; -if (machine__init (machine, root_dir, pid) !=0 ) { +if (machine__init (machine, root_dir, pid, true) !=0 ) {free (machine);return NULL ; }tools/perf/util/machine.h +2
-2@@ -28,6 +28,7 @@ struct machine {pid_t pid;u16 id_hdr_size;bool comm_exec; +bool allocated;char *root_dir;struct rb_root threads;pthread_rwlock_t threads_lock;@@ -131,7 +132,7 @@ void machines__set_symbol_filter(struct machines *machines,void machines__set_comm_exec (struct machines *machines,bool comm_exec);struct machine *machine__new_host (void ); -int machine__init (struct machine *machine,const char *root_dir,pid_t pid); +int machine__init (struct machine *machine,const char *root_dir,pid_t pid,bool allocated);void machine__exit (struct machine *machine);void machine__delete_threads (struct machine *machine);void machine__delete (struct machine *machine); --
Quoted 1 line
1.8.3.4
���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m����������?�I�