Debian Codegraph @

ext/XS-APItest/t/extend.t

raw
1 #!perl
2 #
3 # Test stack expansion macros: EXTEND() etc, especially for edge cases
4 # where the count wraps to a native value or gets truncated.
5 #
6 # Some of these tests aren't really testing; they are however exercising
7 # edge cases, which other tools like ASAN may then detect problems with.
8 # In particular, test_EXTEND() does *(p+n) = NULL and *PL_stack_max = NULL
9 # before returning, to help such tools spot errors.
10 #
11 # Also, it doesn't test large but legal grow requests; only ridiculously
12 # large requests that are guaranteed to wrap.
13
14 use Test::More;
15 use Config;
16 use XS::APItest qw(test_EXTEND hwm_checks_enabled bad_EXTEND);
17
18 plan tests => 50;
19
20 my $uvsize = $Config::Config{uvsize}; # sizeof(UV)
21 my $sizesize = $Config::Config{sizesize}; # sizeof(Size_t)
22
23 # The first arg to test_EXTEND() is the SP to use in EXTEND(), treated
24 # as an offset from PL_stack_max. So extend(-1, 1, $use_ss) shouldn't
25 # call Perl_stack_grow(), while extend(-1, 2, $use_ss) should.
26 # Exercise offsets near to PL_stack_max to detect edge cases.
27 # Note that having the SP pointer beyond PL_stack_max is legal.
28
29 for my $offset (-1, 0, 1) {
30
31 # treat N as either an IV or a SSize_t
32 for my $use_ss (0, 1) {
33
34 # test with N in range -1 .. 3; only the -1 should panic
35
36 eval { test_EXTEND($offset, -1, $use_ss) };
37 like $@, qr/panic: .*negative count/, "test_EXTEND($offset, -1, $use_ss)";
38
39 for my $n (0,1,2,3) {
40 eval { test_EXTEND($offset, $n, $use_ss) };
41 is $@, "", "test_EXTEND($offset, $n, $use_ss)";
42 }
43
44 # some things can wrap if the int size is greater than the ptr size
45
46 SKIP: {
47 skip "Not small ptrs", 3 if $use_ss || $uvsize <= $sizesize;
48
49 # 0xffff... wraps to -1
50 eval { test_EXTEND($offset, (1 << 8*$sizesize)-1, $use_ss) };
51 like $@, qr/panic: .*negative count/,
52 "test_EXTEND(-1, SIZE_MAX, $use_ss)";
53
54 # 0x10000... truncates to zero;
55 # but the wrap-detection code converts it to -1 to force a panic
56 eval { test_EXTEND($offset, 1 << 8*$sizesize, $use_ss) };
57 like $@, qr/panic: .*negative count/,
58 "test_EXTEND(-1, SIZE_MAX+1, $use_ss)";
59
60 # 0x1ffff... truncates and then wraps to -1
61 eval { test_EXTEND($offset, (1 << (8*$sizesize+1))-1, $use_ss) };
62 like $@, qr/panic: .*negative count/,
63 "test_EXTEND(-1, 2*SIZE_MAX-1, $use_ss)";
64
65
66 }
67 }
68 }
69
70 SKIP:
71 {
72 # we've extended the stack a fair bit above so the actual bad_EXTEND*() should
73 # be safe in terms of UB *here*
74 skip "HWM checks not enabled", 2
75 unless hwm_checks_enabled();
76
77 ok(!eval { bad_EXTEND(); 1 }, "bad_EXTEND() should throw");
78 like($@, qr/^panic: XSUB XS::APItest::bad_EXTEND \(APItest\.c\) failed to extend arg stack/,
79 "check panic message");
80 }