_Region_Get and strict aliasing

Steven Johnson sjohnson at sakuraindustries.com
Wed Dec 20 05:19:42 UTC 2006

Well in that case, isn't the following in RTEMS broken RE strict 
aliasing (Which the _Region_Get code pointed me to):

chain.h states:

 *  @struct Chain_Node_struct
 *  This is used to manage each element (node) which is placed
 *  on a chain.
 *  @note Typically, a more complicated structure will use the
 *        chain package.  The more complicated structure will
 *        include a chain node as the first element in its
 *        control structure.  It will then call the chain package
 *        with a pointer to that node element.  The node pointer
 *        and the higher level structure start at the same address
 *        so the user can cast the pointers back and forth.
struct Chain_Node_struct {
  /** This points to the node after to this one on this chain. */
  Chain_Node *next;
  /** This points to the node immediate prior to this one on this chain. */
  Chain_Node *previous;

so the whole premise of the chain code is that a cast will be performed 
from some "more complicated" structure, that starts with a Chain_Node 
structure as its first element, down to a Chain_Node_Structure, so the 
generic chain code can be used on it (as the chain code doesn't care 
about what is held by the rest of the structure).

There are lots of these types of structures in the code, Region_Control 
is one, and use of the Chain_Node fields in those structures by the 
chain code (when it is cast as the generic type of Chain_Node) will be a 
violation of this rule. No?

As in:


which dereferences a chain_node*, which is going to be pointing to an 
effective type that is not a chain_node (as there are no pure Chain_Node 
structures in use, that I can find, as they convey no information).  So 
any use of (a lot of) the code in chain.inl will violate the strict 
aliasing rules, because the whole purpose of this code is to dereference 
type punned pointers?

Steven J


Till Straumann wrote:
> Welcome on board. It seems you got it.
> T.
> Steven Johnson wrote:
>> Lets see if I follow your logic,
>> You say, because the pointer to the object returned by _Region_Get 
>> may have been at its creation a Region_Control structure, then the 
>> casting doesn't change that effective type, and so the pointer to the 
>> object can be cast to anything (provided that the cast pointer is not 
>> accessed) and when it is cast back then it can still be accessed as a 
>> Region_Control structure.  It would just be an invalid alias to 
>> access it through one of it's type cast intermediaries.
>> Is this the basis of your argument that this is not an illegal alias?
>> So the following applies:
>> int a;
>> long *b = (long*)&a;
>> ...
>> short *c = (shortc*)b;
>> ...
>> void *d = (void*)c;
>> ...
>> int *e = (int*)d;
>> long* f = (long*)d;
>> In the above ... means a lot of code, function calls, indirection, 
>> etc, that mean the true effective type of the object previously cast 
>> can not be determined by the compiler.
>> One can access *e to get at 'a', but any use of *c, *d or *f to get 
>> at any part of the object 'a' would be an invalid alias.  Even if 
>> their was no possible way for the compiler to determine that the 
>> object *c, *d and *f points to has an effective type of int.
>> Steven J
>> _______________________________________________
>> rtems-users mailing list
>> rtems-users at rtems.com
>> http://rtems.rtems.org/mailman/listinfo/rtems-users

More information about the users mailing list