Skip to contentSkip to conversation

perf record: Fix memset out-of-range error

PATCH Applied Haoze Xie 2 participants 2 messages Jul 7, 2024 – Jul 12, 2024 1 message not in the archive perf events

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Haoze Xie

author
#1 lore raw

PATCH 1/1 perf record: Fix memset out-of-range error

Modified the object of 'memset' from '&lost.lost' to '&lost' in record__read_lost_samples. This allows 'memset' to access memory properly without causing out-of-bounds problems.

The problems got from builtin-record.c are:

In file included from /usr/include/string.h:495,
from util/parse-events.h:13,
from builtin-record.c:14:
In function 'memset',
inlined from 'record__read_lost_samples' at builtin-record.c:1958:6,
inlined from '__cmd_record.constprop' at builtin-record.c:2817:2:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:71:10: error:
'__builtin_memset' offset [17, 64] from the object at 'lost' is out of the bounds of referenced subobject 'lost' with type 'struct perf_record_lost_samples' at offset 0 [-Werror=array-bounds] 71|return __builtin___memset_chk (__dest,__ch,__len,__bos0 (__dest));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The error arised when performing a memset operation on the 'lost' variable, the bytes of 'sizeof(lost)' exceeds that of '&lost.lost', which are 64 and 16.

Fixes 6c1785cd75ef ("perf record: Ensure space for lost samples")
---
 tools/perf/builtin-record.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/builtin-record.c +2 -2
index 019305b94e5f..a94516e8c522 100644
@@ -1955,7 +1955,7 @@ static void record__read_lost_samples(struct record *rec)
 				}
 
 				if (count.lost) {
-					memset(&lost.lost, 0, sizeof(lost));
+					memset(&lost, 0, sizeof(lost));
 					lost.lost.header.type = PERF_RECORD_LOST_SAMPLES;
 					__record__save_lost_samples(rec, evsel, &lost.lost,
 								    x, y, count.lost, 0);
@@ -1965,7 +1965,7 @@ static void record__read_lost_samples(struct record *rec)
 
 		lost_count = perf_bpf_filter__lost_count(evsel);
 		if (lost_count) {
-			memset(&lost.lost, 0, sizeof(lost));
+			memset(&lost, 0, sizeof(lost));
 			lost.lost.header.type = PERF_RECORD_LOST_SAMPLES;
 			__record__save_lost_samples(rec, evsel, &lost.lost, 0, 0, lost_count,
 						    PERF_RECORD_MISC_LOST_SAMPLES_BPF);
Signature · 1 line
2.25.1
maintainer Applied
#2 lore raw
Quoted 20 lines · Haoze Xie

Modified the object of 'memset' from '&lost.lost' to '&lost' in record__read_lost_samples. This allows 'memset' to access memory properly without causing out-of-bounds problems.

The problems got from builtin-record.c are:

In file included from /usr/include/string.h:495,
from util/parse-events.h:13,
from builtin-record.c:14:
In function 'memset',
inlined from 'record__read_lost_samples' at builtin-record.c:1958:6,
inlined from '__cmd_record.constprop' at builtin-record.c:2817:2:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:71:10: error:
'__builtin_memset' offset [17, 64] from the object at 'lost' is out of the bounds of referenced subobject 'lost' with type 'struct perf_record_lost_samples' at offset 0 [-Werror=array-bounds] 71|return __builtin___memset_chk (__dest,__ch,__len,__bos0 (__dest));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Quoted 1 line

[...]

Applied to perf-tools-next, thanks!

Best regards,
Namhyung