#11339: Illegal use of __deallocate__ in cython (pyx) code
-----------------------------------------------+----------------------------
   Reporter:  gagern                           |          Owner:  drkirkby      
                 
       Type:  defect                           |         Status:  needs_review  
                 
   Priority:  major                            |      Milestone:  sage-4.7.1    
                 
  Component:  porting                          |       Keywords:  sd31          
                 
Work_issues:                                   |       Upstream:  N/A           
                 
   Reviewer:  François Bissey, Steven Trogdon  |         Author:  Volker Braun, 
Martin von Gagern
     Merged:                                   |   Dependencies:                
                 
-----------------------------------------------+----------------------------
Changes (by newvalueoldvalue):

  * keywords:  => sd31
  * status:  needs_work => needs_review
  * author:  => Volker Braun, Martin von Gagern


Old description:

> As [https://github.com/cschwan/sage-on-
> gentoo/issues/51#issuecomment-1181259 described for the Sage on Gentoo
> project], there is a problem in how parts of sage use `__deallocate__` in
> their Cython code. I've actually traced an instance of
> [http://hg.sagemath.org/sage-
> main/file/361a4ad7d52c/sage/libs/singular/groebner_strategy.pyx#l134
> groebner_strategy.pyx] causing segmentation faults under Python 2.7.
>
> What happens is that the garbage collector invokes the
> [http://docs.python.org/c-api/typeobj.html#tp_clear tp_clear] function
> for the object. Its implementation is provided by Cython, and one of its
> effects is that every python reference will be set to `None`. A bit later
> on, [http://docs.python.org/c-api/typeobj.html#tp_dealloc tp_dealloc] is
> called and invokes the `__deallocate__` method. By that time, `_parent`
> is `None`, so accessing `_parent._ring` is a bad idea, and in this case
> it turns out to be null.
>
> Others have had similar problems before. There are
> [http://stackoverflow.com/questions/4501938/how-can-c-object-lifetimes-
> be-correctly-managed-in-cython crude workarounds] floating around. I
> guess a proper solution would be twaking cython to allow custom code in
> the tp_clear function. In other words, have a "magic" mehod `__clear__`
> similar to the magic `__deallocate__`. But I'll wait for comments here
> first, before taking this to cython upstream. Perhaps people with more
> experience have better solutions to offer. And I won't mind if you decide
> to take this to Cython devs yourselves.
>
> Apply:
>  * [attachment:bug11339a.patch]

New description:

 As [https://github.com/cschwan/sage-on-
 gentoo/issues/51#issuecomment-1181259 described for the Sage on Gentoo
 project], there is a problem in how parts of sage use `__deallocate__` in
 their Cython code. I've actually traced an instance of
 [http://hg.sagemath.org/sage-
 main/file/361a4ad7d52c/sage/libs/singular/groebner_strategy.pyx#l134
 groebner_strategy.pyx] causing segmentation faults under Python 2.7.

 What happens is that the garbage collector invokes the
 [http://docs.python.org/c-api/typeobj.html#tp_clear tp_clear] function for
 the object. Its implementation is provided by Cython, and one of its
 effects is that every python reference will be set to `None`. A bit later
 on, [http://docs.python.org/c-api/typeobj.html#tp_dealloc tp_dealloc] is
 called and invokes the `__deallocate__` method. By that time, `_parent` is
 `None`, so accessing `_parent._ring` is a bad idea, and in this case it
 turns out to be null.

 Others have had similar problems before. There are
 [http://stackoverflow.com/questions/4501938/how-can-c-object-lifetimes-be-
 correctly-managed-in-cython crude workarounds] floating around. I guess a
 proper solution would be twaking cython to allow custom code in the
 tp_clear function. In other words, have a "magic" mehod `__clear__`
 similar to the magic `__deallocate__`. But I'll wait for comments here
 first, before taking this to cython upstream. Perhaps people with more
 experience have better solutions to offer. And I won't mind if you decide
 to take this to Cython devs yourselves.

 Apply:
  *
 [attachment:trac_11339_illegal_use_of__deallocate__in_libsingular.patch]

--

Comment:

 In the case at hand we don't really need the Cython class
 `GroebnerStrategy._parent`, only the C struct
 `GroebnerStrategy._parent._ring` is used in `__deallocate__()`. I've made
 a minimal patch that adds a `cdef ring *_parent_ring` data member to
 `GroebnerStrategy` to keep track of the ring only. Because it refers to a
 C struct, it is safe to access it the Cython destructor. Because parents
 are immutable we don't have to worry about the `ring *` pointer becoming
 invalid.

 The patch should fix the crash, but I don't have a Sage-on-Python-2.7
 install to test it. Can somebody give it a try?

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/11339#comment:16>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to