Re: [PATCH bpf-next] bpf: avoid uninitialized value in BPF_CORE_READ_BITFIELD

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> On 5/7/24 4:39 AM, Jose E. Marchesi wrote:
>> GCC warns that `val' may be used uninitialized in the
>> BPF_CORE_READ_BITFIELD macro, defined in bpf_core_read.h as:
>>
>> 	[...]
>> 	unsigned long long val;						      \
>> 	[...]								      \
>> 	switch (__CORE_RELO(s, field, BYTE_SIZE)) {			      \
>> 	case 1: val = *(const unsigned char *)p; break;			      \
>> 	case 2: val = *(const unsigned short *)p; break;		      \
>> 	case 4: val = *(const unsigned int *)p; break;			      \
>> 	case 8: val = *(const unsigned long long *)p; break;		      \
>>          }       							      \
>> 	[...]
>> 	val;								      \
>> 	}								      \
>>
>> This patch initializes `val' to zero in order to avoid the warning,
>> and random values to be used in case __builtin_preserve_field_info
>> returns unexpected values for BPF_FIELD_BYTE_SIZE.
>
> In clang, __builtin_preserve_field_info either returns correct value
> or caused compilation error. Do you mean for gcc __builtin_preserve_field_info
> might return an unexpected value here?

The __builtin_preserve_field_info implementation in GCC will emit an
error if the size of the bitfield is not a power of two.  It doesn't
check that the bitfield is 64-bit or smaller, but that should not be a
problem.

So I would say we are ok there.

> BTW, your change makes sense to silent this warning. So Ack below.
>
>
>>
>> Tested in bpf-next master.
>> No regressions.
>>
>> Signed-off-by: Jose E. Marchesi <jose.marchesi@xxxxxxxxxx>
>> Cc: david.faust@xxxxxxxxxx
>> Cc: cupertino.miranda@xxxxxxxxxx
>> Cc: Eduard Zingerman <eddyz87@xxxxxxxxx>
>> Cc: Yonghong Song <yonghong.song@xxxxxxxxx>
>
> Acked-by: Yonghong Song <yonghong.song@xxxxxxxxx>
>
>> ---
>>   tools/lib/bpf/bpf_core_read.h | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h
>> index b5c7ce5c243a..88d129b5f0a1 100644
>> --- a/tools/lib/bpf/bpf_core_read.h
>> +++ b/tools/lib/bpf/bpf_core_read.h
>> @@ -89,7 +89,7 @@ enum bpf_enum_value_kind {
>>    */
>>   #define BPF_CORE_READ_BITFIELD(s, field) ({				      \
>>   	const void *p = (const void *)s + __CORE_RELO(s, field, BYTE_OFFSET); \
>> -	unsigned long long val;						      \
>> +	unsigned long long val = 0;					      \
>>   									      \
>>   	/* This is a so-called barrier_var() operation that makes specified   \
>>   	 * variable "a black box" for optimizing compiler.		      \




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux