[Pyrex] gcc's -fstrict-aliasing optimization can break vtable construction in PyMODINIT_FUNC

Lenard Lindstrom len-l at telus.net
Sat Nov 24 05:01:00 CET 2007


Dan Gindikin wrote:
> I have a module that works with "-O6 -fno-strict-aliasing", but breaks with just
> "-O6". I chased this down to the bitter end, here is what I think happens, I 
> have the following code:
>
> 	cdef class Base:
> 		cdef void F(self):
> 			...
>
> 	cdef class Derived(Base):
> 		cdef void F(self):
> 			...
>
> 	cdef Derived d=Derived()
>
> 	d.F() # <--- This in fact calls Base.F(), not Derived.F()
>
>   

I reproduced the above problem with gcc 3.4.5 using "-O3". Adding an 
"-fno-strict-aliasing" also fixes it.

> I believe what happens, is that because of the strict aliasing assumption, gcc 
> re-orders the writes to the vtables in PyMODINIT_FUNC(), and the function 
> pointer of Derived.F gets assigned to the vtable before the assignment of 
> Base.F, even though in the code it comes later.
>
> Has anyone seen something like this before?
>
>   

The problem is more complicated than strict aliasing. Try compile with 
"-O2". The C compiler complains but the d.F() method call works 
correctly. So the problem involves a combination of optimizations.

-- 
Lenard Lindstrom
<len-l at telus.net>




More information about the Pyrex mailing list