Re: [PATCH v3 01/28] perf auxtrace: Remove dummy tools

From: Adrian Hunter
Date: Thu Jul 11 2024 - 02:46:56 EST


On 11/07/24 09:43, Adrian Hunter wrote:
> On 29/06/24 07:53, Ian Rogers wrote:
>> diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
>> index 0ec92d47373c..44db5b49128f 100644
>> --- a/tools/perf/util/session.c
>> +++ b/tools/perf/util/session.c
>> @@ -1764,6 +1764,36 @@ int perf_session__deliver_synth_event(struct perf_session *session,
>> return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0, NULL);
>> }
>>
>> +int perf_session__deliver_synth_attr_event(struct perf_session *session,
>> + const struct perf_event_attr *attr,
>> + u64 id)
>> +{
>> + union {
>> + struct {
>> + struct perf_record_header_attr attr;
>> + u64 ids[1];
>> + } attr_id;
>> + union perf_event ev;
>> + } ev = {
>> + .attr_id = {
>> + .attr = {
>> + .header = {
>> + .type = PERF_RECORD_HEADER_ATTR,
>> + .size = PERF_ALIGN(sizeof(struct perf_event_attr),
>> + sizeof(u64))
>> + + sizeof(struct perf_event_header)
>> + + sizeof(u64),
>> + },
>> + },
>> + .ids = { id, }
>> + },
>
> Nested definitions are not needed. It can just be:
>
> .attr_id.attr.header.type = PERF_RECORD_HEADER_ATTR,
> .attr_id.attr.header.size = sizeof(ev.attr_id),
> .attr_id.ids[0] = id,
>
>> + };
>> +
>> + memcpy(&ev.attr_id.attr.attr, attr, min((u32)sizeof(struct perf_event_attr), attr->size));
>
> It is not unreasonable to validate attr->size, but neither slicing
> nor padding will work. Better to just return an error:
>
> if (attr->size != sizeof(ev.attr_id.attr))

Or rather

if (attr->size != sizeof(ev.attr_id.attr.attr))

> return -EINVAL;
>
> Then:
>
> ev.attr_id.attr.attr = *attr;
>
>
>> + ev.attr_id.attr.attr.size = sizeof(struct perf_event_attr);
>> + return perf_session__deliver_synth_event(session, &ev.ev, NULL);
>> +}
>> +