Experts on embedding Perl in C wanted: Weird problem on RH7.3/Perl 5.6.1

Discussion in 'Perl' started by David F. Skoll, Nov 5, 2003.

  1. Hi,

    I'm tearing my hair out on this one. I'm trying to embed a Perl
    interpreter into a C program. I need to be able to create and destroy
    the interpreter periodically, but will never actually have two interpreters
    at the same time.

    On Red Hat Linux 7.3 with Perl 5.6.1, the attached program segfaults. On
    Red Hat 9 with Perl 5.8.0, it works perfectly. Save the program code
    as "test-embed-perl.c" and the build script as "buildte". Then run:

    sh buildte
    ./te

    to see the behaviour. Enlightenment is appreciated! :-(

    A gdb stacktrace shows this:

    $ gdb ./te
    (gdb) run
    Program received signal SIGSEGV, Segmentation fault.
    0x080a098b in Perl_sv_upgrade ()
    (gdb) where
    #0 0x080a098b in Perl_sv_upgrade ()
    #1 0x080a4244 in Perl_sv_setpvn ()
    #2 0x080a708d in Perl_newSVpvn ()
    #3 0x0805abb9 in S_parse_body ()
    #4 0x0805a993 in perl_parse ()
    #5 0x08059a77 in make_embedded_interpreter () at test-embed-perl.c:30
    #6 0x08059aa1 in main () at test-embed-perl.c:39
    #7 0x400841c4 in __libc_start_main () from /lib/libc.so.6

    Thanks,

    David.

    ========= buildte =============
    #!/bin/sh
    echo cc -g `perl -MExtUtils::Embed -e ccopts` -c -o test-embed-perl.o
    test-embed-perl.c
    cc -g `perl -MExtUtils::Embed -e ccopts` -c -o test-embed-perl.o test-embed-perl.c
    echo cc -g `perl -MExtUtils::Embed -e ldopts` -o te test-embed-perl.o -lperl
    cc -g `perl -MExtUtils::Embed -e ldopts` -o te test-embed-perl.o -lperl

    ======== test-embed-perl.c =========
    #include <EXTERN.h>
    #include <perl.h>

    static PerlInterpreter *my_perl = NULL;

    int
    make_embedded_interpreter()
    {
    char *argv[5];
    int argc;

    if (my_perl != NULL) {
    perl_destruct(my_perl);
    perl_free(my_perl);
    my_perl = NULL;
    }
    my_perl = perl_alloc();
    if (!my_perl) {
    return -1;
    }
    PERL_SET_CONTEXT(my_perl);
    /* If you leave out the PL_perl_destruct_level lines, it "works",
    but leaks memory like crazy! */
    PL_perl_destruct_level = 1;
    perl_construct(my_perl);
    PL_perl_destruct_level = 1;
    argv[0] = "";
    argv[1] = "-e";
    argv[2] = "print(\"foo\\n\");";
    argv[3] = NULL;
    argc = 3;
    perl_parse(my_perl, NULL, argc, argv, NULL);
    perl_run(my_perl);
    return 0;
    }

    int
    main()
    {
    while(1) {
    make_embedded_interpreter();
    }
    }

    ============= Output of perl -V on system where it fails ============
    Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration:
    Platform:
    osname=linux, osvers=2.4.17-0.13smp, archname=i386-linux
    uname='linux daffy.perf.redhat.com 2.4.17-0.13smp #1 smp fri feb 1 10:30:48 est 2002 i686 unknown '
    config_args='-des -Doptimize=-O2 -march=i386 -mcpu=i686 -Dcc=gcc -Dcf_by=Red Hat, Inc. -Dcccdlflags=-fPIC -Dinstallprefix=/usr -Dprefix=/usr -Darchname=i386-linux -Dvendorprefix=/usr -Dsiteprefix=/usr -Uusethreads -Uuseithreads -Uuselargefiles -Dd_dosuid -Dd_semctl_semun -Di_db -Di_ndbm -Di_gdbm -Di_shadow -Di_syslog -Dman3ext=3pm'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=undef d_sfio=undef uselargefiles=undef usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    Compiler:
    cc='gcc', ccflags ='-fno-strict-aliasing -I/usr/local/include',
    optimize='-O2 -march=i386 -mcpu=i686',
    cppflags='-fno-strict-aliasing -I/usr/local/include'
    ccversion='', gccversion='2.96 20000731 (Red Hat Linux 7.2 2.96-109)', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=4
    alignbytes=4, usemymalloc=n, prototype=define
    Linker and Libraries:
    ld='gcc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -ldl -lm -lc -lcrypt -lutil
    perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil
    libc=/lib/libc-2.2.5.so, so=so, useshrplib=false, libperl=libperl.a
    Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'


    Characteristics of this binary (from libperl):
    Compile-time options:
    Built under linux
    Compiled at Apr 1 2002 12:23:22
    @INC:
    /usr/lib/perl5/5.6.1/i386-linux
    /usr/lib/perl5/5.6.1
    /usr/lib/perl5/site_perl/5.6.1/i386-linux
    /usr/lib/perl5/site_perl/5.6.1
    /usr/lib/perl5/site_perl/5.6.0
    /usr/lib/perl5/site_perl
    /usr/lib/perl5/vendor_perl/5.6.1/i386-linux
    /usr/lib/perl5/vendor_perl/5.6.1
    /usr/lib/perl5/vendor_perl
     
    David F. Skoll, Nov 5, 2003
    #1
    1. Advertising

  2. David F. Skoll

    dinser Guest

    Am new to perl but know C alright. Perhaps you
    need to allocate some memory to char *argv[]
    before you put values into it? Good luck.

    Ray


    > ======== test-embed-perl.c =========
    > #include <EXTERN.h>
    > #include <perl.h>
    >
    > static PerlInterpreter *my_perl = NULL;
    >
    > int
    > make_embedded_interpreter()
    > {
    > char *argv[5];
    > int argc;
    >
    > if (my_perl != NULL) {
    > perl_destruct(my_perl);
    > perl_free(my_perl);
    > my_perl = NULL;
    > }
    > my_perl = perl_alloc();
    > if (!my_perl) {
    > return -1;
    > }
    > PERL_SET_CONTEXT(my_perl);
    > /* If you leave out the PL_perl_destruct_level lines, it "works",
    > but leaks memory like crazy! */
    > PL_perl_destruct_level = 1;
    > perl_construct(my_perl);
    > PL_perl_destruct_level = 1;
    > argv[0] = "";
    > argv[1] = "-e";
    > argv[2] = "print(\"foo\\n\");";
    > argv[3] = NULL;
    > argc = 3;
    > perl_parse(my_perl, NULL, argc, argv, NULL);
    > perl_run(my_perl);
    > return 0;
     
    dinser, Nov 14, 2003
    #2
    1. Advertising

  3. dinser wrote:

    > Am new to perl but know C alright. Perhaps you
    > need to allocate some memory to char *argv[]
    > before you put values into it? Good luck.


    Nope; that's not it. I guess you're not as familiar with C as you think. :)

    Anyone else?

    Regards,

    David.

    >> ======== test-embed-perl.c =========
    >> #include <EXTERN.h>
    >> #include <perl.h>
    >>
    >> static PerlInterpreter *my_perl = NULL;
    >>
    >> int
    >> make_embedded_interpreter()
    >> {
    >> char *argv[5];
    >> int argc;
    >>
    >> if (my_perl != NULL) {
    >> perl_destruct(my_perl);
    >> perl_free(my_perl);
    >> my_perl = NULL;
    >> }
    >> my_perl = perl_alloc();
    >> if (!my_perl) {
    >> return -1;
    >> }
    >> PERL_SET_CONTEXT(my_perl);
    >> /* If you leave out the PL_perl_destruct_level lines, it "works",
    >> but leaks memory like crazy! */
    >> PL_perl_destruct_level = 1;
    >> perl_construct(my_perl);
    >> PL_perl_destruct_level = 1;
    >> argv[0] = "";
    >> argv[1] = "-e";
    >> argv[2] = "print(\"foo\\n\");";
    >> argv[3] = NULL;
    >> argc = 3;
    >> perl_parse(my_perl, NULL, argc, argv, NULL);
    >> perl_run(my_perl);
     
    David F. Skoll, Nov 14, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harvey
    Replies:
    0
    Views:
    792
    Harvey
    Jul 16, 2004
  2. Harvey
    Replies:
    1
    Views:
    899
    Daniel
    Jul 16, 2004
  3. david
    Replies:
    2
    Views:
    1,170
    david
    Nov 12, 2003
  4. Michal M
    Replies:
    7
    Views:
    825
    Andrew Thompson
    Aug 2, 2005
  5. Allan Adler

    anaconda.real in RH7.1

    Allan Adler, Sep 2, 2005, in forum: Python
    Replies:
    4
    Views:
    410
    Steve Holden
    Sep 7, 2005
Loading...

Share This Page