Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

SvPV Segmentation Fault

by adler187 (Initiate)
on Apr 25, 2012 at 20:23 UTC ( #967160=perlquestion: print w/ replies, xml ) Need Help??
adler187 has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on an XS perl module and having troubles with an SV. From perlapi, SvPV should coerce any scalar to a string, but whenever I do this, I get a SegFault. Trying example #2 for Inline::C gives me the same. Here's some basic functions I've been testing:
void foo(SV * sv) { if(sv) { printf("%s\n", SvPV_nolen(sv)); } } void bar(SV * sv) { printf ("%i\n", SvPOK(sv)); } Mytest2::foo('asdf'); # prints asdf Mytest2::bar(1); # prints 0 Mytest2::foo(1); # Segmentation Fault
Do I really have to do something like this:
void foo(SV * sv) { char buf[1024]; switch(SvTYPE(sv)) { case SVt_IV: snprintf(buf, sizeof(buf), "%s\n", SvPV(sv)); break; case SVt_PV: snprintf(buf, sizeof(buf), "%d\n", SvIV(sv)); break; case SVt_NV: snprintf(buf, sizeof(buf), "%f\n", SvNV(sv)); break; ... handle scalar refs here ... } printf("%s\n", buf); }
This is on perl 5.14.2, linux x86-64

Comment on SvPV Segmentation Fault
Select or Download Code
Re: SvPV Segmentation Fault
by BrowserUk (Pope) on Apr 25, 2012 at 20:52 UTC

    Different platform, version and build environment, but it confirms that SvPV_nolen() should do what you think it should do:

    #! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => '_967160', CLEAN_AFTER_BUILD => 0; void foo( SV *sv ) { if( sv ) printf( "%s\n", SvPV_nolen( sv ) ); return; } END_C foo( 'fred' ); foo( 1 ); __END__ C:\test>967160 validate Stage get_maps Stage Writing Makefile for _967160 Microsoft (R) Program Maintenance Utility Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. C:\Perl64\bin\perl.exe C:\Perl64\lib\ExtUtils\xsubpp -typ cl -c -I"C:/test" -nologo -GF -W3 -MD -Zi -Ox -GL -fp:pr _967160.c Running Mkbootstrap for _967160 () C:\Perl64\bin\perl.exe -MExtUtils::Command -e chmod -- 644 C:\Perl64\bin\perl.exe -MExtUtils::Mksymlists -e "Mksymli link -out:blib\arch\auto\_967160\_967160.dll -dll -nologo Creating library blib\arch\auto\_967160\_967160.lib and object Generating code Finished generating code if exist blib\arch\auto\_967160\_967160.dll.manifest mt -n if exist blib\arch\auto\_967160\_967160.dll.manifest del b C:\Perl64\bin\perl.exe -MExtUtils::Command -e chmod -- 755 C:\Perl64\bin\perl.exe -MExtUtils::Command -e cp -- _96716 C:\Perl64\bin\perl.exe -MExtUtils::Command -e chmod -- 644 Microsoft (R) Program Maintenance Utility Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. Files found in blib\arch: installing files in blib\lib into archit Installing C:\test\_Inline\lib\auto\_967160\_967160.dll Installing C:\test\_Inline\lib\auto\_967160\_967160.exp Installing C:\test\_Inline\lib\auto\_967160\_967160.lib Installing C:\test\_Inline\lib\auto\_967160\_967160.pdb fred 1 C:\test>967160 fred 1

    It also works here on 5.14.2:

    Which leaves platform and build environment to isolate, with the latter the most likely the problem I think. Perhaps you could show your build process console log?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Ok, that worked:

      ./svpv.pl Starting Build Preprocess Stage + + + Finished Build Preprocess Stage + + + + + + Starting Build Parse Stage Finished Build Parse Stage Starting Build Glue 1 Stage Finished Build Glue 1 Stage Starting Build Glue 2 Stage Finished Build Glue 2 Stage Starting Build Glue 3 Stage Finished Build Glue 3 Stage Starting Build Compile Stage Starting "perl Makefile.PL" Stage Writing Makefile for _967160 Writing MYMETA.yml Finished "perl Makefile.PL" Stage Starting "make" Stage /usr/bin/perl /usr/lib/perl5/5.14.2/ExtUtils/xsubpp -typemap /usr/lib +/perl5/5.14.2/ExtUtils/typemap _967160.xs > _967160.xsc && mv _9671 +60.xsc _967160.c cc -c -I/home/kadler -D_REENTRANT -D_GNU_SOURCE -DPERL_USE_SAFE_PUTEN +V -fno-strict-aliasing -pipe -fstack-protector -D_LARGEFILE_SOURCE -D +_FILE_OFFSET_BITS=64 -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 + -fstack-protector -funwind-tables -fasynchronous-unwind-tables -g -W +all -pipe -DVERSION=\"0.00\" -DXS_VERSION=\"0.00\" -fPIC "-I/usr/li +b/perl5/5.14.2/x86_64-linux-thread-multi/CORE" _967160.c Running Mkbootstrap for _967160 () chmod 644 _967160.bs rm -f blib/arch/auto/_967160/_967160.so cc -shared -L/usr/local/lib64 -fstack-protector _967160.o -o blib/ar +ch/auto/_967160/_967160.so \ \ chmod 755 blib/arch/auto/_967160/_967160.so cp _967160.bs blib/arch/auto/_967160/_967160.bs chmod 644 blib/arch/auto/_967160/_967160.bs Finished "make" Stage Starting "make install" Stage Files found in blib/arch: installing files in blib/lib into architectu +re dependent library tree Installing /home/kadler/_Inline/lib/auto/_967160/_967160.bs Installing /home/kadler/_Inline/lib/auto/_967160/_967160.so Finished "make install" Stage Starting Cleaning Up Stage Finished Cleaning Up Stage Finished Build Compile Stage fred 1

      But, I wasn't actually using Inline::C. Just using that as an example of using SvPV and SvPV_nolen to build my XS module. There must be something wrong with building my XS module, then.

      I've been using perlxstut as a basis, specifically examples #4 and #5.

      Here's mylib.c:
      #include <stdlib.h> #include "mylib.h" PerlInterpreter *my_perl; void foo(SV * sv) { if(sv) { printf("%s\n", SvPV_nolen(sv)); } } void bar(SV * sv) { printf ("%i\n", SvPOK(sv)); }
      Here's mylib.h:
      #include "EXTERN.h" #include "perl.h" #define TESTVAL 4 void foo(SV * sv); void bar(SV * sv);
      Here's Mytest2.xs:
      #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include "mylib/mylib.h" #include "const-c.inc" MODULE = Mytest2 PACKAGE = Mytest2 INCLUDE: const-xs.inc void foo(SV * sv) void bar(SV * sv)
      Everything else should be exactly as in perlxstut.

        I'd start by looking for differences in the generated C files between the Inline::C and XS versions. Also, a comparison of the respective makefile.pl's might shed some light.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

        PerlInterpreter *my_perl
        That's very wrong. I don't know why you've put that there. You need to either pass the my_perl arg to the foo/bar functions using aTHX_/pTHX, or (less efficiently), retrieve it locally using dTHX.

        Dave.

Re: SvPV Segmentation Fault
by Khen1950fx (Canon) on Apr 25, 2012 at 22:35 UTC
    I tried it, keeping it simple---no segmentation fault.
    #!/usr/bin/perl -l use strict; use warnings; use Inline C => <<'END_C'; void foo( SV *sv ) { if( sv ) { printf( "%s\n", SvPV_nolen( sv ) ); return; } } void bar( SV *sv ) { printf( "%i\n", SvPOK( sv ) ); } END_C foo('asdf'); foo(1); bar(0);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967160]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2014-08-01 13:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (21 votes), past polls