|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [PATCH 4/4] automation/eclair: deviate MISRA C Rule 11.8 for xfree() and vfree()
On Wed, 18 Nov 2026, Jan Beulich wrote: > On 17.11.2026 23:35, Stefano Stabellini wrote: > > On Tue, 17 Nov 2026, Andrew Cooper wrote: > >> On 17/11/2026 8:01 pm, Dmytro Prokopchuk wrote: > >>> On 11/4/26 13:24, Andrew Cooper wrote: > >>>> On 03/11/2026 10:29 am, Dmytro Prokopchuk1 wrote: > >>>>> Livepatch passes const-qualified pointers from struct payload to > >>>>> xfree() > >>>>> and vfree(), which take a 'void *' argument. The cast that removes the > >>>>> const qualifier is a violation of MISRA C Rule 11.8. > >>>>> > >>>>> Such casts are safe. The deallocation functions neither read nor write > >>>>> the content of the object being freed, and the object is not accessed > >>>>> after it has been freed. > >>>> > >>>> I have a very firm objection to this. > >>>> > >>>> It is impossible for any allocation from malloc()/calloc()/etc to point > >>>> to read-only memory, which is why free() takes a mutable pointer, not a > >>>> const pointer. The same is true of any other heap implementation. > >>>> > >>>> It is impossible to make an object in read-only memory undefined. > >>>> Objects in read-only memory by definition have a global lifetime, and it > >>>> is absurd to argue that a specific heap implementation ought to permit > >>>> making such objects undefined. > >>>> > >>>> > >>>> The bug here is livepatch storing a heap allocation in a const pointer. > >>>> > >>>> This comes from deeper problem where some contributors/reviewers want > >>>> the const keyword to mean something it doesn't. > >>>> > >>>> ~Andrew > >>> > >>> Jan, Andrew, > >>> > >>> thanks for the review. Given the disagreement, I will drop this > >>> deviation for now. > >>> > >>> If the preferred fix is to stop storing heap allocations in > >>> const-qualified members of 'struct payload' (so the casts go away), I > >>> can prepare a follow-up patch. Please confirm that is the direction > >>> you want for livepatch. > >> > >> Please remove the const qualification in struct payload. The logic > >> should not have been accepted in that state to begin with. > >> > >> This is the only course of action here which does not create a class of > >> undefined behaviour, or permit said undefined behaviour to go unnoticed. > > > > I agree with Andrew. xfree() does write into the freed block, so a > > const parameter would be misleading. > > Well, this is the point: Yes, xfree() modifies the underlying memory. But > no, it does not modify the object a pointer to which was passed into it. > That object simply ceases to exist. And what C is concerned about are > objects, not the underlying memory. > > I also still don't quite understand why Andrew considers such uses of > const wrong: I don't think the spec says anything in this regard, and the > C library's free() taking plain void * can easily be seen as heritage. > Using const like this properly conveys to compiler and readers that post- > allocation / -population such objects aren't supposed to be altered > again. Together with forbidding the casting away of const-ness, this > serves as an extra level of safety of the code overall. >From a theoretical perspective I don't disagree with you, but there are two problems. First, in this specific case the const in struct payload is wrong regardless of the xfree() question: these fields are not read-only. rw_addr and text_addr are owning pointers to mutable buffers. Second, I would agree with you if free/unmap functions could take a pointer-to-const without casting const away internally. That is the case for vunmap() and unmap_domain_page(), which only use the pointer as an address, and it would be the case for vfree(), since _vfree() already takes const void *. It is not the case for xfree(). A const parameter would just move the Rule 11.8 cast from the callers into the allocator. This is not a good idea. So I think we should remove the const from those livepatch fields, that removes all six casts without any deviation. I don't mind adding const to vfree(), xvfree(), or other functions where it doesn't require casting const away in the implementation, but I don't think xfree() should be one of them.
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |