tech-userlevel archive

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

Re: in which we present an ugly hack to make sys/queue.h CIRCLEQ work



On 22 Nov, 2013, at 21:40 , David Holland <dholland-tech%netbsd.org@localhost> 
wrote:
>> So ... looking at this code ... it seems like the core problem is that
>> TAILQ_HEAD and TAILQ_ENTRY are two different types (even though they
>> literally the same structure layout).  So if TAILQ_HEAD and TAILQ_ENTRY
>> were the same structure, it wouldn't be an issue.  It doesn't quite leap
>> out to me how that would be possible without changing the API a bit.
> 
> I think it can be done by sticking an anonymous union into TAILQ_HEAD,
> but of course anonymous unions aren't supported until C11.

It isn't perfectly clear to me that this code has an aliasing problem
the way it is, though.  The only thing that matters in the standard are
the types of the lvalue expressions used to access object in storage.  The
lvalue expression types used to access the objects in storage in this
case are 'type **', 'type **' and 'type *', which are the types those
objects were stored with and the types that would be used for other
accesses to the same locations.  The structure type used to arrive there
should only matter if it is the type of an lvalue expression itself,
e.g. *(struct foo *)ptr(?).

I would be interested in knowing an actual example of the comparison
problem with the CIRCLEQ macro, if the concern isn't theoretical.  Since
the C standard explicitly allows a pointer to a structure type to be
converted to the type of its first member and back, to another structure
type and back, or to char * or void * and back, the fact that the two
pointers point at different structure types is by itself insufficient to
prove that they would not compare equal when suitably converted.  It seems
like that conclusion would minimally need to depend on proving that there
was no possible use of the structure pointers which wouldn't violate the
aliasing requirements, i.e. that that are no structure members at the same
offsets which have compatible types.  That's a rather aggressive optimization,
and is kind of like throwing you in jail for a crime you haven't actually
committed yet (though I guess that happens too).

Dennis Ferguson


Home | Main Index | Thread Index | Old Index