-
Notifications
You must be signed in to change notification settings - Fork 89
/
Cover.xs
1611 lines (1353 loc) · 41.7 KB
/
Cover.xs
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2001-2024, Paul Johnson ([email protected])
*
* This software is free. It is licensed under the same terms as Perl itself.
*
* The latest version of this software should be available from my homepage:
* http://www.pjcj.net
*
*/
#ifdef __cplusplus
extern "C" {
#endif
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif
#ifdef PERL_OBJECT
#define CALLOP this->*PL_op
#else
#define CALLOP *PL_op
#endif
#ifndef START_MY_CXT
/* No threads in 5.6 */
#define START_MY_CXT static my_cxt_t my_cxt;
#define dMY_CXT_SV dNOOP
#define dMY_CXT dNOOP
#define MY_CXT_INIT NOOP
#define MY_CXT my_cxt
#define pMY_CXT void
#define pMY_CXT_
#define _pMY_CXT
#define aMY_CXT
#define aMY_CXT_
#define _aMY_CXT
#endif
#define MY_CXT_KEY "Devel::Cover::_guts" XS_VERSION
#define PDEB(a) a
#define NDEB(a) ;
#define D PerlIO_printf
#define L Perl_debug_log
#define svdump(sv) do_sv_dump(0, L, (SV *)sv, 0, 10, 1, 0);
#define None 0x00000000
#define Statement 0x00000001
#define Branch 0x00000002
#define Condition 0x00000004
#define Subroutine 0x00000008
#define Path 0x00000010
#define Pod 0x00000020
#define Time 0x00000040
#define All 0xffffffff
#if defined HAS_GETTIMEOFDAY || defined HAS_TIMES
# define CAN_PROFILE 1
#else
# define CAN_PROFILE 0
#endif
struct unique { /* Well, we'll be fairly unlucky if it's not */
OP *addr,
op;
/* include hashed file location information, where available (cops) */
size_t fileinfohash;
};
#define KEY_SZ sizeof(struct unique)
typedef struct {
unsigned covering;
int collecting_here;
HV *cover,
*statements,
*branches,
*conditions,
#if CAN_PROFILE
*times,
#endif
*modules,
*files;
AV *ends;
char profiling_key[KEY_SZ];
bool profiling_key_valid;
SV *module,
*lastfile;
int tid;
int replace_ops;
/* - fix up whatever is broken with module_relative on Windows here */
#if PERL_VERSION > 8
Perl_ppaddr_t ppaddr[MAXO];
#else
OP *(*ppaddr[MAXO])(pTHX);
#endif
} my_cxt_t;
#ifdef USE_ITHREADS
static perl_mutex DC_mutex;
#endif
static HV *Pending_conditionals,
*Return_ops;
static int tid;
START_MY_CXT
#define collecting(criterion) (MY_CXT.covering & (criterion))
#ifdef HAS_GETTIMEOFDAY
#ifdef __cplusplus
extern "C" {
#endif
#ifdef WIN32
#include <time.h>
#else
#include <sys/time.h>
#endif
#ifdef __cplusplus
}
#endif
/* op->op_sibling is deprecated on new perls, but the OpSIBLING macro doesn't
exist on older perls. We don't need to check for PERL_OP_PARENT here
because if PERL_OP_PARENT was set, and we needed to check op_moresib,
we would already have this macro. */
#ifndef OpSIBLING
#define OpSIBLING(o) (0 + (o)->op_sibling)
#endif
static double get_elapsed() {
#ifdef WIN32
dTHX;
#endif
struct timeval time;
double e;
gettimeofday(&time, NULL);
e = time.tv_sec * 1e6 + time.tv_usec;
return e;
}
static double elapsed() {
static double p;
double e, t;
t = get_elapsed();
e = t - p;
p = t;
return e;
}
#elif defined HAS_TIMES
#ifndef HZ
# ifdef CLK_TCK
# define HZ CLK_TCK
# else
# define HZ 60
# endif
#endif
static int cpu() {
#ifdef WIN32
dTHX;
#endif
static struct tms time;
static int utime = 0,
stime = 0;
int e;
#ifndef VMS
(void)PerlProc_times(&time);
#else
(void)PerlProc_times((tbuffer_t *)&time);
#endif
e = time.tms_utime - utime + time.tms_stime - stime;
utime = time.tms_utime;
stime = time.tms_stime;
return e / HZ;
}
#endif /* HAS_GETTIMEOFDAY */
/*
* http://codereview.stackexchange.com/questions/85556/simple-string-hashing-algorithm-implementation
* https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
* http://www.isthe.com/chongo/tech/comp/fnv/index.html#public_domain
*
* FNV hash algorithms and source code have been released into the
* public domain. The authors of the FNV algorithm took deliberate
* steps to disclose the algorithm in a public forum soon after it was
* invented. More than a year passed after this public disclosure and the
* authors deliberately took no steps to patent the FNV algorithm. Therefore
* it is safe to say that the FNV authors have no patent claims on the FNV
* algorithm as published.
*
*/
/* Fowler/Noll/Vo (FNV) hash function, variant 1a */
static size_t fnv1a_hash(const char* cp)
{
size_t hash = 0x811c9dc5;
while (*cp) {
hash ^= (unsigned char) *cp++;
hash *= 0x01000193;
}
return hash;
}
#define FILEINFOSZ 1024
static char *get_key(OP *o) {
static struct unique uniq;
static char mybuf[FILEINFOSZ];
uniq.addr = o;
uniq.op = *o;
uniq.op.op_ppaddr = 0; /* we mess with this field */
uniq.op.op_targ = 0; /* might change */
if (o->op_type == OP_NEXTSTATE || o->op_type == OP_DBSTATE) {
/* cop, has file location information */
char *file = CopFILE((COP *)o);
long line = CopLINE((COP *)o);
snprintf(mybuf, FILEINFOSZ - 1, "%s:%ld", file, line);
uniq.fileinfohash = fnv1a_hash(mybuf);
} else {
/* no file location information available */
uniq.fileinfohash = 0;
}
return (char *)&uniq;
}
static char *hex_key(char *key) {
static char hk[KEY_SZ * 2 + 1];
size_t c;
for (c = 0; c < KEY_SZ; c++) {
NDEB(D(L, "%zu of %zu, <%02X> at %p\n",
c, KEY_SZ, (unsigned char)key[c], hk + c * 2));
sprintf(hk + c * 2, "%02X", (unsigned char)key[c]);
}
hk[c * 2] = 0;
return hk;
}
static void set_firsts_if_needed(pTHX) {
SV *init = (SV *)get_cv("Devel::Cover::first_init", 0);
SV *end = (SV *)get_cv("Devel::Cover::first_end", 0);
NDEB(svdump(end));
if (PL_initav && av_len(PL_initav) >= 0)
{
SV **cv = av_fetch(PL_initav, 0, 0);
if (*cv != init) {
av_unshift(PL_initav, 1);
av_store(PL_initav, 0, init);
}
}
if (PL_endav && av_len(PL_endav) >= 0) {
SV **cv = av_fetch(PL_endav, 0, 0);
if (*cv != end) {
av_unshift(PL_endav, 1);
av_store(PL_endav, 0, end);
}
}
}
static int check_if_collecting(pTHX_ COP *cop) {
dMY_CXT;
#if !NO_TAINT_SUPPORT
int tainted = PL_tainted;
#endif
char *file = CopFILE(cop);
int in_re_eval = strnEQ(file, "(reeval ", 8);
NDEB(D(L, "check_if_collecting at: %s:%ld\n", file, (long)CopLINE(cop)));
if (file && strNE(SvPV_nolen(MY_CXT.lastfile), file)) {
int found = 0;
if (MY_CXT.files) {
SV **f = hv_fetch(MY_CXT.files, file, strlen(file), 0);
if (f) {
MY_CXT.collecting_here = SvIV(*f);
found = 1;
NDEB(D(L, "File: %s:%ld [%d]\n",
file, (long)CopLINE(cop), MY_CXT.collecting_here));
}
}
if (!found && MY_CXT.replace_ops && !in_re_eval) {
dSP;
int count;
SV *rv;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file, 0)));
PUTBACK;
count = call_pv("Devel::Cover::use_file", G_SCALAR);
SPAGAIN;
if (count != 1)
croak("use_file returned %d values\n", count);
rv = POPs;
MY_CXT.collecting_here = SvTRUE(rv) ? 1 : 0;
NDEB(D(L, "-- %s - %d\n", file, MY_CXT.collecting_here));
PUTBACK;
FREETMPS;
LEAVE;
}
sv_setpv(MY_CXT.lastfile, file);
}
NDEB(D(L, "%s - %d\n",
SvPV_nolen(MY_CXT.lastfile), MY_CXT.collecting_here));
#if PERL_VERSION > 6
if (SvTRUE(MY_CXT.module)) {
STRLEN mlen,
flen = strlen(file);
char *m = SvPV(MY_CXT.module, mlen);
if (flen >= mlen && strnEQ(m, file + flen - mlen, mlen)) {
SV **dir = hv_fetch(MY_CXT.modules, file, strlen(file), 1);
if (!SvROK(*dir)) {
SV *cwd = newSV(0);
AV *d = newAV();
*dir = newRV_inc((SV*) d);
av_push(d, newSVsv(MY_CXT.module));
if (getcwd_sv(cwd)) {
av_push(d, newSVsv(cwd));
NDEB(D(L, "require %s as %s from %s\n",
m, file, SvPV_nolen(cwd)));
}
}
}
sv_setpv(MY_CXT.module, "");
set_firsts_if_needed(aTHX);
}
#endif
#if !NO_TAINT_SUPPORT
PL_tainted = tainted;
#endif
return MY_CXT.collecting_here;
}
#if CAN_PROFILE
static void cover_time(pTHX)
{
dMY_CXT;
SV **count;
NV c;
if (collecting(Time)) {
/*
* Profiling information is stored against MY_CXT.profiling_key,
* the key for the op we have just run
*/
NDEB(D(L, "Cop at %p, op at %p\n", PL_curcop, PL_op));
if (MY_CXT.profiling_key_valid) {
count = hv_fetch(MY_CXT.times, MY_CXT.profiling_key, KEY_SZ, 1);
c = (SvTRUE(*count) ? SvNV(*count) : 0) +
#if defined HAS_GETTIMEOFDAY
elapsed();
#else
cpu();
#endif
sv_setnv(*count, c);
}
if (PL_op) {
memcpy(MY_CXT.profiling_key, get_key(PL_op), KEY_SZ);
MY_CXT.profiling_key_valid = 1;
} else {
MY_CXT.profiling_key_valid = 0;
}
}
}
#endif
static int collecting_here(pTHX) {
dMY_CXT;
if (MY_CXT.collecting_here) return 1;
#if CAN_PROFILE
cover_time(aTHX);
MY_CXT.profiling_key_valid = 0;
#endif
NDEB(D(L, "op %p is %s\n", PL_op, OP_NAME(PL_op)));
if (hv_exists(Return_ops, get_key(PL_op), KEY_SZ))
return MY_CXT.collecting_here = 1;
else
return 0;
}
static void store_return(pTHX) {
dMY_CXT;
/*
* If we are jumping somewhere we might not be collecting
* coverage there, so store where we will be coming back to
* so we can turn on coverage straight away. We need to
* store more than one return op because a non collecting
* sub may call back to a collecting sub.
*/
if (MY_CXT.collecting_here && PL_op->op_next) {
(void)hv_fetch(Return_ops, get_key(PL_op->op_next), KEY_SZ, 1);
NDEB(D(L, "adding return op %p\n", PL_op->op_next));
}
}
static void store_module(pTHX) {
dMY_CXT;
dSP;
#if PERL_VERSION > 8
SvSetSV_nosteal(MY_CXT.module, (SV*)newSVpv(SvPV_nolen(TOPs), 0));
NDEB(D(L, "require %s\n", SvPV_nolen(MY_CXT.module)));
#endif
}
static void call_report(pTHX) {
dSP;
PUSHMARK(SP);
call_pv("Devel::Cover::report", G_VOID|G_DISCARD|G_EVAL);
SPAGAIN;
}
static void cover_statement(pTHX_ OP *op) {
dMY_CXT;
char *ch;
SV **count;
IV c;
if (!collecting(Statement)) return;
ch = get_key(op);
count = hv_fetch(MY_CXT.statements, ch, KEY_SZ, 1);
c = SvTRUE(*count) ? SvIV(*count) + 1 : 1;
NDEB(D(L, "Statement: %s:%ld\n", CopFILE(cCOPx(op)), (long)CopLINE(cCOPx(op))));
sv_setiv(*count, c);
NDEB(op_dump(op));
}
static void cover_current_statement(pTHX) {
#if CAN_PROFILE
cover_time(aTHX);
#endif
cover_statement(aTHX_ PL_op);
}
static void add_branch(pTHX_ OP *op, int br) {
dMY_CXT;
AV *branches;
SV **count;
int c;
SV **tmp = hv_fetch(MY_CXT.branches, get_key(op), KEY_SZ, 1);
if (SvROK(*tmp)) {
branches = (AV *) SvRV(*tmp);
} else {
*tmp = newRV_inc((SV*) (branches = newAV()));
av_unshift(branches, 2);
}
count = av_fetch(branches, br, 1);
c = SvTRUE(*count) ? SvIV(*count) + 1 : 1;
sv_setiv(*count, c);
NDEB(D(L, "Adding branch making %d at %p\n", c, op));
}
static AV *get_conditional_array(pTHX_ OP *op) {
dMY_CXT;
AV *conds;
SV **cref = hv_fetch(MY_CXT.conditions, get_key(op), KEY_SZ, 1);
if (SvROK(*cref))
conds = (AV *) SvRV(*cref);
else
*cref = newRV_inc((SV*) (conds = newAV()));
return conds;
}
static void set_conditional(pTHX_ OP *op, int cond, int value) {
/*
* The conditional array is composed of six elements:
*
* 0 - 1 iff we are in an xor and the first operand was true
* 1 - not short circuited - second operand is false
* 2 - not short circuited - second operand is true
* 3 - short circuited, or for xor second operand is false
* 4 - for xor second operand is true
* 5 - 1 iff we are in void context
*/
SV **count = av_fetch(get_conditional_array(aTHX_ op), cond, 1);
sv_setiv(*count, value);
NDEB(D(L, "Setting %d conditional to %d at %p\n", cond, value, op));
}
static void add_conditional(pTHX_ OP *op, int cond) {
SV **count = av_fetch(get_conditional_array(aTHX_ op), cond, 1);
int c = SvTRUE(*count) ? SvIV(*count) + 1 : 1;
sv_setiv(*count, c);
NDEB(D(L, "Adding %d conditional making %d at %p\n", cond, c, op));
}
#ifdef USE_ITHREADS
static AV *get_conds(pTHX_ AV *conds) {
dMY_CXT;
AV *thrconds;
HV *threads;
SV *tid,
**cref;
char *t;
if (av_exists(conds, 2)) {
SV **cref = av_fetch(conds, 2, 0);
threads = (HV *) *cref;
} else {
threads = newHV();
HvSHAREKEYS_off(threads);
av_store(conds, 2, (SV *)threads);
}
tid = newSViv(MY_CXT.tid);
t = SvPV_nolen(tid);
cref = hv_fetch(threads, t, strlen(t), 1);
if (SvROK(*cref))
thrconds = (AV *)SvRV(*cref);
else
*cref = newRV_inc((SV*) (thrconds = newAV()));
return thrconds;
}
#endif
static void add_condition(pTHX_ SV *cond_ref, int value) {
int final = !value;
AV *conds = (AV *) SvRV(cond_ref);
OP *next = INT2PTR(OP *, SvIV(*av_fetch(conds, 0, 0)));
OP *(*addr)(pTHX) = INT2PTR(OP *(*)(pTHX), SvIV(*av_fetch(conds, 1, 0)));
I32 i;
if (!final && next != PL_op)
croak("next (%p) does not match PL_op (%p)", next, PL_op);
#ifdef USE_ITHREADS
i = 0;
conds = get_conds(aTHX_ conds);
#else
i = 2;
#endif
NDEB(D(L, "Looking through %zd conditionals at %p\n",
av_len(conds) - 1, PL_op));
for (; i <= av_len(conds); i++) {
OP *op = INT2PTR(OP *, SvIV(*av_fetch(conds, i, 0)));
SV **count = av_fetch(get_conditional_array(aTHX_ op), 0, 1);
int type = SvTRUE(*count) ? SvIV(*count) : 0;
sv_setiv(*count, 0);
/* Check if we have come from an xor with a true first op */
if (final) value = 1;
if (type == 1) value += 2;
NDEB(D(L, "Found %p: %d, %d\n", op, type, value));
add_conditional(aTHX_ op, value);
}
#ifdef USE_ITHREADS
i = -1;
#else
i = 1;
#endif
while (av_len(conds) > i) av_pop(conds);
NDEB(svdump(conds));
NDEB(D(L, "addr is %p, next is %p, PL_op is %p, length is %zd final is %d\n",
addr, next, PL_op, av_len(conds), final));
if (!final) next->op_ppaddr = addr;
}
static void dump_conditions(pTHX) {
HE *e;
MUTEX_LOCK(&DC_mutex);
hv_iterinit(Pending_conditionals);
PDEB(D(L, "Pending_conditionals:\n"));
while ((e = hv_iternext(Pending_conditionals))) {
I32 len;
char *key = hv_iterkey(e, &len);
SV *cond_ref = hv_iterval(Pending_conditionals, e);
AV *conds = (AV *) SvRV(cond_ref);
OP *next = INT2PTR(OP *, SvIV(*av_fetch(conds, 0,0)));
OP *(*addr)(pTHX) = INT2PTR(OP *(*)(pTHX), SvIV(*av_fetch(conds, 1,0)));
I32 i;
#ifdef USE_ITHREADS
i = 0; /* TODO - this can't be right */
conds = get_conds(aTHX_ conds);
#else
i = 2;
#endif
PDEB(D(L, " %s: op %p, next %p (%ld)\n",
hex_key(key), next, addr, (long)av_len(conds) - 1));
for (; i <= av_len(conds); i++) {
OP *op = INT2PTR(OP *, SvIV(*av_fetch(conds, i, 0)));
SV **count = av_fetch(get_conditional_array(aTHX_ op), 0, 1);
int type = SvTRUE(*count) ? SvIV(*count) : 0;
sv_setiv(*count, 0);
PDEB(D(L, " %2d: %p, %d\n", i - 2, op, type));
}
}
MUTEX_UNLOCK(&DC_mutex);
}
#if PERL_VERSION > 18
/* For if ($a || $b) and unless ($a && $b), rpeep skips past a few
* logops and messes with Devel::Cover
*
* This function will find the skipped op if there is one
*/
static OP *find_skipped_conditional(pTHX_ OP *o) {
OP *right,
*next;
if (o->op_type != OP_OR && o->op_type != OP_AND)
return NULL;
/* Get to the end of the "a || b || c" block */
right = OpSIBLING(cLOGOP->op_first);
while (right && OpSIBLING(cLOGOPx(right)))
right = OpSIBLING(cLOGOPx(right));
if (!right)
return NULL;
next = right->op_next;
while (next && next->op_type == OP_NULL)
next = next->op_next;
if (!next)
return NULL;
if (o == next)
return NULL;
if (next->op_type != OP_OR && next->op_type != OP_AND)
return NULL;
/* if ($a || $b) or unless ($a && $b) */
if (o->op_type == next->op_type)
return NULL;
if ((next->op_flags & OPf_WANT) != OPf_WANT_VOID)
return NULL;
if (!cLOGOPx(next)->op_other || !o->op_next)
return NULL;
if (cLOGOPx(next)->op_other != o->op_next)
return NULL;
return next;
}
#endif
/* NOTE: caller must protect get_condition calls by locking DC_mutex */
static OP *get_condition(pTHX) {
SV **pc = hv_fetch(Pending_conditionals, get_key(PL_op), KEY_SZ, 0);
if (pc && SvROK(*pc)) {
dSP;
NDEB(D(L, "get_condition from %p, %p: %p (%s)\n",
PL_op, (void *)PL_op->op_targ, pc, hex_key(get_key(PL_op))));
/* dump_conditions(aTHX); */
NDEB(svdump(Pending_conditionals));
add_condition(aTHX_ *pc, SvTRUE(TOPs) ? 2 : 1);
} else {
PDEB(D(L, "All is lost, I know not where to go from %p, %p: %p (%s)\n",
PL_op, (void *)PL_op->op_targ, pc, hex_key(get_key(PL_op))));
dump_conditions(aTHX);
NDEB(svdump(Pending_conditionals));
/* croak("urgh"); */
exit(1);
}
return PL_op;
}
static void finalise_conditions(pTHX) {
/*
* Our algorithm for conditions relies on ending up at a particular
* op which we use to call get_condition(). It's possible that we
* never get to that op; for example we might return out of a sub.
* This causes us to lose coverage information.
*
* This function is called after the program has been run in order
* to collect that lost information.
*/
HE *e;
NDEB(D(L, "finalise_conditions\n"));
/* dump_conditions(aTHX); */
NDEB(svdump(Pending_conditionals));
MUTEX_LOCK(&DC_mutex);
hv_iterinit(Pending_conditionals);
while ((e = hv_iternext(Pending_conditionals)))
add_condition(aTHX_ hv_iterval(Pending_conditionals, e), 0);
MUTEX_UNLOCK(&DC_mutex);
}
static void cover_cond(pTHX)
{
dMY_CXT;
if (collecting(Branch)) {
dSP;
int val = SvTRUE(TOPs);
add_branch(aTHX_ PL_op, !val);
}
}
static void cover_logop(pTHX) {
/*
* For OP_AND, if the first operand is false, we have short
* circuited the second, otherwise the value of the op is the
* value of the second operand.
*
* For OP_OR, if the first operand is true, we have short circuited
* the second, otherwise the value of the op is the value of the
* second operand.
*
* We check the value of the first operand by simply looking on the
* stack. To check the second operand it is necessary to note the
* location of the next op after this logop. When we get there, we
* look at the stack and store the coverage information indexed to
* this op.
*
* This scheme also works for OP_XOR with a small modification
* because it doesn't short circuit. See the comment below.
*
* To find out when we get to the next op we change the op_ppaddr to
* point to get_condition(), which will do the necessary work and
* then reset and run the original op_ppaddr. We also store
* information in the Pending_conditionals hash. This is keyed on
* the op and the value is an array, the first element of which is
* the op we are messing with, the second element of which is the
* op_ppaddr we overwrote, and the subsequent elements are the ops
* about which we are collecting the condition coverage information.
* Note that an op may be collecting condition coverage information
* about a number of conditions.
*/
dMY_CXT;
NDEB(D(L, "logop() at %p\n", PL_op));
NDEB(op_dump(PL_op));
if (!collecting(Condition))
return;
if (cLOGOP->op_first->op_type == OP_ITER) {
/* loop - ignore it for now */
} else {
dSP;
int left_val = SvTRUE(TOPs);
#if PERL_VERSION > 8
int left_val_def = SvOK(TOPs);
#endif
/* We don't count X= as void context because we care about the value
* of the RHS */
int void_context = GIMME_V == G_VOID &&
#if PERL_VERSION > 8
PL_op->op_type != OP_DORASSIGN &&
#endif
PL_op->op_type != OP_ANDASSIGN &&
PL_op->op_type != OP_ORASSIGN;
NDEB(D(L, "left_val: %d, void_context: %d at %p\n",
left_val, void_context, PL_op));
NDEB(op_dump(PL_op));
set_conditional(aTHX_ PL_op, 5, void_context);
if ((PL_op->op_type == OP_AND && left_val) ||
(PL_op->op_type == OP_ANDASSIGN && left_val) ||
(PL_op->op_type == OP_OR && !left_val) ||
(PL_op->op_type == OP_ORASSIGN && !left_val) ||
#if PERL_VERSION > 8
(PL_op->op_type == OP_DOR && !left_val_def) ||
(PL_op->op_type == OP_DORASSIGN && !left_val_def) ||
#endif
(PL_op->op_type == OP_XOR)) {
/* no short circuit */
OP *right = OpSIBLING(cLOGOP->op_first);
NDEB(op_dump(right));
if (void_context ||
right->op_type == OP_NEXT ||
right->op_type == OP_LAST ||
right->op_type == OP_REDO ||
right->op_type == OP_GOTO ||
right->op_type == OP_RETURN ||
right->op_type == OP_DIE) {
/*
* If we are in void context, or the right side of the op is a
* branch, we don't care what its value is - it won't be
* returning one. We're just glad to be here, so we chalk up
* success.
*/
NDEB(D(L, "Add conditional 2\n"));
add_conditional(aTHX_ PL_op, 2);
} else {
char *ch;
AV *conds;
SV **cref,
*cond;
OP *next;
if (PL_op->op_type == OP_XOR && left_val) {
/*
* This is an xor. It does not short circuit. We
* have just executed the first op. When we get to
* next we will have already done the xor, so we can
* work out what the value of the second op was.
*
* We set a flag in the first element of the array
* to say that we had a true value from the first
* op.
*/
set_conditional(aTHX_ PL_op, 0, 1);
}
#if PERL_VERSION > 14
NDEB(D(L, "Getting next\n"));
next = (PL_op->op_type == OP_XOR)
? PL_op->op_next
: right->op_next;
while (next && next->op_type == OP_NULL)
next = next->op_next;
#else
next = PL_op->op_next;
#endif
if (!next) return; /* in fold_constants */
NDEB(op_dump(PL_op));
NDEB(op_dump(next));
ch = get_key(next);
MUTEX_LOCK(&DC_mutex);
cref = hv_fetch(Pending_conditionals, ch, KEY_SZ, 1);
if (SvROK(*cref))
conds = (AV *)SvRV(*cref);
else
*cref = newRV_inc((SV*) (conds = newAV()));
if (av_len(conds) < 0) {
av_push(conds, newSViv(PTR2IV(next)));
av_push(conds, newSViv(PTR2IV(next->op_ppaddr)));
}
#ifdef USE_ITHREADS
conds = get_conds(aTHX_ conds);
#endif
cond = newSViv(PTR2IV(PL_op));
av_push(conds, cond);
NDEB(D(L, "Adding conditional %p (%s) "
"making %zd at %p (%s), ppaddr: %p\n",
next, PL_op_name[next->op_targ], av_len(conds) - 1,
PL_op, hex_key(ch), next->op_ppaddr));
/* dump_conditions(aTHX); */
NDEB(svdump(Pending_conditionals));
NDEB(op_dump(PL_op));
NDEB(op_dump(next));
next->op_ppaddr = get_condition;
MUTEX_UNLOCK(&DC_mutex);
}
} else {
/* short circuit */
#if PERL_VERSION > 14
OP *up = OpSIBLING(cLOGOP->op_first)->op_next;
#if PERL_VERSION > 18
OP *skipped;
#endif
while (up && up->op_type == PL_op->op_type) {
NDEB(D(L, "Considering adding %p (%s) -> (%p) "
"from %p (%s) -> (%p)\n",
up, PL_op_name[up->op_type], up->op_next,
PL_op, PL_op_name[PL_op->op_type], PL_op->op_next));
add_conditional(aTHX_ up, 3);
if (up->op_next == PL_op->op_next)
break;
up = OpSIBLING(cLOGOPx(up)->op_first)->op_next;
}
#endif
add_conditional(aTHX_ PL_op, 3);
#if PERL_VERSION > 18
skipped = PL_op;
while ((skipped = find_skipped_conditional(aTHX_ skipped)) != NULL)
add_conditional(aTHX_ skipped, 2); /* Should this ever be 1? */
#endif
}
}
}
#if PERL_VERSION > 16
/* A sequence of variable declarations may have been optimized
* to a single OP_PADRANGE. The original sequence may span multiple lines,
* but only the first line has been marked as covered for now.
* Mark other OP_NEXTSTATE inside the original sequence of statements.
*/
static void cover_padrange(pTHX) {
dMY_CXT;
OP *next,
*orig;
if (!collecting(Statement)) return;
next = PL_op->op_next;
orig = OpSIBLING(PL_op);
/* Ignore padrange preparing subroutine call */
while (orig && orig != next) {
if (orig->op_type == OP_ENTERSUB) return;
orig = orig->op_next;
}
orig = OpSIBLING(PL_op);
while (orig && orig != next) {
if (orig->op_type == OP_NEXTSTATE) {
cover_statement(aTHX_ orig);
}
orig = orig->op_next;
}
}
static OP *dc_padrange(pTHX) {
dMY_CXT;
check_if_collecting(aTHX_ PL_curcop);
NDEB(D(L, "dc_padrange() at %p (%d)\n", PL_op, collecting_here(aTHX)));
if (MY_CXT.covering) cover_padrange(aTHX);
return MY_CXT.ppaddr[OP_PADRANGE](aTHX);
}
#endif
static OP *dc_nextstate(pTHX) {
dMY_CXT;
NDEB(D(L, "dc_nextstate() at %p (%d)\n", PL_op, collecting_here(aTHX)));