summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/perl/dist/Storable/t/recurse.t
blob: 6f821691ee5b5beaab92041c1d8aebbbe42736cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!./perl
#
#  Copyright (c) 1995-2000, Raphael Manfredi
#  
#  You may redistribute only under the same terms as Perl 5, as specified
#  in the README file that comes with the distribution.
#  
use Config;

sub BEGIN {
    unshift @INC, 't';
    unshift @INC, 't/compat' if $] < 5.006002;
    if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
        print "1..0 # Skip: Storable was not built\n";
        exit 0;
    }
}

use Storable qw(freeze thaw dclone);

$Storable::flags = Storable::FLAGS_COMPAT;

use Test::More tests => 39;

package OBJ_REAL;

use Storable qw(freeze thaw);

@x = ('a', 1);

sub make { bless [], shift }

sub STORABLE_freeze {
    my $self = shift;
    my $cloning = shift;
    die "STORABLE_freeze" unless Storable::is_storing;
    return (freeze(\@x), $self);
}

sub STORABLE_thaw {
    my $self = shift;
    my $cloning = shift;
    my ($x, $obj) = @_;
    die "STORABLE_thaw #1" unless $obj eq $self;
    my $len = length $x;
    my $a = thaw $x;
    die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
    die "STORABLE_thaw #3" unless @$a == 2 && $a->[0] eq 'a' && $a->[1] == 1;
    @$self = @$a;
    die "STORABLE_thaw #4" unless Storable::is_retrieving;
}

package OBJ_SYNC;

@x = ('a', 1);

sub make { bless {}, shift }

sub STORABLE_freeze {
    my $self = shift;
    my ($cloning) = @_;
    return if $cloning;
    return ("", \@x, $self);
}

sub STORABLE_thaw {
    my $self = shift;
    my ($cloning, $undef, $a, $obj) = @_;
    die "STORABLE_thaw #1" unless $obj eq $self;
    die "STORABLE_thaw #2" unless ref $a eq 'ARRAY' || @$a != 2;
    $self->{ok} = $self;
}

package OBJ_SYNC2;

use Storable qw(dclone);

sub make {
    my $self = bless {}, shift;
    my ($ext) = @_;
    $self->{sync} = OBJ_SYNC->make;
    $self->{ext} = $ext;
    return $self;
}

sub STORABLE_freeze {
    my $self = shift;
    my %copy = %$self;
    my $r = \%copy;
    my $t = dclone($r->{sync});
    return ("", [$t, $self->{ext}], $r, $self, $r->{ext});
}

sub STORABLE_thaw {
    my $self = shift;
    my ($cloning, $undef, $a, $r, $obj, $ext) = @_;
    die "STORABLE_thaw #1" unless $obj eq $self;
    die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
    die "STORABLE_thaw #3" unless ref $r eq 'HASH';
    die "STORABLE_thaw #4" unless $a->[1] == $r->{ext};
    $self->{ok} = $self;
    ($self->{sync}, $self->{ext}) = @$a;
}

package OBJ_REAL2;

use Storable qw(freeze thaw);

$MAX = 20;
$recursed = 0;
$hook_called = 0;

sub make { bless [], shift }

sub STORABLE_freeze {
    my $self = shift;
    $hook_called++;
    return (freeze($self), $self) if ++$recursed < $MAX;
    return ("no", $self);
}

sub STORABLE_thaw {
    my $self = shift;
    my $cloning = shift;
    my ($x, $obj) = @_;
    die "STORABLE_thaw #1" unless $obj eq $self;
    $self->[0] = thaw($x) if $x ne "no";
    $recursed--;
}

package main;

my $real = OBJ_REAL->make;
my $x = freeze $real;
isnt($x, undef);

my $y = thaw $x;
is(ref $y, 'OBJ_REAL');
is($y->[0], 'a');
is($y->[1], 1);

my $sync = OBJ_SYNC->make;
$x = freeze $sync;
isnt($x, undef);

$y = thaw $x;
is(ref $y, 'OBJ_SYNC');
is($y->{ok}, $y);

my $ext = [1, 2];
$sync = OBJ_SYNC2->make($ext);
$x = freeze [$sync, $ext];
isnt($x, undef);

my $z = thaw $x;
$y = $z->[0];
is(ref $y, 'OBJ_SYNC2');
is($y->{ok}, $y);
is(ref $y->{sync}, 'OBJ_SYNC');
is($y->{ext}, $z->[1]);

$real = OBJ_REAL2->make;
$x = freeze $real;
isnt($x, undef);
is($OBJ_REAL2::recursed, $OBJ_REAL2::MAX);
is($OBJ_REAL2::hook_called, $OBJ_REAL2::MAX);

$y = thaw $x;
is(ref $y, 'OBJ_REAL2');
is($OBJ_REAL2::recursed, 0);

$x = dclone $real;
isnt($x, undef);
is(ref $x, 'OBJ_REAL2');
is($OBJ_REAL2::recursed, 0);
is($OBJ_REAL2::hook_called, 2 * $OBJ_REAL2::MAX);

is(Storable::is_storing, '');
is(Storable::is_retrieving, '');

#
# The following was a test-case that Salvador Ortiz Garcia <sog@msg.com.mx>
# sent me, along with a proposed fix.
#

package Foo;

sub new {
    my $class = shift;
    my $dat = shift;
    return bless {dat => $dat}, $class;
}

package Bar;
sub new {
    my $class = shift;
    return bless {
        a => 'dummy',
        b => [ 
            Foo->new(1),
            Foo->new(2), # Second instance of a Foo 
          ]
    }, $class;
}

sub STORABLE_freeze {
    my($self,$clonning) = @_;
    return "$self->{a}", $self->{b};
}

sub STORABLE_thaw {
    my($self,$clonning,$dummy,$o) = @_;
    $self->{a} = $dummy;
    $self->{b} = $o;
}

package main;

my $bar = new Bar;
my $bar2 = thaw freeze $bar;

is(ref($bar2), 'Bar');
is(ref($bar->{b}[0]), 'Foo');
is(ref($bar->{b}[1]), 'Foo');
is(ref($bar2->{b}[0]), 'Foo');
is(ref($bar2->{b}[1]), 'Foo');

#
# The following attempts to make sure blessed objects are blessed ASAP
# at retrieve time.
#

package CLASS_1;

sub make {
    my $self = bless {}, shift;
    return $self;
}

package CLASS_2;

sub make {
    my $self = bless {}, shift;
    my ($o) = @_;
    $self->{c1} = CLASS_1->make();
    $self->{o} = $o;
    $self->{c3} = bless CLASS_1->make(), "CLASS_3";
    $o->set_c2($self);
    return $self;
}

sub STORABLE_freeze {
    my($self, $clonning) = @_;
    return "", $self->{c1}, $self->{c3}, $self->{o};
}

sub STORABLE_thaw {
    my($self, $clonning, $frozen, $c1, $c3, $o) = @_;
    main::is(ref $self, "CLASS_2");
    main::is(ref $c1, "CLASS_1");
    main::is(ref $c3, "CLASS_3");
    main::is(ref $o, "CLASS_OTHER");
    $self->{c1} = $c1;
    $self->{c3} = $c3;
}

package CLASS_OTHER;

sub make {
    my $self = bless {}, shift;
    return $self;
}

sub set_c2 { $_[0]->{c2} = $_[1] }

#
# Is the reference count of the extra references returned from a
# STORABLE_freeze hook correct? [ID 20020601.005 (#9436)]
#
package Foo2;

sub new {
    my $self = bless {}, $_[0];
    $self->{freezed} = "$self";
    return $self;
}

sub DESTROY {
    my $self = shift;
    $::refcount_ok = 1 unless "$self" eq $self->{freezed};
}

package Foo3;

sub new {
    bless {}, $_[0];
}

sub STORABLE_freeze {
    my $obj = shift;
    return ("", $obj, Foo2->new);
}

sub STORABLE_thaw { } # Not really used

package main;

my $o = CLASS_OTHER->make();
my $c2 = CLASS_2->make($o);
my $so = thaw freeze $o;

our $refcount_ok = 0;
thaw freeze(Foo3->new);
is($refcount_ok, 1, "check refcount");

# Check stack overflows [cpan #97526]
# JSON::XS limits this to 512.
# Small 64bit systems fail with 1200 (c++ debugging), with gcc 3000.
# Optimized 64bit allows up to 33.000 recursion depth.
# with asan the limit is 255 though.

local $Storable::recursion_limit = 30;
local $Storable::recursion_limit_hash = 20;
sub MAX_DEPTH () { Storable::stack_depth() }
sub MAX_DEPTH_HASH () { Storable::stack_depth_hash() }
{
    my $t;
    print "# max depth ", MAX_DEPTH, "\n";
    $t = [$t] for 1 .. MAX_DEPTH;
    dclone $t;
    pass "can nest ".MAX_DEPTH." array refs";
}
{
    my $t;
    $t = {1=>$t} for 1 .. MAX_DEPTH_HASH-10;
    dclone $t;
    pass "can nest ".(MAX_DEPTH_HASH)." hash refs";
}
{
    my (@t);
    push @t, [{}] for 1..5000;
    #diag 'trying simple array[5000] stack overflow, no recursion';
    dclone \@t;
    is $@, '', 'No simple array[5000] stack overflow #257';
}

eval {
    my $t;
    $t = [$t] for 1 .. MAX_DEPTH*2;
    eval { note('trying catching recursive aref stack overflow') };
    dclone $t;
};
like $@, qr/Max\. recursion depth with nested structures exceeded/,
      'Caught aref stack overflow '.MAX_DEPTH*2;

if ($ENV{APPVEYOR} and length(pack "p", "") >= 8) {
    # TODO: need to repro this fail on a small machine.
    ok(1, "skip dclone of big hash");
}
else {
    eval {
        my $t;
        # 35.000 will cause appveyor 64bit windows to fail earlier
        $t = {1=>$t} for 1 .. MAX_DEPTH * 2;
        eval { note('trying catching recursive href stack overflow') };
        dclone $t;
    };
    like $@, qr/Max\. recursion depth with nested structures exceeded/,
      'Caught href stack overflow '.MAX_DEPTH_HASH*2;
}

{
    # perl #133326
    my @tt;
    #$Storable::DEBUGME=1;
    for (1..16000) {
        my $t = [[[]]];
        push @tt, $t;
    }
    ok(eval { dclone \@tt; 1 },
       "low depth structure shouldn't be treated as nested");
}