Skip to contentSkip to conversation

libbpf ringbuffer callback prototype

DISCUSSION Martin Kelly 2 participants 2 messages Sep 7, 2023 – Sep 8, 2023 BPF

lore
Share & tools
lore
#0 lore raw

I noticed that the data pointer in the libbpf ringbuffer callback has type void *, without const:

typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);

However, if you actually try to write to this data, you'll get a SIGSEGV.

It seems to me the prototype should ideally be const void * so the compiler can throw -Wdiscarded-qualifiers if someone tries to assign it to a non-const type. I'm not sure there's anything to be done about it, as changing it now would break the API. However, I wanted to mention it just in case anyone has thoughts about this.

Thanks,

Martin

#1 lore raw
Quoted 6 lines · Martin Kelly

I noticed that the data pointer in the libbpf ringbuffer callback has type void *, without const:

typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);

Quoted 7 lines

However, if you actually try to write to this data, you'll get a SIGSEGV.

It seems to me the prototype should ideally be const void * so the compiler can throw -Wdiscarded-qualifiers if someone tries to assign it to a non-const type. I'm not sure there's anything to be done about it, as changing it now would break the API. However, I wanted to mention it just in case anyone has thoughts about this.

Yeah, that's an unfortunate omission... Kernel never allowed to mmap data area as writable, so there is no way to ever write anything back through this pointer.

But as you said, adding that const in a callback type would cause compilation error for every single customer. So it seems just not worth it at this point.

Quoted 6 lines

Thanks,

Martin