in reply to Mark-and-sweep-like mechanism to verify refcount integrity

I'd think that this would be tricky. The problem is that while you might track what Perl is doing (be careful to not follow weak references!), various XS modules are going to have pointers into Perl data structures that come from non-Perl data structures. How do you find and account for those?

A cheesy but possibly effective way to solve this problem is build a custom version of Perl that allows you to log all calls to increment/decrement a particular variable. Install that in a custom location, recompile all of your XS modules against that, and see what you get.

Glancing at the code, you'd want to redefine SvREFCNT_inc and SvREFCNT_dec in sv.h. The latter is easier to add custom logic to - it calls sv_free in sv.c so you can edit that. The former is an inline macro that may be harder to edit.

From an uninformed glance at the code, if you want a custom per-scalar flag the sv_flag's struct has 8 more flags available: 0x00000010, 0x00000020, 0x00000040, 0x00000080, 0x00000001, 0x00000002, 0x00000004, and 0x00000008. However seeing how much work is going into avoiding using those makes me suspect that someone, somewhere uses them in a way that is not obvious to me.

  • Comment on Re: Mark-and-sweep-like mechanism to verify refcount integrity

Replies are listed 'Best First'.
Re^2: Mark-and-sweep-like mechanism to verify refcount integrity
by nobull (Friar) on Oct 06, 2004 at 10:55 UTC
    I'd think that this would be tricky. The problem is that while you might track what Perl is doing (be careful to not follow weak references!), various XS modules are going to have pointers into Perl data structures that come from non-Perl data structures. How do you find and account for those?

    I wouldn't bother. The tool can simply report those as refcount inconsitancies. The tool could show all the paths if found to each SV together with the reference count on the SV. If there are legitmate refernce paths through non-perlish structures then these should show up.

    That said, when I write XS modules I avoid this problem. If I need my objects to contain hidden internal stuff that holds references to SVs I do so by hanging Perl structures off of the MAGIC list. In this way my private stuff is hidden from the Perl programmer but could still be traversed by the proposed tool.