perf record: Fix segfault with '--timestamp-filename' option and pipe mode
cover perf record: Fix segfault with '--timestamp-filename' option and pipe mode
When perf record uses '--timestamp-filename' option and pipe mode, will occur segfault:
# perf record --timestamp-filename -o- true 1>/dev/null [ perf record: Woken up 1 times to write data ] [ perf record: Dump -.2024011813361681 ] perf: Segmentation fault Segmentation fault (core dumped)
Debug the core file by using the gdb:
# gdb perf core.3706841 <SNIP> [New LWP 3706841] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Core was generated by `perf record --timestamp-filename -o- true'. Program terminated with signal SIGSEGV, Segmentation fault. #0 evsel__free_stat_priv (evsel=0x555ea56197af) at util/stat.c:145 145 struct perf_stat_evsel *ps = evsel->stats; (gdb) bt #0 evsel__free_stat_priv (evsel=0x555ea56197af) at util/stat.c:145 #1 evlist__free_stats (evlist=evlist@entry=0x555bf0dad1b0) at util/stat.c:215 #2 0x0000555befd425e8 in evlist__delete (evlist=0x555bf0dad1b0) at util/evlist.c:175 #3 0x0000555befd42785 in evlist__delete (evlist=<optimized out>) at util/evlist.c:181 #4 0x0000555befc7f547 in cmd_record (argc=<optimized out>, argv=<optimized out>) at builtin-record.c:4252 #5 0x0000555befd27700 in run_builtin (p=p@entry=0x555bf05acf88 <commands+264>, argc=argc@entry=4, argv=argv@entry=0x7ffc2696a920) at perf.c:349 #6 0x0000555befc68751 in handle_internal_command (argv=0x7ffc2696a920, argc=4) at perf.c:402 #7 run_argv (argv=<synthetic pointer>, argcp=<synthetic pointer>) at perf.c:446 #8 main (argc=4, argv=0x7ffc2696a920) at perf.c:562
It is found that the memory of the evsel is modified.
The reason is that perf_data__switch() not initialized 'new_filename', as a result, it uses the on-stack variable. It happens that the evsel address is stored here. 'new_filename' is freed later when uses '--timestamp-filename' option. Therefore, segfault occurs in the evsel_delete().
1. patch1 fixes this problem.
2. patch2 to optimize the process, check conflict between '--timestamp-filename' option and the pipe mode before recording to avoid switch perf data.
3. patch3 fixes the code style problem by the way.
Yang Jihong (3):
perf record: Fix possible incorrect free in record__switch_output() perf record: Check conflict between '--timestamp-filename' option and
pipe mode before recording
perf data: Minor code style alignment cleanup
tools/perf/builtin-record.c | 14 ++++++++++---- tools/perf/util/data.c | 10 ++++------ tools/perf/util/data.h | 6 +++--- 3 files changed, 17 insertions(+), 13 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 perf record: Fix possible incorrect free in record__switch_output()
perf_data__switch() may not assign a legal value to 'new_filename'. In this case, 'new_filename' uses the on-stack value, which may cause a incorrect free and unexpected result.
--- tools/perf/builtin-record.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-record.c +1 -1
@@ -1830,8 +1830,8 @@ static int record__switch_output(struct record *rec, bool at_exit) {struct perf_data *data = &rec->data; +char *new_filename =NULL ;int fd, err; -char *new_filename;/* Same Size: "2015122520103046"*/ char timestamp[] ="InvalidTimestamp" ;
Signature · 1 line
2.34.1
PATCH 2/3 perf record: Check conflict between '--timestamp-filename' option and pipe mode before recording
In reply to Yang JihongIn pipe mode, no need to switch perf data output, therefore, '--timestamp-filename' option should not take effect.
Check the conflict before recording and output WARNING.
In this case, the check pipe mode in perf_data__switch() can be removed.
Before:
# perf record --timestamp-filename -o- perf test -w noploop | perf report -i- --percent-limit=1
# To display the perf.data header info, please use --header/--header-only options.
#
[ perf record: Woken up 1 times to write data ]
[ perf record: Dump -.2024011812110182 ]
#
# Total Lost Samples: 0
#
# Samples: 4K of event 'cycles:P'
# Event count (approx.): 2176784359
#
# Overhead Command Shared Object Symbol
# ........ ....... .................... ......................................
#
97.83% perf perf [.] noploop #
# (Tip: Print event counts in CSV format with: perf stat -x,) #
After:
# perf record --timestamp-filename -o- perf test -w noploop | perf report -i- --percent-limit=1
WARNING: --timestamp-filename option is not available in pipe mode.
# To display the perf.data header info, please use --header/--header-only options.
#
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.000 MB - ]
#
# Total Lost Samples: 0
#
# Samples: 4K of event 'cycles:P'
# Event count (approx.): 2185575421
#
# Overhead Command Shared Object Symbol
# ........ ....... ..................... .............................................
#
97.75% perf perf [.] noploop #
# (Tip: Profiling branch (mis)predictions with: perf record -b / perf report) #
--- tools/perf/builtin-record.c | 5 +++++ tools/perf/util/data.c | 2 -- 2 files changed, 5 insertions(+), 2 deletions(-)
tools/perf/builtin-record.c +5 -0
@@ -2472,6 +2472,11 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)if (data->is_pipe && rec->evlist->core.nr_entries ==1 ) rec->opts.sample_id = true; +if (rec->timestamp_filename &&perf_data__is_pipe (data)) { + rec->timestamp_filename = false; +pr_warning ("WARNING: --timestamp-filename option is not available in pipe mode.\n" ); + } +evlist__uniquify_name (rec->evlist);/* Debug message used by test scripts */
tools/perf/util/data.c +0 -2
@@ -430,8 +430,6 @@ int perf_data__switch(struct perf_data *data, {int ret; -if (check_pipe (data)) -return -EINVAL ;if (perf_data__is_read (data))return -EINVAL ;
Signature · 1 line
2.34.1
PATCH 3/3 perf data: Minor code style alignment cleanup
In reply to Yang JihongMinor code style alignment cleanup for perf_data__switch() and perf_data__write().
No functional change.
--- tools/perf/builtin-record.c | 7 ++++--- tools/perf/util/data.c | 8 ++++---- tools/perf/util/data.h | 6 +++--- 3 files changed, 11 insertions(+), 10 deletions(-)
tools/perf/builtin-record.c +4 -3
@@ -1853,16 +1853,17 @@ record__switch_output(struct record *rec, bool at_exit) } fd =perf_data__switch (data, timestamp, - rec->session->header.data_offset, - at_exit, &new_filename); + rec->session->header.data_offset, + at_exit, &new_filename);if (fd >=0 && !at_exit) { rec->bytes_written =0 ; rec->session->header.data_size =0 ; } -if (!quiet) +if (!quiet) {fprintf (stderr,"[ perf record: Dump %s.%s ]\n" , data->path, timestamp); + }if (rec->switch_output.num_files) {int n = rec->switch_output.cur_file +1 ;
tools/perf/util/data.c +4 -4
@@ -413,7 +413,7 @@ ssize_t perf_data_file__write(struct perf_data_file *file, }ssize_t perf_data__write (struct perf_data *data, -void *buf,size_t size) +void *buf,size_t size) {if (data->use_stdio) {if (fwrite (buf, size,1 , data->file.fptr) ==1 )
@@ -424,9 +424,9 @@ ssize_t perf_data__write(struct perf_data *data, }int perf_data__switch (struct perf_data *data, -const char *postfix, -size_t pos,bool at_exit, -char **new_filepath) +const char *postfix, +size_t pos,bool at_exit, +char **new_filepath) {int ret;
tools/perf/util/data.h +3 -3
@@ -80,7 +80,7 @@ int perf_data__open(struct perf_data *data);void perf_data__close (struct perf_data *data);ssize_t perf_data__read (struct perf_data *data,void *buf,size_t size);ssize_t perf_data__write (struct perf_data *data, -void *buf,size_t size); +void *buf,size_t size);ssize_t perf_data_file__write (struct perf_data_file *file,void *buf,size_t size);/*
@@ -91,8 +91,8 @@ ssize_t perf_data_file__write(struct perf_data_file *file,* Return value is fd of new output. */ int perf_data__switch (struct perf_data *data, -const char *postfix, -size_t pos,bool at_exit,char **new_filepath); +const char *postfix, +size_t pos,bool at_exit,char **new_filepath);int perf_data__create_dir (struct perf_data *data,int nr);int perf_data__open_dir (struct perf_data *data);
Signature · 1 line
2.34.1
Hello,
Quoted 46 lines · Yang Jihong
When perf record uses '--timestamp-filename' option and pipe mode, will occur segfault:
# perf record --timestamp-filename -o- true 1>/dev/null [ perf record: Woken up 1 times to write data ] [ perf record: Dump -.2024011813361681 ] perf: Segmentation fault Segmentation fault (core dumped)Debug the core file by using the gdb:
# gdb perf core.3706841 <SNIP> [New LWP 3706841] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Core was generated by `perf record --timestamp-filename -o- true'. Program terminated with signal SIGSEGV, Segmentation fault. #0 evsel__free_stat_priv (evsel=0x555ea56197af) at util/stat.c:145 145 struct perf_stat_evsel *ps = evsel->stats; (gdb) bt #0 evsel__free_stat_priv (evsel=0x555ea56197af) at util/stat.c:145 #1 evlist__free_stats (evlist=evlist@entry=0x555bf0dad1b0) at util/stat.c:215 #2 0x0000555befd425e8 in evlist__delete (evlist=0x555bf0dad1b0) at util/evlist.c:175 #3 0x0000555befd42785 in evlist__delete (evlist=<optimized out>) at util/evlist.c:181 #4 0x0000555befc7f547 in cmd_record (argc=<optimized out>, argv=<optimized out>) at builtin-record.c:4252 #5 0x0000555befd27700 in run_builtin (p=p@entry=0x555bf05acf88 <commands+264>, argc=argc@entry=4, argv=argv@entry=0x7ffc2696a920) at perf.c:349 #6 0x0000555befc68751 in handle_internal_command (argv=0x7ffc2696a920, argc=4) at perf.c:402 #7 run_argv (argv=<synthetic pointer>, argcp=<synthetic pointer>) at perf.c:446 #8 main (argc=4, argv=0x7ffc2696a920) at perf.c:562It is found that the memory of the evsel is modified.
The reason is thatperf_data__switch()not initialized 'new_filename', as a result, it uses the on-stack variable. It happens that the evsel address is stored here. 'new_filename' is freed later when uses '--timestamp-filename' option. Therefore, segfault occurs in theevsel_delete().1. patch1 fixes this problem.
2. patch2 to optimize the process, check conflict between '--timestamp-filename' option and the pipe mode before recording to avoid switch perf data.
3. patch3 fixes the code style problem by the way.
Quoted 5 lines
Yang Jihong (3):
perf record: Fix possible incorrect free inrecord__switch_output()perf record: Check conflict between '--timestamp-filename' option and
pipe mode before recording
perf data: Minor code style alignment cleanup
Thanks,
Namhyung
Quoted 9 lines
tools/perf/builtin-record.c | 14 ++++++++++---- tools/perf/util/data.c | 10 ++++------ tools/perf/util/data.h | 6 +++--- 3 files changed, 17 insertions(+), 13 deletions(-)--
2.34.1
Quoted 10 lines · Yang Jihong
When perf record uses '--timestamp-filename' option and pipe mode, will occur segfault:
# perf record --timestamp-filename -o- true 1>/dev/null [ perf record: Woken up 1 times to write data ] [ perf record: Dump -.2024011813361681 ] perf: Segmentation fault Segmentation fault (core dumped)[...]
Applied to perf-tools-next, thanks!
Namhyung