This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make the Storable stacksize messages less scary
[perl5.git] / dist / Storable / stacksize
CommitLineData
dd7f75e0
RU
1#!/usr/bin/perl
2# binary search maximum stack depth for arrays and hashes
c0e3b4b5 3# and store it in lib/Storable/Limit.pm
dd7f75e0 4
e58927f4
RU
5use Config;
6use Cwd;
c0e3b4b5 7use strict;
e58927f4 8
c0e3b4b5
TC
9my $fn = "lib/Storable/Limit.pm";
10my $ptrsize = $Config{ptrsize};
11my ($bad1, $bad2) = (65001, 25000);
dd7f75e0 12sub QUIET () {
e58927f4
RU
13 (defined $ENV{MAKEFLAGS} and $ENV{MAKEFLAGS} =~ /\b(s|silent|quiet)\b/
14 and !defined($ENV{TRAVIS}))
dd7f75e0
RU
15 ? 1 : 0
16}
17sub PARALLEL () {
18 if (defined $ENV{MAKEFLAGS}
19 and $ENV{MAKEFLAGS} =~ /\bj\s*(\d+)\b/
20 and $1 > 1) {
21 return 1;
22 } else {
23 return 0;
24 }
25}
26sub is_miniperl {
27 return !defined &DynaLoader::boot_DynaLoader;
28}
29
30if (is_miniperl()) {
c0e3b4b5
TC
31 die "Should not run during miniperl\n";
32}
7ec112d2 33my $prefix = "";
c0e3b4b5
TC
34if ($^O eq "MSWin32") {
35 # prevent Windows popping up a dialog each time we overflow
36 # the stack
37 require Win32API::File;
38 Win32API::File->import(qw(SetErrorMode SEM_NOGPFAULTERRORBOX SEM_FAILCRITICALERRORS));
39 SetErrorMode(SEM_NOGPFAULTERRORBOX() | SEM_FAILCRITICALERRORS());
dd7f75e0 40}
7ec112d2
TC
41# the ; here is to ensure system() passes this to the shell
42elsif (system("ulimit -c 0 ;") == 0) {
43 # try to prevent core dumps
44 $prefix = "ulimit -c 0 ; ";
45}
e58927f4
RU
46if (@ARGV and $ARGV[0] eq '--core') {
47 $ENV{PERL_CORE} = 1;
48}
49my $PERL = $^X;
dd7f75e0 50if ($ENV{PERL_CORE}) {
e58927f4
RU
51 my $path;
52 my $ldlib = $Config{ldlibpthname};
dd7f75e0
RU
53 if (-d 'dist/Storable') {
54 chdir 'dist/Storable';
e58927f4 55 $PERL = "../../$PERL" unless $PERL =~ m|^/|;
dd7f75e0 56 }
a65c1822
RU
57 if ($ldlib) {
58 $path = getcwd()."/../..";
59 }
dd7f75e0 60 if ($^O eq 'MSWin32' and -d '../dist/Storable') {
e58927f4
RU
61 chdir '..\dist\Storable';
62 $PERL = "..\\..\\$PERL" unless $PERL =~ /^[A-Za-z]:\\/;
63 }
64 $PERL = "\"$PERL\"" if $PERL =~ / /;
91ea6c3e 65 if ($ldlib and $ldlib ne 'PATH') {
e58927f4 66 $PERL = "$ldlib=$path $PERL";
dd7f75e0
RU
67 }
68}
69
c0e3b4b5
TC
70-d "lib" or mkdir "lib";
71-d "lib/Storable" or mkdir "lib/Storable";
dd7f75e0
RU
72
73my ($n, $good, $bad, $found) =
c0e3b4b5 74 (65000, 100, $bad1, undef);
dd7f75e0 75print "probe for max. stack sizes...\n" unless QUIET;
c0e3b4b5
TC
76# -I. since we're run before pm_to_blib (which is going to copy the
77# file we create) and need to load our Storable.pm, not the already
78# installed Storable.pm
79my $mblib = '-Mblib -I.';
dd7f75e0
RU
80if ($ENV{PERL_CORE}) {
81 if ($^O eq 'MSWin32') {
82 $mblib = '-I..\..\lib\auto -I..\..\lib';
83 } else {
84 $mblib = '-I../../lib/auto -I../../lib';
85 }
86}
87if (PARALLEL) {
88 # problem with parallel builds. wait for INST_DYNAMIC linking to be done.
89 # the problem is the RM_F INST_DYNAMIC race.
90 print "parallel build race - wait for linker ...\n" unless QUIET;
91 sleep(2.0);
92}
dd7f75e0
RU
93
94sub cmd {
c0e3b4b5 95 my ($i, $try, $limit_name) = @_;
dd7f75e0 96 die unless $i;
c0e3b4b5
TC
97 my $code = "my \$t; \$Storable::$limit_name = -1; $try for 1..$i;dclone(\$t);";
98 my $q = ($^O eq 'MSWin32') ? '"' : "'";
99
7ec112d2 100 "$prefix $PERL $mblib -MStorable=dclone -e$q$code$q"
dd7f75e0
RU
101}
102# try more
103sub good {
104 my $i = shift; # this passed
105 my $j = $i + abs(int(($bad - $i) / 2));
f73ae54b 106 print "Storable: determining recursion limit: $i passed, try more $j ...\n" unless QUIET;
dd7f75e0
RU
107 $good = $i;
108 if ($j <= $i) {
109 $found++;
110 }
111 return $j;
112}
113# try less
114sub bad {
115 my $i = shift; # this failed
116 my $j = $i - abs(int(($i - $good) / 2));
f73ae54b 117 print "Storable: determining recursion limit: $i too big, try less $j ...\n" unless QUIET;
dd7f75e0
RU
118 $bad = $i;
119 if ($j >= $i) {
120 $j = $good;
121 $found++;
122 }
123 return $j;
124}
125
126while (!$found) {
c0e3b4b5 127 my $cmd = cmd($n, '$t=[$t]', 'recursion_limit');
dd7f75e0
RU
128 #print "$cmd\n" unless $QUIET;
129 if (system($cmd) == 0) {
130 $n = good($n);
131 } else {
132 $n = bad($n);
133 }
134}
135print "MAX_DEPTH = $n\n" unless QUIET;
136my $max_depth = $n;
137
138($n, $good, $bad, $found) =
139 (int($n/2), 50, $n, undef);
140# pack j only since 5.8
141my $max = ($] > 5.007 and length(pack "j", 0) < 8)
142 ? ($^O eq 'MSWin32' ? 3000 : 8000)
c0e3b4b5 143 : $max_depth;
dd7f75e0
RU
144$n = $max if $n > $max;
145$bad = $max if $bad > $max;
dd7f75e0 146while (!$found) {
c0e3b4b5 147 my $cmd = cmd($n, '$t={1=>$t}', 'recursion_limit_hash');
dd7f75e0
RU
148 #print "$cmd\n" unless $QUIET;
149 if (system($cmd) == 0) {
150 $n = good($n);
151 } else {
152 $n = bad($n);
153 }
154}
155if ($max_depth == $bad1-1
156 and $n == $bad2-1)
157{
2a4dadc5 158 # more likely the shell. travis docker ubuntu, mingw e.g.
dd7f75e0
RU
159 print "Error: Apparently your system(SHELLSTRING) cannot catch stack overflows\n"
160 unless QUIET;
161 $max_depth = 512;
162 $n = 256;
163 print "MAX_DEPTH = $max_depth\n" unless QUIET;
164}
165print "MAX_DEPTH_HASH = $n\n" unless QUIET;
166my $max_depth_hash = $n;
167
c0e3b4b5
TC
168# Previously this calculation was done in the macro, calculate it here
169# instead so a user setting of either variable more closely matches
170# the limits the use sees.
171
172# be fairly aggressive in trimming this, smoke testing showed several
173# several apparently random failures here, eg. working in one
174# configuration, but not in a very similar configuration.
175$max_depth = int(0.6 * $max_depth);
176$max_depth_hash = int(0.6 * $max_depth);
177
178my $stack_reserve = $^O eq "MSWin32" ? 32 : 16;
179if ($] ge "5.016" && !($^O eq "cygwin" && $ptrsize == 8)) {
180 $max_depth -= $stack_reserve;
181 $max_depth_hash -= $stack_reserve;
182}
183else {
184 # within the exception we need another stack depth to recursively
185 # cleanup the hash
186 $max_depth = ($max_depth >> 1) - $stack_reserve;
187 $max_depth_hash = ($max_depth_hash >> 1) - $stack_reserve * 2;
188}
189
190my $f;
12d9690f 191open $f, ">", $fn or die "$fn $!";
c0e3b4b5
TC
192print $f <<EOS;
193# bisected by stacksize
194\$Storable::recursion_limit = $max_depth
195 unless defined \$Storable::recursion_limit;
196\$Storable::recursion_limit_hash = $max_depth_hash
197 unless defined \$Storable::recursion_limit_hash;
1981;
199EOS
dd7f75e0 200close $f;