Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perl script failure with "Attempt to free unreferenced scalar" #22155

Closed
reshmavk opened this issue Apr 18, 2024 · 13 comments
Closed

Perl script failure with "Attempt to free unreferenced scalar" #22155

reshmavk opened this issue Apr 18, 2024 · 13 comments

Comments

@reshmavk
Copy link

We have a locally created perl script/XS that works with perl 5.34.1. When we updated perl to version 5.38.2, these scripts fail with the following errors

Attempt to free unreferenced scalar: SV 0xd, Perl interpreter: 0x200073a8 at XX.pm line 1224.
panic: unknown regstclass 0 at XX.pm line 1224.

We see this failure because of this commit 75acd14
Any idea what is causing this issue?

@jkeenan
Copy link
Contributor

jkeenan commented Apr 18, 2024

We have a locally created perl script/XS that works with perl 5.34.1. When we updated perl to version 5.38.2, these scripts fail with the following errors

Attempt to free unreferenced scalar: SV 0xd, Perl interpreter: 0x200073a8 at XX.pm line 1224.
panic: unknown regstclass 0 at XX.pm line 1224.

We see this failure because of this commit 75acd14 Any idea what is causing this issue?

Can you provide a reduced version of your program that works in 5.34 but generates that panic in 5.38? Thanks.

@reshmavk
Copy link
Author

reshmavk commented Apr 19, 2024

Since it is proprietary code, exact code cannot be shared here. But when something like below is done , we get the error.

sub test {
  my($s,$q)=@_;
  return undef unless defined $s;
  $q = '' unless defined $q;
  return($s =~ m/^$q$/);
}

Perl 5.38.2 is built using xlc 13.1.3 and the platform is AIX 7.3.

@iabyn
Copy link
Contributor

iabyn commented Apr 19, 2024 via email

@reshmavk
Copy link
Author

Thanks for the reply.
Yes, it uses private XS code written for the application. I will try to come up with a standalone program that can reproduce this problem. In the meanwhile, can you provide me some thoughts on where to look into in the XS code? (since the issue is seen after this commit 75acd14 which makes the newSV_type an inline function)

@iabyn
Copy link
Contributor

iabyn commented Apr 19, 2024 via email

@reshmavk
Copy link
Author

Thanks for the information. It will be helpful in debugging this issue further.

@reshmavk
Copy link
Author

After rebuilding perl 5.38.2 on AIX with -DDEBUGGING and -DDEBUG_LEAKING_SCALARS, our local perl script fails with Segmentation fault(coredump) . Following is the stack trace.

XX.Perl_newSV_type(my_perl = 0x20007428, type = SVt_PVHV), line 417 in "sv_inline.h"
XX_init(), line 209 in "XX.xs"
unnamed block in XS_XX_fp(my_perl = 0x20007428, cv = 0x200fadd8), line 247 in "XX.xs"
XS_XX_fp(my_perl = 0x20007428, cv = 0x200fadd8), line 247 in "XX.xs"
Perl_pp_entersub(my_perl = 0x20007428), line 5555 in "pp_hot.c"
Perl_runops_debug(my_perl = 0x20007428), line 2861 in "dump.c"
S_run_body(my_perl = 0x20007428, oldscope = 1), line 2812 in "perl.c"
perl_run(my_perl = 0x20007428), line 2727 in "perl.c"

Any thoughts?

@tonycoz
Copy link
Contributor

tonycoz commented Apr 30, 2024

Did you rebuild your XS module for the rebuilt perl?

DEBUG_LEAKING_SCALARS changes the layout of the interpreter structure, which could cause a SEGV like this if the module hasn't been rebuilt.

Looking at the opening post, that SV pointer is strange. At the point this message is produced the SV pointer has been dereferenced many times, but 0xd is almost a NULL pointer and is mis-aligned too.

It might be worth running under a debugger and putting a breakpoint on the line (7280 in sv.c for 5.38.2) to check the value of sv at that point, in case there's some other problem with the formatted output code (which is unlikely).

You could also build with DEBUG_LEAKING_SCALARS_ABORT to produce a core dump immediately after the "Attempt to free unreferenced scalar" message to debug that sv value from a core dump.

@reshmavk
Copy link
Author

reshmavk commented May 8, 2024

That's right, our XS module was not being built with the debug flags. After rebuilding XS module with debug flags, Segmentation fault error is resolved.
We also rebuilt the XS module with -D_LARGE_FILES. This resolved the initial error. We are not getting the following error and our perl script is working as expected.
Attempt to free unreferenced scalar: SV 0xd, Perl interpreter: 0x200073a8 at XX.pm line 1224.
panic: unknown regstclass 0 at XX.pm line 1224.
FYI, perl is also built with -D_LARGE_FILES and it is 32bit.

@tonycoz
Copy link
Contributor

tonycoz commented May 12, 2024

Do I understand correctly that you've resolved this problem?

@reshmavk
Copy link
Author

Yes, the issue is resolved.
Our XS module was being compiled without the -D_LARGE_FILES option for a long time and everything was working fine. However, it looks like after the commit 75acd14 , our scripts started failing with the "Attempt to free unreferenced scalar" error. Now, after compiling XS module with -D_LARGE_FILES , the issue is resolved. So, there seems to be some correlation between the commit and the -D_LARGE_FILES option. Do you have any thoughts on this? It would be helpful for us if we understand more about this.

@tonycoz
Copy link
Contributor

tonycoz commented May 14, 2024

I wouldn't expect that to make a difference if you're building your XS module using the normal process.

You should be building your XS modules with the flags from ccflags in the configuration, which the typical ExtUtils::MakeMaker or Module::Build build process will add for you. You can see those flags with:

perl -V:ccflags

If you aren't using the normal build process and want to include the flags in your build process you can fetch them with:

perl -MConfig -e 'print $Config{ccflags}'

You didn't provide the perl -V output so I can't tell if those flags include -D_LARGE_FILES in your perl build, it does look like the AIX hints try to fetch the needed flags with getconf XBS5_LP64_OFF64_CFLAGS for 64-bit builds or getconf XBS5_ILP32_OFFBIG_CFLAGS for 32-bit builds

This adds -q64 or -q32 -D_LARGE_FILES -qlonglong respectively, so I suspect you aren't properly using ccflags.

@reshmavk
Copy link
Author

Our perl is built with -D_LARGE_FILES option. However, XS modules are not built using the normal build process. So it doesn't use any of the perl ccflags by default. We will consider the options suggested.
Thanks again for all the inputs and suggestions provided.

@tonycoz tonycoz closed this as completed May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants