xref: /openbsd-src/gnu/usr.bin/perl/util.c (revision 68dd5bb1859285b71cb62a10bf107b8ad54064d9)
1 /*    util.c
2  *
3  *    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4  *    2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10 
11 /*
12  * 'Very useful, no doubt, that was to Saruman; yet it seems that he was
13  *  not content.'                                    --Gandalf to Pippin
14  *
15  *     [p.598 of _The Lord of the Rings_, III/xi: "The Palantír"]
16  */
17 
18 /* This file contains assorted utility routines.
19  * Which is a polite way of saying any stuff that people couldn't think of
20  * a better place for. Amongst other things, it includes the warning and
21  * dieing stuff, plus wrappers for malloc code.
22  */
23 
24 #include "EXTERN.h"
25 #define PERL_IN_UTIL_C
26 #include "perl.h"
27 #include "reentr.h"
28 
29 #if defined(USE_PERLIO)
30 #include "perliol.h" /* For PerlIOUnix_refcnt */
31 #endif
32 
33 #ifndef PERL_MICRO
34 #include <signal.h>
35 #ifndef SIG_ERR
36 # define SIG_ERR ((Sighandler_t) -1)
37 #endif
38 #endif
39 
40 #include <math.h>
41 #include <stdlib.h>
42 
43 #ifdef __Lynx__
44 /* Missing protos on LynxOS */
45 int putenv(char *);
46 #endif
47 
48 #ifdef __amigaos__
49 # include "amigaos4/amigaio.h"
50 #endif
51 
52 #ifdef HAS_SELECT
53 # ifdef I_SYS_SELECT
54 #  include <sys/select.h>
55 # endif
56 #endif
57 
58 #ifdef USE_C_BACKTRACE
59 #  ifdef I_BFD
60 #    define USE_BFD
61 #    ifdef PERL_DARWIN
62 #      undef USE_BFD /* BFD is useless in OS X. */
63 #    endif
64 #    ifdef USE_BFD
65 #      include <bfd.h>
66 #    endif
67 #  endif
68 #  ifdef I_DLFCN
69 #    include <dlfcn.h>
70 #  endif
71 #  ifdef I_EXECINFO
72 #    include <execinfo.h>
73 #  endif
74 #endif
75 
76 #ifdef PERL_DEBUG_READONLY_COW
77 # include <sys/mman.h>
78 #endif
79 
80 #define FLUSH
81 
82 /* NOTE:  Do not call the next three routines directly.  Use the macros
83  * in handy.h, so that we can easily redefine everything to do tracking of
84  * allocated hunks back to the original New to track down any memory leaks.
85  * XXX This advice seems to be widely ignored :-(   --AD  August 1996.
86  */
87 
88 #if defined (DEBUGGING) || defined(PERL_IMPLICIT_SYS) || defined (PERL_TRACK_MEMPOOL)
89 #  define ALWAYS_NEED_THX
90 #endif
91 
92 #if defined(PERL_TRACK_MEMPOOL) && defined(PERL_DEBUG_READONLY_COW)
93 static void
94 S_maybe_protect_rw(pTHX_ struct perl_memory_debug_header *header)
95 {
96     if (header->readonly
97      && mprotect(header, header->size, PROT_READ|PROT_WRITE))
98         Perl_warn(aTHX_ "mprotect for COW string %p %lu failed with %d",
99                          header, header->size, errno);
100 }
101 
102 static void
103 S_maybe_protect_ro(pTHX_ struct perl_memory_debug_header *header)
104 {
105     if (header->readonly
106      && mprotect(header, header->size, PROT_READ))
107         Perl_warn(aTHX_ "mprotect RW for COW string %p %lu failed with %d",
108                          header, header->size, errno);
109 }
110 # define maybe_protect_rw(foo) S_maybe_protect_rw(aTHX_ foo)
111 # define maybe_protect_ro(foo) S_maybe_protect_ro(aTHX_ foo)
112 #else
113 # define maybe_protect_rw(foo) NOOP
114 # define maybe_protect_ro(foo) NOOP
115 #endif
116 
117 #if defined(PERL_TRACK_MEMPOOL) || defined(PERL_DEBUG_READONLY_COW)
118  /* Use memory_debug_header */
119 # define USE_MDH
120 # if (defined(PERL_POISON) && defined(PERL_TRACK_MEMPOOL)) \
121    || defined(PERL_DEBUG_READONLY_COW)
122 #  define MDH_HAS_SIZE
123 # endif
124 #endif
125 
126 /*
127 =for apidoc_section $memory
128 =for apidoc safesysmalloc
129 Paranoid version of system's malloc()
130 
131 =cut
132 */
133 
134 Malloc_t
135 Perl_safesysmalloc(MEM_SIZE size)
136 {
137 #ifdef ALWAYS_NEED_THX
138     dTHX;
139 #endif
140     Malloc_t ptr;
141     dSAVEDERRNO;
142 
143 #ifdef USE_MDH
144     if (size + PERL_MEMORY_DEBUG_HEADER_SIZE < size)
145         goto out_of_memory;
146     size += PERL_MEMORY_DEBUG_HEADER_SIZE;
147 #endif
148 #ifdef DEBUGGING
149     if ((SSize_t)size < 0)
150         Perl_croak_nocontext("panic: malloc, size=%" UVuf, (UV) size);
151 #endif
152     if (!size) size = 1;	/* malloc(0) is NASTY on our system */
153     SAVE_ERRNO;
154 #ifdef PERL_DEBUG_READONLY_COW
155     if ((ptr = mmap(0, size, PROT_READ|PROT_WRITE,
156                     MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
157         perror("mmap failed");
158         abort();
159     }
160 #else
161     ptr = (Malloc_t)PerlMem_malloc(size);
162 #endif
163     PERL_ALLOC_CHECK(ptr);
164     if (ptr != NULL) {
165 #ifdef USE_MDH
166         struct perl_memory_debug_header *const header
167             = (struct perl_memory_debug_header *)ptr;
168 #endif
169 
170 #ifdef PERL_POISON
171         PoisonNew(((char *)ptr), size, char);
172 #endif
173 
174 #ifdef PERL_TRACK_MEMPOOL
175         header->interpreter = aTHX;
176         /* Link us into the list.  */
177         header->prev = &PL_memory_debug_header;
178         header->next = PL_memory_debug_header.next;
179         PL_memory_debug_header.next = header;
180         maybe_protect_rw(header->next);
181         header->next->prev = header;
182         maybe_protect_ro(header->next);
183 #  ifdef PERL_DEBUG_READONLY_COW
184         header->readonly = 0;
185 #  endif
186 #endif
187 #ifdef MDH_HAS_SIZE
188         header->size = size;
189 #endif
190         ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE);
191         DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) malloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
192 
193         /* malloc() can modify errno() even on success, but since someone
194            writing perl code doesn't have any control over when perl calls
195            malloc() we need to hide that.
196         */
197         RESTORE_ERRNO;
198     }
199     else {
200 #ifdef USE_MDH
201       out_of_memory:
202 #endif
203         {
204 #ifndef ALWAYS_NEED_THX
205             dTHX;
206 #endif
207             if (PL_nomemok)
208                 ptr =  NULL;
209             else
210                 croak_no_mem();
211         }
212     }
213     return ptr;
214 }
215 
216 /*
217 =for apidoc safesysrealloc
218 Paranoid version of system's realloc()
219 
220 =cut
221 */
222 
223 Malloc_t
224 Perl_safesysrealloc(Malloc_t where,MEM_SIZE size)
225 {
226 #ifdef ALWAYS_NEED_THX
227     dTHX;
228 #endif
229     Malloc_t ptr;
230 #ifdef PERL_DEBUG_READONLY_COW
231     const MEM_SIZE oldsize = where
232         ? ((struct perl_memory_debug_header *)((char *)where - PERL_MEMORY_DEBUG_HEADER_SIZE))->size
233         : 0;
234 #endif
235 
236     if (!size) {
237         safesysfree(where);
238         ptr = NULL;
239     }
240     else if (!where) {
241         ptr = safesysmalloc(size);
242     }
243     else {
244         dSAVE_ERRNO;
245 #ifdef USE_MDH
246         where = (Malloc_t)((char*)where-PERL_MEMORY_DEBUG_HEADER_SIZE);
247         if (size + PERL_MEMORY_DEBUG_HEADER_SIZE < size)
248             goto out_of_memory;
249         size += PERL_MEMORY_DEBUG_HEADER_SIZE;
250         {
251             struct perl_memory_debug_header *const header
252                 = (struct perl_memory_debug_header *)where;
253 
254 # ifdef PERL_TRACK_MEMPOOL
255             if (header->interpreter != aTHX) {
256                 Perl_croak_nocontext("panic: realloc from wrong pool, %p!=%p",
257                                      header->interpreter, aTHX);
258             }
259             assert(header->next->prev == header);
260             assert(header->prev->next == header);
261 #  ifdef PERL_POISON
262             if (header->size > size) {
263                 const MEM_SIZE freed_up = header->size - size;
264                 char *start_of_freed = ((char *)where) + size;
265                 PoisonFree(start_of_freed, freed_up, char);
266             }
267 #  endif
268 # endif
269 # ifdef MDH_HAS_SIZE
270             header->size = size;
271 # endif
272         }
273 #endif
274 #ifdef DEBUGGING
275         if ((SSize_t)size < 0)
276             Perl_croak_nocontext("panic: realloc, size=%" UVuf, (UV)size);
277 #endif
278 #ifdef PERL_DEBUG_READONLY_COW
279         if ((ptr = mmap(0, size, PROT_READ|PROT_WRITE,
280                         MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
281             perror("mmap failed");
282             abort();
283         }
284         Copy(where,ptr,oldsize < size ? oldsize : size,char);
285         if (munmap(where, oldsize)) {
286             perror("munmap failed");
287             abort();
288         }
289 #else
290         ptr = (Malloc_t)PerlMem_realloc(where,size);
291 #endif
292         PERL_ALLOC_CHECK(ptr);
293 
294     /* MUST do this fixup first, before doing ANYTHING else, as anything else
295        might allocate memory/free/move memory, and until we do the fixup, it
296        may well be chasing (and writing to) free memory.  */
297         if (ptr != NULL) {
298 #ifdef PERL_TRACK_MEMPOOL
299             struct perl_memory_debug_header *const header
300                 = (struct perl_memory_debug_header *)ptr;
301 
302 #  ifdef PERL_POISON
303             if (header->size < size) {
304                 const MEM_SIZE fresh = size - header->size;
305                 char *start_of_fresh = ((char *)ptr) + size;
306                 PoisonNew(start_of_fresh, fresh, char);
307             }
308 #  endif
309 
310             maybe_protect_rw(header->next);
311             header->next->prev = header;
312             maybe_protect_ro(header->next);
313             maybe_protect_rw(header->prev);
314             header->prev->next = header;
315             maybe_protect_ro(header->prev);
316 #endif
317             ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE);
318 
319             /* realloc() can modify errno() even on success, but since someone
320                writing perl code doesn't have any control over when perl calls
321                realloc() we need to hide that.
322             */
323             RESTORE_ERRNO;
324         }
325 
326     /* In particular, must do that fixup above before logging anything via
327      *printf(), as it can reallocate memory, which can cause SEGVs.  */
328 
329         DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) rfree\n",PTR2UV(where),(long)PL_an++));
330         DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) realloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
331 
332         if (ptr == NULL) {
333 #ifdef USE_MDH
334           out_of_memory:
335 #endif
336             {
337 #ifndef ALWAYS_NEED_THX
338                 dTHX;
339 #endif
340                 if (PL_nomemok)
341                     ptr = NULL;
342                 else
343                     croak_no_mem();
344             }
345         }
346     }
347     return ptr;
348 }
349 
350 /*
351 =for apidoc safesysfree
352 Safe version of system's free()
353 
354 =cut
355 */
356 
357 Free_t
358 Perl_safesysfree(Malloc_t where)
359 {
360 #ifdef ALWAYS_NEED_THX
361     dTHX;
362 #endif
363     DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) free\n",PTR2UV(where),(long)PL_an++));
364     if (where) {
365 #ifdef USE_MDH
366         Malloc_t where_intrn = (Malloc_t)((char*)where-PERL_MEMORY_DEBUG_HEADER_SIZE);
367         {
368             struct perl_memory_debug_header *const header
369                 = (struct perl_memory_debug_header *)where_intrn;
370 
371 # ifdef MDH_HAS_SIZE
372             const MEM_SIZE size = header->size;
373 # endif
374 # ifdef PERL_TRACK_MEMPOOL
375             if (header->interpreter != aTHX) {
376                 Perl_croak_nocontext("panic: free from wrong pool, %p!=%p",
377                                      header->interpreter, aTHX);
378             }
379             if (!header->prev) {
380                 Perl_croak_nocontext("panic: duplicate free");
381             }
382             if (!(header->next))
383                 Perl_croak_nocontext("panic: bad free, header->next==NULL");
384             if (header->next->prev != header || header->prev->next != header) {
385                 Perl_croak_nocontext("panic: bad free, ->next->prev=%p, "
386                                      "header=%p, ->prev->next=%p",
387                                      header->next->prev, header,
388                                      header->prev->next);
389             }
390             /* Unlink us from the chain.  */
391             maybe_protect_rw(header->next);
392             header->next->prev = header->prev;
393             maybe_protect_ro(header->next);
394             maybe_protect_rw(header->prev);
395             header->prev->next = header->next;
396             maybe_protect_ro(header->prev);
397             maybe_protect_rw(header);
398 #  ifdef PERL_POISON
399             PoisonNew(where_intrn, size, char);
400 #  endif
401             /* Trigger the duplicate free warning.  */
402             header->next = NULL;
403 # endif
404 # ifdef PERL_DEBUG_READONLY_COW
405             if (munmap(where_intrn, size)) {
406                 perror("munmap failed");
407                 abort();
408             }
409 # endif
410         }
411 #else
412         Malloc_t where_intrn = where;
413 #endif /* USE_MDH */
414 #ifndef PERL_DEBUG_READONLY_COW
415         PerlMem_free(where_intrn);
416 #endif
417     }
418 }
419 
420 /*
421 =for apidoc safesyscalloc
422 Safe version of system's calloc()
423 
424 =cut
425 */
426 
427 Malloc_t
428 Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size)
429 {
430 #ifdef ALWAYS_NEED_THX
431     dTHX;
432 #endif
433     Malloc_t ptr;
434 #if defined(USE_MDH) || defined(DEBUGGING)
435     MEM_SIZE total_size = 0;
436 #endif
437 
438     /* Even though calloc() for zero bytes is strange, be robust. */
439     if (size && (count <= MEM_SIZE_MAX / size)) {
440 #if defined(USE_MDH) || defined(DEBUGGING)
441         total_size = size * count;
442 #endif
443     }
444     else
445         croak_memory_wrap();
446 #ifdef USE_MDH
447     if (PERL_MEMORY_DEBUG_HEADER_SIZE <= MEM_SIZE_MAX - (MEM_SIZE)total_size)
448         total_size += PERL_MEMORY_DEBUG_HEADER_SIZE;
449     else
450         croak_memory_wrap();
451 #endif
452 #ifdef DEBUGGING
453     if ((SSize_t)size < 0 || (SSize_t)count < 0)
454         Perl_croak_nocontext("panic: calloc, size=%" UVuf ", count=%" UVuf,
455                              (UV)size, (UV)count);
456 #endif
457 #ifdef PERL_DEBUG_READONLY_COW
458     if ((ptr = mmap(0, total_size ? total_size : 1, PROT_READ|PROT_WRITE,
459                     MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
460         perror("mmap failed");
461         abort();
462     }
463 #elif defined(PERL_TRACK_MEMPOOL)
464     /* Have to use malloc() because we've added some space for our tracking
465        header.  */
466     /* malloc(0) is non-portable. */
467     ptr = (Malloc_t)PerlMem_malloc(total_size ? total_size : 1);
468 #else
469     /* Use calloc() because it might save a memset() if the memory is fresh
470        and clean from the OS.  */
471     if (count && size)
472         ptr = (Malloc_t)PerlMem_calloc(count, size);
473     else /* calloc(0) is non-portable. */
474         ptr = (Malloc_t)PerlMem_calloc(count ? count : 1, size ? size : 1);
475 #endif
476     PERL_ALLOC_CHECK(ptr);
477     DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) calloc %zu x %zu = %zu bytes\n",PTR2UV(ptr),(long)PL_an++, count, size, total_size));
478     if (ptr != NULL) {
479 #ifdef USE_MDH
480         {
481             struct perl_memory_debug_header *const header
482                 = (struct perl_memory_debug_header *)ptr;
483 
484 #  ifndef PERL_DEBUG_READONLY_COW
485             memset((void*)ptr, 0, total_size);
486 #  endif
487 #  ifdef PERL_TRACK_MEMPOOL
488             header->interpreter = aTHX;
489             /* Link us into the list.  */
490             header->prev = &PL_memory_debug_header;
491             header->next = PL_memory_debug_header.next;
492             PL_memory_debug_header.next = header;
493             maybe_protect_rw(header->next);
494             header->next->prev = header;
495             maybe_protect_ro(header->next);
496 #    ifdef PERL_DEBUG_READONLY_COW
497             header->readonly = 0;
498 #    endif
499 #  endif
500 #  ifdef MDH_HAS_SIZE
501             header->size = total_size;
502 #  endif
503             ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE);
504         }
505 #endif
506         return ptr;
507     }
508     else {
509 #ifndef ALWAYS_NEED_THX
510         dTHX;
511 #endif
512         if (PL_nomemok)
513             return NULL;
514         croak_no_mem();
515     }
516 }
517 
518 /* These must be defined when not using Perl's malloc for binary
519  * compatibility */
520 
521 #ifndef MYMALLOC
522 
523 Malloc_t Perl_malloc (MEM_SIZE nbytes)
524 {
525 #ifdef PERL_IMPLICIT_SYS
526     dTHX;
527 #endif
528     return (Malloc_t)PerlMem_malloc(nbytes);
529 }
530 
531 Malloc_t Perl_calloc (MEM_SIZE elements, MEM_SIZE size)
532 {
533 #ifdef PERL_IMPLICIT_SYS
534     dTHX;
535 #endif
536     return (Malloc_t)PerlMem_calloc(elements, size);
537 }
538 
539 Malloc_t Perl_realloc (Malloc_t where, MEM_SIZE nbytes)
540 {
541 #ifdef PERL_IMPLICIT_SYS
542     dTHX;
543 #endif
544     return (Malloc_t)PerlMem_realloc(where, nbytes);
545 }
546 
547 Free_t   Perl_mfree (Malloc_t where)
548 {
549 #ifdef PERL_IMPLICIT_SYS
550     dTHX;
551 #endif
552     PerlMem_free(where);
553 }
554 
555 #endif
556 
557 /* This is the value stored in *retlen in the two delimcpy routines below when
558  * there wasn't enough room in the destination to store everything it was asked
559  * to.  The value is deliberately very large so that hopefully if code uses it
560  * unquestioninly to access memory, it will likely segfault.  And it is small
561  * enough that if the caller does some arithmetic on it before accessing, it
562  * won't overflow into a small legal number. */
563 #define DELIMCPY_OUT_OF_BOUNDS_RET  I32_MAX
564 
565 /*
566 =for apidoc_section $string
567 =for apidoc delimcpy_no_escape
568 
569 Copy a source buffer to a destination buffer, stopping at (but not including)
570 the first occurrence in the source of the delimiter byte, C<delim>.  The source
571 is the bytes between S<C<from> and C<from_end> - 1>.  Similarly, the dest is
572 C<to> up to C<to_end>.
573 
574 The number of bytes copied is written to C<*retlen>.
575 
576 Returns the position of C<delim> in the C<from> buffer, but if there is no
577 such occurrence before C<from_end>, then C<from_end> is returned, and the entire
578 buffer S<C<from> .. C<from_end> - 1> is copied.
579 
580 If there is room in the destination available after the copy, an extra
581 terminating safety C<NUL> byte is appended (not included in the returned
582 length).
583 
584 The error case is if the destination buffer is not large enough to accommodate
585 everything that should be copied.  In this situation, a value larger than
586 S<C<to_end> - C<to>> is written to C<*retlen>, and as much of the source as
587 fits will be written to the destination.  Not having room for the safety C<NUL>
588 is not considered an error.
589 
590 =cut
591 */
592 char *
593 Perl_delimcpy_no_escape(char *to, const char *to_end,
594                         const char *from, const char *from_end,
595                         const int delim, I32 *retlen)
596 {
597     const char * delim_pos;
598     Ptrdiff_t from_len = from_end - from;
599     Ptrdiff_t to_len = to_end - to;
600     SSize_t copy_len;
601 
602     PERL_ARGS_ASSERT_DELIMCPY_NO_ESCAPE;
603 
604     assert(from_len >= 0);
605     assert(to_len >= 0);
606 
607     /* Look for the first delimiter in the source */
608     delim_pos = (const char *) memchr(from, delim, from_len);
609 
610     /* Copy up to where the delimiter was found, or the entire buffer if not
611      * found */
612     copy_len = (delim_pos) ? delim_pos - from : from_len;
613 
614     /* If not enough room, copy as much as can fit, and set error return */
615     if (copy_len > to_len) {
616         Copy(from, to, to_len, char);
617         *retlen = DELIMCPY_OUT_OF_BOUNDS_RET;
618     }
619     else {
620         Copy(from, to, copy_len, char);
621 
622         /* If there is extra space available, add a trailing NUL */
623         if (copy_len < to_len) {
624             to[copy_len] = '\0';
625         }
626 
627         *retlen = copy_len;
628     }
629 
630     return (char *) from + copy_len;
631 }
632 
633 /*
634 =for apidoc delimcpy
635 
636 Copy a source buffer to a destination buffer, stopping at (but not including)
637 the first occurrence in the source of an unescaped (defined below) delimiter
638 byte, C<delim>.  The source is the bytes between S<C<from> and C<from_end> -
639 1>.  Similarly, the dest is C<to> up to C<to_end>.
640 
641 The number of bytes copied is written to C<*retlen>.
642 
643 Returns the position of the first uncopied C<delim> in the C<from> buffer, but
644 if there is no such occurrence before C<from_end>, then C<from_end> is returned,
645 and the entire buffer S<C<from> .. C<from_end> - 1> is copied.
646 
647 If there is room in the destination available after the copy, an extra
648 terminating safety C<NUL> byte is appended (not included in the returned
649 length).
650 
651 The error case is if the destination buffer is not large enough to accommodate
652 everything that should be copied.  In this situation, a value larger than
653 S<C<to_end> - C<to>> is written to C<*retlen>, and as much of the source as
654 fits will be written to the destination.  Not having room for the safety C<NUL>
655 is not considered an error.
656 
657 In the following examples, let C<x> be the delimiter, and C<0> represent a C<NUL>
658 byte (B<NOT> the digit C<0>).  Then we would have
659 
660   Source     Destination
661  abcxdef        abc0
662 
663 provided the destination buffer is at least 4 bytes long.
664 
665 An escaped delimiter is one which is immediately preceded by a single
666 backslash.  Escaped delimiters are copied, and the copy continues past the
667 delimiter; the backslash is not copied:
668 
669   Source       Destination
670  abc\xdef       abcxdef0
671 
672 (provided the destination buffer is at least 8 bytes long).
673 
674 It's actually somewhat more complicated than that. A sequence of any odd number
675 of backslashes escapes the following delimiter, and the copy continues with
676 exactly one of the backslashes stripped.
677 
678      Source         Destination
679      abc\xdef          abcxdef0
680    abc\\\xdef        abc\\xdef0
681  abc\\\\\xdef      abc\\\\xdef0
682 
683 (as always, if the destination is large enough)
684 
685 An even number of preceding backslashes does not escape the delimiter, so that
686 the copy stops just before it, and includes all the backslashes (no stripping;
687 zero is considered even):
688 
689       Source         Destination
690       abcxdef          abc0
691     abc\\xdef          abc\\0
692   abc\\\\xdef          abc\\\\0
693 
694 =cut
695 */
696 
697 char *
698 Perl_delimcpy(char *to, const char *to_end,
699               const char *from, const char *from_end,
700               const int delim, I32 *retlen)
701 {
702     const char * const orig_to = to;
703     Ptrdiff_t copy_len = 0;
704     bool stopped_early = FALSE;     /* Ran out of room to copy to */
705 
706     PERL_ARGS_ASSERT_DELIMCPY;
707     assert(from_end >= from);
708     assert(to_end >= to);
709 
710     /* Don't use the loop for the trivial case of the first character being the
711      * delimiter; otherwise would have to worry inside the loop about backing
712      * up before the start of 'from' */
713     if (LIKELY(from_end > from && *from != delim)) {
714         while ((copy_len = from_end - from) > 0) {
715             const char * backslash_pos;
716             const char * delim_pos;
717 
718             /* Look for the next delimiter in the remaining portion of the
719              * source. A loop invariant is that we already know that the copy
720              * should include *from; this comes from the conditional before the
721              * loop, and how we set things up at the end of each iteration */
722             delim_pos = (const char *) memchr(from + 1, delim, copy_len - 1);
723 
724             /* If didn't find it, done looking; set up so copies all of the
725              * source */
726             if (! delim_pos) {
727                 copy_len = from_end - from;
728                 break;
729             }
730 
731             /* Look for a backslash immediately before the delimiter */
732             backslash_pos = delim_pos - 1;
733 
734             /* If the delimiter is not escaped, this ends the copy */
735             if (*backslash_pos != '\\') {
736                 copy_len = delim_pos - from;
737                 break;
738             }
739 
740             /* Here there is a backslash just before the delimiter, but it
741              * could be the final backslash in a sequence of them.  Backup to
742              * find the first one in it. */
743             do {
744                 backslash_pos--;
745             }
746             while (backslash_pos >= from && *backslash_pos == '\\');
747 
748             /* If the number of backslashes is even, they just escape one
749              * another, leaving the delimiter unescaped, and stopping the copy.
750              * */
751             if (! ((delim_pos - (backslash_pos + 1)) & 1)) {
752                 copy_len = delim_pos - from;  /* even, copy up to delimiter */
753                 break;
754             }
755 
756             /* Here is odd, so the delimiter is escaped.  We will try to copy
757              * all but the final backslash in the sequence */
758             copy_len = delim_pos - 1 - from;
759 
760             /* Do the copy, but not beyond the end of the destination */
761             if (copy_len >= to_end - to) {
762                 Copy(from, to, to_end - to, char);
763                 stopped_early = TRUE;
764                 to = (char *) to_end;
765             }
766             else {
767                 Copy(from, to, copy_len, char);
768                 to += copy_len;
769             }
770 
771             /* Set up so next iteration will include the delimiter */
772             from = delim_pos;
773         }
774     }
775 
776     /* Here, have found the final segment to copy.  Copy that, but not beyond
777      * the size of the destination.  If not enough room, copy as much as can
778      * fit, and set error return */
779     if (stopped_early || copy_len > to_end - to) {
780         Copy(from, to, to_end - to, char);
781         *retlen = DELIMCPY_OUT_OF_BOUNDS_RET;
782     }
783     else {
784         Copy(from, to, copy_len, char);
785 
786         to += copy_len;
787 
788         /* If there is extra space available, add a trailing NUL */
789         if (to < to_end) {
790             *to = '\0';
791         }
792 
793         *retlen = to - orig_to;
794     }
795 
796     return (char *) from + copy_len;
797 }
798 
799 /*
800 =for apidoc ninstr
801 
802 Find the first (leftmost) occurrence of a sequence of bytes within another
803 sequence.  This is the Perl version of C<strstr()>, extended to handle
804 arbitrary sequences, potentially containing embedded C<NUL> characters (C<NUL>
805 is what the initial C<n> in the function name stands for; some systems have an
806 equivalent, C<memmem()>, but with a somewhat different API).
807 
808 Another way of thinking about this function is finding a needle in a haystack.
809 C<big> points to the first byte in the haystack.  C<big_end> points to one byte
810 beyond the final byte in the haystack.  C<little> points to the first byte in
811 the needle.  C<little_end> points to one byte beyond the final byte in the
812 needle.  All the parameters must be non-C<NULL>.
813 
814 The function returns C<NULL> if there is no occurrence of C<little> within
815 C<big>.  If C<little> is the empty string, C<big> is returned.
816 
817 Because this function operates at the byte level, and because of the inherent
818 characteristics of UTF-8 (or UTF-EBCDIC), it will work properly if both the
819 needle and the haystack are strings with the same UTF-8ness, but not if the
820 UTF-8ness differs.
821 
822 =cut
823 
824 */
825 
826 char *
827 Perl_ninstr(const char *big, const char *bigend, const char *little, const char *lend)
828 {
829     PERL_ARGS_ASSERT_NINSTR;
830 
831 #ifdef HAS_MEMMEM
832     return ninstr(big, bigend, little, lend);
833 #else
834 
835     if (little >= lend) {
836         return (char*) big;
837     }
838     else {
839         const U8 first = *little;
840         Size_t lsize;
841 
842         /* No match can start closer to the end of the haystack than the length
843          * of the needle. */
844         bigend -= lend - little;
845         little++;       /* Look for 'first', then the remainder is in here */
846         lsize = lend - little;
847 
848         while (big <= bigend) {
849             big = (char *) memchr((U8 *) big, first, bigend - big + 1);
850             if (big == NULL || big > bigend) {
851                 return NULL;
852             }
853 
854             if (memEQ(big + 1, little, lsize)) {
855                 return (char*) big;
856             }
857             big++;
858         }
859     }
860 
861     return NULL;
862 
863 #endif
864 
865 }
866 
867 /*
868 =for apidoc rninstr
869 
870 Like C<L</ninstr>>, but instead finds the final (rightmost) occurrence of a
871 sequence of bytes within another sequence, returning C<NULL> if there is no
872 such occurrence.
873 
874 =cut
875 
876 */
877 
878 char *
879 Perl_rninstr(const char *big, const char *bigend, const char *little, const char *lend)
880 {
881     const Ptrdiff_t little_len = lend - little;
882     const Ptrdiff_t big_len = bigend - big;
883 
884     PERL_ARGS_ASSERT_RNINSTR;
885 
886     /* A non-existent needle trivially matches the rightmost possible position
887      * in the haystack */
888     if (UNLIKELY(little_len <= 0)) {
889         return (char*)bigend;
890     }
891 
892     /* If the needle is larger than the haystack, the needle can't possibly fit
893      * inside the haystack. */
894     if (UNLIKELY(little_len > big_len)) {
895         return NULL;
896     }
897 
898     /* Special case length 1 needles.  It's trivial if we have memrchr();
899      * and otherwise we just do a per-byte search backwards.
900      *
901      * XXX When we don't have memrchr, we could use something like
902      * S_find_next_masked( or S_find_span_end() to do per-word searches */
903     if (little_len == 1) {
904         const char final = *little;
905 
906 #ifdef HAS_MEMRCHR
907 
908         return (char *) memrchr(big, final, big_len);
909 #else
910         const char * cur = bigend - 1;
911 
912         do {
913             if (*cur == final) {
914                 return (char *) cur;
915             }
916         } while (--cur >= big);
917 
918         return NULL;
919 #endif
920 
921     }
922     else {  /* Below, the needle is longer than a single byte */
923 
924         /* We search backwards in the haystack for the final character of the
925          * needle.  Each time one is found, we see if the characters just
926          * before it in the haystack match the rest of the needle. */
927         const char final = *(lend - 1);
928 
929         /* What matches consists of 'little_len'-1 characters, then the final
930          * one */
931         const Size_t prefix_len = little_len - 1;
932 
933         /* If the final character in the needle is any closer than this to the
934          * left edge, there wouldn't be enough room for all of it to fit in the
935          * haystack */
936         const char * const left_fence = big + prefix_len;
937 
938         /* Start at the right edge */
939         char * cur = (char *) bigend;
940 
941         /* memrchr() makes the search easy (and fast); otherwise, look
942          * backwards byte-by-byte. */
943         do {
944 
945 #ifdef HAS_MEMRCHR
946 
947             cur = (char *) memrchr(left_fence, final, cur - left_fence);
948             if (cur == NULL) {
949                 return NULL;
950             }
951 #else
952             do {
953                 cur--;
954                 if (cur < left_fence) {
955                     return NULL;
956                 }
957             }
958             while (*cur != final);
959 #endif
960 
961             /* Here, we know that *cur is 'final'; see if the preceding bytes
962              * of the needle also match the corresponding haystack bytes */
963             if memEQ(cur - prefix_len, little, prefix_len) {
964                 return cur - prefix_len;
965             }
966         } while (cur > left_fence);
967 
968         return NULL;
969     }
970 }
971 
972 /* As a space optimization, we do not compile tables for strings of length
973    0 and 1, and for strings of length 2 unless FBMcf_TAIL.  These are
974    special-cased in fbm_instr().
975 
976    If FBMcf_TAIL, the table is created as if the string has a trailing \n. */
977 
978 /*
979 
980 =for apidoc fbm_compile
981 
982 Analyzes the string in order to make fast searches on it using C<fbm_instr()>
983 -- the Boyer-Moore algorithm.
984 
985 =cut
986 */
987 
988 void
989 Perl_fbm_compile(pTHX_ SV *sv, U32 flags)
990 {
991     const U8 *s;
992     STRLEN i;
993     STRLEN len;
994     MAGIC *mg;
995 
996     PERL_ARGS_ASSERT_FBM_COMPILE;
997 
998     if (isGV_with_GP(sv) || SvROK(sv))
999         return;
1000 
1001     if (SvVALID(sv))
1002         return;
1003 
1004     if (flags & FBMcf_TAIL) {
1005         MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
1006         sv_catpvs(sv, "\n");		/* Taken into account in fbm_instr() */
1007         if (mg && mg->mg_len >= 0)
1008             mg->mg_len++;
1009     }
1010     if (!SvPOK(sv) || SvNIOKp(sv))
1011         s = (U8*)SvPV_force_mutable(sv, len);
1012     else s = (U8 *)SvPV_mutable(sv, len);
1013     if (len == 0)		/* TAIL might be on a zero-length string. */
1014         return;
1015     SvUPGRADE(sv, SVt_PVMG);
1016     SvIOK_off(sv);
1017     SvNOK_off(sv);
1018 
1019     /* add PERL_MAGIC_bm magic holding the FBM lookup table */
1020 
1021     assert(!mg_find(sv, PERL_MAGIC_bm));
1022     mg = sv_magicext(sv, NULL, PERL_MAGIC_bm, &PL_vtbl_bm, NULL, 0);
1023     assert(mg);
1024 
1025     if (len > 2) {
1026         /* Shorter strings are special-cased in Perl_fbm_instr(), and don't use
1027            the BM table.  */
1028         const U8 mlen = (len>255) ? 255 : (U8)len;
1029         const unsigned char *const sb = s + len - mlen; /* first char (maybe) */
1030         U8 *table;
1031 
1032         Newx(table, 256, U8);
1033         memset((void*)table, mlen, 256);
1034         mg->mg_ptr = (char *)table;
1035         mg->mg_len = 256;
1036 
1037         s += len - 1; /* last char */
1038         i = 0;
1039         while (s >= sb) {
1040             if (table[*s] == mlen)
1041                 table[*s] = (U8)i;
1042             s--, i++;
1043         }
1044     }
1045 
1046     BmUSEFUL(sv) = 100;			/* Initial value */
1047     ((XPVNV*)SvANY(sv))->xnv_u.xnv_bm_tail = cBOOL(flags & FBMcf_TAIL);
1048 }
1049 
1050 
1051 /*
1052 =for apidoc fbm_instr
1053 
1054 Returns the location of the SV in the string delimited by C<big> and
1055 C<bigend> (C<bigend>) is the char following the last char).
1056 It returns C<NULL> if the string can't be found.  The C<sv>
1057 does not have to be C<fbm_compiled>, but the search will not be as fast
1058 then.
1059 
1060 =cut
1061 
1062 If SvTAIL(littlestr) is true, a fake "\n" was appended to the string
1063 during FBM compilation due to FBMcf_TAIL in flags. It indicates that
1064 the littlestr must be anchored to the end of bigstr (or to any \n if
1065 FBMrf_MULTILINE).
1066 
1067 E.g. The regex compiler would compile /abc/ to a littlestr of "abc",
1068 while /abc$/ compiles to "abc\n" with SvTAIL() true.
1069 
1070 A littlestr of "abc", !SvTAIL matches as /abc/;
1071 a littlestr of "ab\n", SvTAIL matches as:
1072    without FBMrf_MULTILINE: /ab\n?\z/
1073    with    FBMrf_MULTILINE: /ab\n/ || /ab\z/;
1074 
1075 (According to Ilya from 1999; I don't know if this is still true, DAPM 2015):
1076   "If SvTAIL is actually due to \Z or \z, this gives false positives
1077   if multiline".
1078 */
1079 
1080 
1081 char *
1082 Perl_fbm_instr(pTHX_ unsigned char *big, unsigned char *bigend, SV *littlestr, U32 flags)
1083 {
1084     unsigned char *s;
1085     STRLEN l;
1086     const unsigned char *little = (const unsigned char *)SvPV_const(littlestr,l);
1087     STRLEN littlelen = l;
1088     const I32 multiline = flags & FBMrf_MULTILINE;
1089     bool valid = SvVALID(littlestr);
1090     bool tail = valid ? cBOOL(SvTAIL(littlestr)) : FALSE;
1091 
1092     PERL_ARGS_ASSERT_FBM_INSTR;
1093 
1094     assert(bigend >= big);
1095 
1096     if ((STRLEN)(bigend - big) < littlelen) {
1097         if (     tail
1098              && ((STRLEN)(bigend - big) == littlelen - 1)
1099              && (littlelen == 1
1100                  || (*big == *little &&
1101                      memEQ((char *)big, (char *)little, littlelen - 1))))
1102             return (char*)big;
1103         return NULL;
1104     }
1105 
1106     switch (littlelen) { /* Special cases for 0, 1 and 2  */
1107     case 0:
1108         return (char*)big;		/* Cannot be SvTAIL! */
1109 
1110     case 1:
1111             if (tail && !multiline) /* Anchor only! */
1112                 /* [-1] is safe because we know that bigend != big.  */
1113                 return (char *) (bigend - (bigend[-1] == '\n'));
1114 
1115             s = (unsigned char *)memchr((void*)big, *little, bigend-big);
1116             if (s)
1117                 return (char *)s;
1118             if (tail)
1119                 return (char *) bigend;
1120             return NULL;
1121 
1122     case 2:
1123         if (tail && !multiline) {
1124             /* a littlestr with SvTAIL must be of the form "X\n" (where X
1125              * is a single char). It is anchored, and can only match
1126              * "....X\n"  or  "....X" */
1127             if (bigend[-2] == *little && bigend[-1] == '\n')
1128                 return (char*)bigend - 2;
1129             if (bigend[-1] == *little)
1130                 return (char*)bigend - 1;
1131             return NULL;
1132         }
1133 
1134         {
1135             /* memchr() is likely to be very fast, possibly using whatever
1136              * hardware support is available, such as checking a whole
1137              * cache line in one instruction.
1138              * So for a 2 char pattern, calling memchr() is likely to be
1139              * faster than running FBM, or rolling our own. The previous
1140              * version of this code was roll-your-own which typically
1141              * only needed to read every 2nd char, which was good back in
1142              * the day, but no longer.
1143              */
1144             unsigned char c1 = little[0];
1145             unsigned char c2 = little[1];
1146 
1147             /* *** for all this case, bigend points to the last char,
1148              * not the trailing \0: this makes the conditions slightly
1149              * simpler */
1150             bigend--;
1151             s = big;
1152             if (c1 != c2) {
1153                 while (s < bigend) {
1154                     /* do a quick test for c1 before calling memchr();
1155                      * this avoids the expensive fn call overhead when
1156                      * there are lots of c1's */
1157                     if (LIKELY(*s != c1)) {
1158                         s++;
1159                         s = (unsigned char *)memchr((void*)s, c1, bigend - s);
1160                         if (!s)
1161                             break;
1162                     }
1163                     if (s[1] == c2)
1164                         return (char*)s;
1165 
1166                     /* failed; try searching for c2 this time; that way
1167                      * we don't go pathologically slow when the string
1168                      * consists mostly of c1's or vice versa.
1169                      */
1170                     s += 2;
1171                     if (s > bigend)
1172                         break;
1173                     s = (unsigned char *)memchr((void*)s, c2, bigend - s + 1);
1174                     if (!s)
1175                         break;
1176                     if (s[-1] == c1)
1177                         return (char*)s - 1;
1178                 }
1179             }
1180             else {
1181                 /* c1, c2 the same */
1182                 while (s < bigend) {
1183                     if (s[0] == c1) {
1184                       got_1char:
1185                         if (s[1] == c1)
1186                             return (char*)s;
1187                         s += 2;
1188                     }
1189                     else {
1190                         s++;
1191                         s = (unsigned char *)memchr((void*)s, c1, bigend - s);
1192                         if (!s || s >= bigend)
1193                             break;
1194                         goto got_1char;
1195                     }
1196                 }
1197             }
1198 
1199             /* failed to find 2 chars; try anchored match at end without
1200              * the \n */
1201             if (tail && bigend[0] == little[0])
1202                 return (char *)bigend;
1203             return NULL;
1204         }
1205 
1206     default:
1207         break; /* Only lengths 0 1 and 2 have special-case code.  */
1208     }
1209 
1210     if (tail && !multiline) {	/* tail anchored? */
1211         s = bigend - littlelen;
1212         if (s >= big && bigend[-1] == '\n' && *s == *little
1213             /* Automatically of length > 2 */
1214             && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
1215         {
1216             return (char*)s;		/* how sweet it is */
1217         }
1218         if (s[1] == *little
1219             && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2))
1220         {
1221             return (char*)s + 1;	/* how sweet it is */
1222         }
1223         return NULL;
1224     }
1225 
1226     if (!valid) {
1227         /* not compiled; use Perl_ninstr() instead */
1228         char * const b = ninstr((char*)big,(char*)bigend,
1229                          (char*)little, (char*)little + littlelen);
1230 
1231         assert(!tail); /* valid => FBM; tail only set on SvVALID SVs */
1232         return b;
1233     }
1234 
1235     /* Do actual FBM.  */
1236     if (littlelen > (STRLEN)(bigend - big))
1237         return NULL;
1238 
1239     {
1240         const MAGIC *const mg = mg_find(littlestr, PERL_MAGIC_bm);
1241         const unsigned char *oldlittle;
1242 
1243         assert(mg);
1244 
1245         --littlelen;			/* Last char found by table lookup */
1246 
1247         s = big + littlelen;
1248         little += littlelen;		/* last char */
1249         oldlittle = little;
1250         if (s < bigend) {
1251             const unsigned char * const table = (const unsigned char *) mg->mg_ptr;
1252             const unsigned char lastc = *little;
1253             I32 tmp;
1254 
1255           top2:
1256             if ((tmp = table[*s])) {
1257                 /* *s != lastc; earliest position it could match now is
1258                  * tmp slots further on */
1259                 if ((s += tmp) >= bigend)
1260                     goto check_end;
1261                 if (LIKELY(*s != lastc)) {
1262                     s++;
1263                     s = (unsigned char *)memchr((void*)s, lastc, bigend - s);
1264                     if (!s) {
1265                         s = bigend;
1266                         goto check_end;
1267                     }
1268                     goto top2;
1269                 }
1270             }
1271 
1272 
1273             /* hand-rolled strncmp(): less expensive than calling the
1274              * real function (maybe???) */
1275             {
1276                 unsigned char * const olds = s;
1277 
1278                 tmp = littlelen;
1279 
1280                 while (tmp--) {
1281                     if (*--s == *--little)
1282                         continue;
1283                     s = olds + 1;	/* here we pay the price for failure */
1284                     little = oldlittle;
1285                     if (s < bigend)	/* fake up continue to outer loop */
1286                         goto top2;
1287                     goto check_end;
1288                 }
1289                 return (char *)s;
1290             }
1291         }
1292       check_end:
1293         if ( s == bigend
1294              && tail
1295              && memEQ((char *)(bigend - littlelen),
1296                       (char *)(oldlittle - littlelen), littlelen) )
1297             return (char*)bigend - littlelen;
1298         return NULL;
1299     }
1300 }
1301 
1302 const char *
1303 Perl_cntrl_to_mnemonic(const U8 c)
1304 {
1305     /* Returns the mnemonic string that represents character 'c', if one
1306      * exists; NULL otherwise.  The only ones that exist for the purposes of
1307      * this routine are a few control characters */
1308 
1309     switch (c) {
1310         case '\a':       return "\\a";
1311         case '\b':       return "\\b";
1312         case ESC_NATIVE: return "\\e";
1313         case '\f':       return "\\f";
1314         case '\n':       return "\\n";
1315         case '\r':       return "\\r";
1316         case '\t':       return "\\t";
1317     }
1318 
1319     return NULL;
1320 }
1321 
1322 /* copy a string to a safe spot */
1323 
1324 /*
1325 =for apidoc_section $string
1326 =for apidoc savepv
1327 
1328 Perl's version of C<strdup()>.  Returns a pointer to a newly allocated
1329 string which is a duplicate of C<pv>.  The size of the string is
1330 determined by C<strlen()>, which means it may not contain embedded C<NUL>
1331 characters and must have a trailing C<NUL>.  To prevent memory leaks, the
1332 memory allocated for the new string needs to be freed when no longer needed.
1333 This can be done with the C<L</Safefree>> function, or
1334 L<C<SAVEFREEPV>|perlguts/SAVEFREEPV(p)>.
1335 
1336 On some platforms, Windows for example, all allocated memory owned by a thread
1337 is deallocated when that thread ends.  So if you need that not to happen, you
1338 need to use the shared memory functions, such as C<L</savesharedpv>>.
1339 
1340 =cut
1341 */
1342 
1343 char *
1344 Perl_savepv(pTHX_ const char *pv)
1345 {
1346     PERL_UNUSED_CONTEXT;
1347     if (!pv)
1348         return NULL;
1349     else {
1350         char *newaddr;
1351         const STRLEN pvlen = strlen(pv)+1;
1352         Newx(newaddr, pvlen, char);
1353         return (char*)memcpy(newaddr, pv, pvlen);
1354     }
1355 }
1356 
1357 /* same thing but with a known length */
1358 
1359 /*
1360 =for apidoc savepvn
1361 
1362 Perl's version of what C<strndup()> would be if it existed.  Returns a
1363 pointer to a newly allocated string which is a duplicate of the first
1364 C<len> bytes from C<pv>, plus a trailing
1365 C<NUL> byte.  The memory allocated for
1366 the new string can be freed with the C<Safefree()> function.
1367 
1368 On some platforms, Windows for example, all allocated memory owned by a thread
1369 is deallocated when that thread ends.  So if you need that not to happen, you
1370 need to use the shared memory functions, such as C<L</savesharedpvn>>.
1371 
1372 =cut
1373 */
1374 
1375 char *
1376 Perl_savepvn(pTHX_ const char *pv, Size_t len)
1377 {
1378     char *newaddr;
1379     PERL_UNUSED_CONTEXT;
1380 
1381     Newx(newaddr,len+1,char);
1382     /* Give a meaning to NULL pointer mainly for the use in sv_magic() */
1383     if (pv) {
1384         /* might not be null terminated */
1385         newaddr[len] = '\0';
1386         return (char *) CopyD(pv,newaddr,len,char);
1387     }
1388     else {
1389         return (char *) ZeroD(newaddr,len+1,char);
1390     }
1391 }
1392 
1393 /*
1394 =for apidoc savesharedpv
1395 
1396 A version of C<savepv()> which allocates the duplicate string in memory
1397 which is shared between threads.
1398 
1399 =cut
1400 */
1401 char *
1402 Perl_savesharedpv(pTHX_ const char *pv)
1403 {
1404     char *newaddr;
1405     STRLEN pvlen;
1406 
1407     PERL_UNUSED_CONTEXT;
1408 
1409     if (!pv)
1410         return NULL;
1411 
1412     pvlen = strlen(pv)+1;
1413     newaddr = (char*)PerlMemShared_malloc(pvlen);
1414     if (!newaddr) {
1415         croak_no_mem();
1416     }
1417     return (char*)memcpy(newaddr, pv, pvlen);
1418 }
1419 
1420 /*
1421 =for apidoc savesharedpvn
1422 
1423 A version of C<savepvn()> which allocates the duplicate string in memory
1424 which is shared between threads.  (With the specific difference that a C<NULL>
1425 pointer is not acceptable)
1426 
1427 =cut
1428 */
1429 char *
1430 Perl_savesharedpvn(pTHX_ const char *const pv, const STRLEN len)
1431 {
1432     char *const newaddr = (char*)PerlMemShared_malloc(len + 1);
1433 
1434     PERL_UNUSED_CONTEXT;
1435     /* PERL_ARGS_ASSERT_SAVESHAREDPVN; */
1436 
1437     if (!newaddr) {
1438         croak_no_mem();
1439     }
1440     newaddr[len] = '\0';
1441     return (char*)memcpy(newaddr, pv, len);
1442 }
1443 
1444 /*
1445 =for apidoc savesvpv
1446 
1447 A version of C<savepv()>/C<savepvn()> which gets the string to duplicate from
1448 the passed in SV using C<SvPV()>
1449 
1450 On some platforms, Windows for example, all allocated memory owned by a thread
1451 is deallocated when that thread ends.  So if you need that not to happen, you
1452 need to use the shared memory functions, such as C<L</savesharedsvpv>>.
1453 
1454 =cut
1455 */
1456 
1457 char *
1458 Perl_savesvpv(pTHX_ SV *sv)
1459 {
1460     STRLEN len;
1461     const char * const pv = SvPV_const(sv, len);
1462     char *newaddr;
1463 
1464     PERL_ARGS_ASSERT_SAVESVPV;
1465 
1466     ++len;
1467     Newx(newaddr,len,char);
1468     return (char *) CopyD(pv,newaddr,len,char);
1469 }
1470 
1471 /*
1472 =for apidoc savesharedsvpv
1473 
1474 A version of C<savesharedpv()> which allocates the duplicate string in
1475 memory which is shared between threads.
1476 
1477 =cut
1478 */
1479 
1480 char *
1481 Perl_savesharedsvpv(pTHX_ SV *sv)
1482 {
1483     STRLEN len;
1484     const char * const pv = SvPV_const(sv, len);
1485 
1486     PERL_ARGS_ASSERT_SAVESHAREDSVPV;
1487 
1488     return savesharedpvn(pv, len);
1489 }
1490 
1491 /* the SV for Perl_form() and mess() is not kept in an arena */
1492 
1493 STATIC SV *
1494 S_mess_alloc(pTHX)
1495 {
1496     SV *sv;
1497     XPVMG *any;
1498 
1499     if (PL_phase != PERL_PHASE_DESTRUCT)
1500         return newSVpvs_flags("", SVs_TEMP);
1501 
1502     if (PL_mess_sv)
1503         return PL_mess_sv;
1504 
1505     /* Create as PVMG now, to avoid any upgrading later */
1506     Newx(sv, 1, SV);
1507     Newxz(any, 1, XPVMG);
1508     SvFLAGS(sv) = SVt_PVMG;
1509     SvANY(sv) = (void*)any;
1510     SvPV_set(sv, NULL);
1511     SvREFCNT(sv) = 1 << 30; /* practically infinite */
1512     PL_mess_sv = sv;
1513     return sv;
1514 }
1515 
1516 #if defined(MULTIPLICITY)
1517 char *
1518 Perl_form_nocontext(const char* pat, ...)
1519 {
1520     dTHX;
1521     char *retval;
1522     va_list args;
1523     PERL_ARGS_ASSERT_FORM_NOCONTEXT;
1524     va_start(args, pat);
1525     retval = vform(pat, &args);
1526     va_end(args);
1527     return retval;
1528 }
1529 #endif /* MULTIPLICITY */
1530 
1531 /*
1532 =for apidoc_section $display
1533 =for apidoc form
1534 =for apidoc_item form_nocontext
1535 
1536 These take a sprintf-style format pattern and conventional
1537 (non-SV) arguments and return the formatted string.
1538 
1539     (char *) Perl_form(pTHX_ const char* pat, ...)
1540 
1541 can be used any place a string (char *) is required:
1542 
1543     char * s = Perl_form("%d.%d",major,minor);
1544 
1545 They use a single (per-thread) private buffer so if you want to format several
1546 strings you must explicitly copy the earlier strings away (and free the copies
1547 when you are done).
1548 
1549 The two forms differ only in that C<form_nocontext> does not take a thread
1550 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
1551 already have the thread context.
1552 
1553 =for apidoc vform
1554 Like C<L</form>> but but the arguments are an encapsulated argument list.
1555 
1556 =cut
1557 */
1558 
1559 char *
1560 Perl_form(pTHX_ const char* pat, ...)
1561 {
1562     char *retval;
1563     va_list args;
1564     PERL_ARGS_ASSERT_FORM;
1565     va_start(args, pat);
1566     retval = vform(pat, &args);
1567     va_end(args);
1568     return retval;
1569 }
1570 
1571 char *
1572 Perl_vform(pTHX_ const char *pat, va_list *args)
1573 {
1574     SV * const sv = mess_alloc();
1575     PERL_ARGS_ASSERT_VFORM;
1576     sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
1577     return SvPVX(sv);
1578 }
1579 
1580 /*
1581 =for apidoc mess
1582 =for apidoc_item mess_nocontext
1583 
1584 These take a sprintf-style format pattern and argument list, which are used to
1585 generate a string message.  If the message does not end with a newline, then it
1586 will be extended with some indication of the current location in the code, as
1587 described for C<L</mess_sv>>.
1588 
1589 Normally, the resulting message is returned in a new mortal SV.
1590 But during global destruction a single SV may be shared between uses of
1591 this function.
1592 
1593 The two forms differ only in that C<mess_nocontext> does not take a thread
1594 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
1595 already have the thread context.
1596 
1597 =cut
1598 */
1599 
1600 #if defined(MULTIPLICITY)
1601 SV *
1602 Perl_mess_nocontext(const char *pat, ...)
1603 {
1604     dTHX;
1605     SV *retval;
1606     va_list args;
1607     PERL_ARGS_ASSERT_MESS_NOCONTEXT;
1608     va_start(args, pat);
1609     retval = vmess(pat, &args);
1610     va_end(args);
1611     return retval;
1612 }
1613 #endif /* MULTIPLICITY */
1614 
1615 SV *
1616 Perl_mess(pTHX_ const char *pat, ...)
1617 {
1618     SV *retval;
1619     va_list args;
1620     PERL_ARGS_ASSERT_MESS;
1621     va_start(args, pat);
1622     retval = vmess(pat, &args);
1623     va_end(args);
1624     return retval;
1625 }
1626 
1627 const COP*
1628 Perl_closest_cop(pTHX_ const COP *cop, const OP *o, const OP *curop,
1629                        bool opnext)
1630 {
1631     /* Look for curop starting from o.  cop is the last COP we've seen. */
1632     /* opnext means that curop is actually the ->op_next of the op we are
1633        seeking. */
1634 
1635     PERL_ARGS_ASSERT_CLOSEST_COP;
1636 
1637     if (!o || !curop || (
1638         opnext ? o->op_next == curop && o->op_type != OP_SCOPE : o == curop
1639     ))
1640         return cop;
1641 
1642     if (o->op_flags & OPf_KIDS) {
1643         const OP *kid;
1644         for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
1645             const COP *new_cop;
1646 
1647             /* If the OP_NEXTSTATE has been optimised away we can still use it
1648              * the get the file and line number. */
1649 
1650             if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE)
1651                 cop = (const COP *)kid;
1652 
1653             /* Keep searching, and return when we've found something. */
1654 
1655             new_cop = closest_cop(cop, kid, curop, opnext);
1656             if (new_cop)
1657                 return new_cop;
1658         }
1659     }
1660 
1661     /* Nothing found. */
1662 
1663     return NULL;
1664 }
1665 
1666 /*
1667 =for apidoc mess_sv
1668 
1669 Expands a message, intended for the user, to include an indication of
1670 the current location in the code, if the message does not already appear
1671 to be complete.
1672 
1673 C<basemsg> is the initial message or object.  If it is a reference, it
1674 will be used as-is and will be the result of this function.  Otherwise it
1675 is used as a string, and if it already ends with a newline, it is taken
1676 to be complete, and the result of this function will be the same string.
1677 If the message does not end with a newline, then a segment such as C<at
1678 foo.pl line 37> will be appended, and possibly other clauses indicating
1679 the current state of execution.  The resulting message will end with a
1680 dot and a newline.
1681 
1682 Normally, the resulting message is returned in a new mortal SV.
1683 During global destruction a single SV may be shared between uses of this
1684 function.  If C<consume> is true, then the function is permitted (but not
1685 required) to modify and return C<basemsg> instead of allocating a new SV.
1686 
1687 =cut
1688 */
1689 
1690 SV *
1691 Perl_mess_sv(pTHX_ SV *basemsg, bool consume)
1692 {
1693     SV *sv;
1694 
1695 #if defined(USE_C_BACKTRACE) && defined(USE_C_BACKTRACE_ON_ERROR)
1696     {
1697         char *ws;
1698         UV wi;
1699         /* The PERL_C_BACKTRACE_ON_WARN must be an integer of one or more. */
1700         if ((ws = PerlEnv_getenv("PERL_C_BACKTRACE_ON_ERROR"))
1701             && grok_atoUV(ws, &wi, NULL)
1702             && wi <= PERL_INT_MAX
1703         ) {
1704             Perl_dump_c_backtrace(aTHX_ Perl_debug_log, (int)wi, 1);
1705         }
1706     }
1707 #endif
1708 
1709     PERL_ARGS_ASSERT_MESS_SV;
1710 
1711     if (SvROK(basemsg)) {
1712         if (consume) {
1713             sv = basemsg;
1714         }
1715         else {
1716             sv = mess_alloc();
1717             sv_setsv(sv, basemsg);
1718         }
1719         return sv;
1720     }
1721 
1722     if (SvPOK(basemsg) && consume) {
1723         sv = basemsg;
1724     }
1725     else {
1726         sv = mess_alloc();
1727         sv_copypv(sv, basemsg);
1728     }
1729 
1730     if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1731         /*
1732          * Try and find the file and line for PL_op.  This will usually be
1733          * PL_curcop, but it might be a cop that has been optimised away.  We
1734          * can try to find such a cop by searching through the optree starting
1735          * from the sibling of PL_curcop.
1736          */
1737 
1738         if (PL_curcop) {
1739             const COP *cop =
1740                 closest_cop(PL_curcop, OpSIBLING(PL_curcop), PL_op, FALSE);
1741             if (!cop)
1742                 cop = PL_curcop;
1743 
1744             if (CopLINE(cop))
1745                 Perl_sv_catpvf(aTHX_ sv, " at %s line %" IVdf,
1746                                 OutCopFILE(cop), (IV)CopLINE(cop));
1747         }
1748 
1749         /* Seems that GvIO() can be untrustworthy during global destruction. */
1750         if (GvIO(PL_last_in_gv) && (SvTYPE(GvIOp(PL_last_in_gv)) == SVt_PVIO)
1751                 && IoLINES(GvIOp(PL_last_in_gv)))
1752         {
1753             STRLEN l;
1754             const bool line_mode = (RsSIMPLE(PL_rs) &&
1755                                    *SvPV_const(PL_rs,l) == '\n' && l == 1);
1756             Perl_sv_catpvf(aTHX_ sv, ", <%" SVf "> %s %" IVdf,
1757                            SVfARG(PL_last_in_gv == PL_argvgv
1758                                  ? &PL_sv_no
1759                                  : sv_2mortal(newSVhek(GvNAME_HEK(PL_last_in_gv)))),
1760                            line_mode ? "line" : "chunk",
1761                            (IV)IoLINES(GvIOp(PL_last_in_gv)));
1762         }
1763         if (PL_phase == PERL_PHASE_DESTRUCT)
1764             sv_catpvs(sv, " during global destruction");
1765         sv_catpvs(sv, ".\n");
1766     }
1767     return sv;
1768 }
1769 
1770 /*
1771 =for apidoc vmess
1772 
1773 C<pat> and C<args> are a sprintf-style format pattern and encapsulated
1774 argument list, respectively.  These are used to generate a string message.  If
1775 the
1776 message does not end with a newline, then it will be extended with
1777 some indication of the current location in the code, as described for
1778 L</mess_sv>.
1779 
1780 Normally, the resulting message is returned in a new mortal SV.
1781 During global destruction a single SV may be shared between uses of
1782 this function.
1783 
1784 =cut
1785 */
1786 
1787 SV *
1788 Perl_vmess(pTHX_ const char *pat, va_list *args)
1789 {
1790     SV * const sv = mess_alloc();
1791 
1792     PERL_ARGS_ASSERT_VMESS;
1793 
1794     sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
1795     return mess_sv(sv, 1);
1796 }
1797 
1798 void
1799 Perl_write_to_stderr(pTHX_ SV* msv)
1800 {
1801     IO *io;
1802     MAGIC *mg;
1803 
1804     PERL_ARGS_ASSERT_WRITE_TO_STDERR;
1805 
1806     if (PL_stderrgv && SvREFCNT(PL_stderrgv)
1807         && (io = GvIO(PL_stderrgv))
1808         && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
1809         Perl_magic_methcall(aTHX_ MUTABLE_SV(io), mg, SV_CONST(PRINT),
1810                             G_SCALAR | G_DISCARD | G_WRITING_TO_STDERR, 1, msv);
1811     else {
1812         PerlIO * const serr = Perl_error_log;
1813 
1814         do_print(msv, serr);
1815         (void)PerlIO_flush(serr);
1816     }
1817 }
1818 
1819 /*
1820 =for apidoc_section $warning
1821 */
1822 
1823 /* Common code used in dieing and warning */
1824 
1825 STATIC SV *
1826 S_with_queued_errors(pTHX_ SV *ex)
1827 {
1828     PERL_ARGS_ASSERT_WITH_QUEUED_ERRORS;
1829     if (PL_errors && SvCUR(PL_errors) && !SvROK(ex)) {
1830         sv_catsv(PL_errors, ex);
1831         ex = sv_mortalcopy(PL_errors);
1832         SvCUR_set(PL_errors, 0);
1833     }
1834     return ex;
1835 }
1836 
1837 STATIC bool
1838 S_invoke_exception_hook(pTHX_ SV *ex, bool warn)
1839 {
1840     HV *stash;
1841     GV *gv;
1842     CV *cv;
1843     SV **const hook = warn ? &PL_warnhook : &PL_diehook;
1844     /* sv_2cv might call Perl_croak() or Perl_warner() */
1845     SV * const oldhook = *hook;
1846 
1847     if (!oldhook || oldhook == PERL_WARNHOOK_FATAL)
1848         return FALSE;
1849 
1850     ENTER;
1851     SAVESPTR(*hook);
1852     *hook = NULL;
1853     cv = sv_2cv(oldhook, &stash, &gv, 0);
1854     LEAVE;
1855     if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1856         dSP;
1857         SV *exarg;
1858 
1859         ENTER;
1860         save_re_context();
1861         if (warn) {
1862             SAVESPTR(*hook);
1863             *hook = NULL;
1864         }
1865         exarg = newSVsv(ex);
1866         SvREADONLY_on(exarg);
1867         SAVEFREESV(exarg);
1868 
1869         PUSHSTACKi(warn ? PERLSI_WARNHOOK : PERLSI_DIEHOOK);
1870         PUSHMARK(SP);
1871         XPUSHs(exarg);
1872         PUTBACK;
1873         call_sv(MUTABLE_SV(cv), G_DISCARD);
1874         POPSTACK;
1875         LEAVE;
1876         return TRUE;
1877     }
1878     return FALSE;
1879 }
1880 
1881 /*
1882 =for apidoc die_sv
1883 
1884 This behaves the same as L</croak_sv>, except for the return type.
1885 It should be used only where the C<OP *> return type is required.
1886 The function never actually returns.
1887 
1888 =cut
1889 */
1890 
1891 /* silence __declspec(noreturn) warnings */
1892 MSVC_DIAG_IGNORE(4646 4645)
1893 OP *
1894 Perl_die_sv(pTHX_ SV *baseex)
1895 {
1896     PERL_ARGS_ASSERT_DIE_SV;
1897     croak_sv(baseex);
1898     /* NOTREACHED */
1899     NORETURN_FUNCTION_END;
1900 }
1901 MSVC_DIAG_RESTORE
1902 
1903 /*
1904 =for apidoc      die
1905 =for apidoc_item die_nocontext
1906 
1907 These behave the same as L</croak>, except for the return type.
1908 They should be used only where the C<OP *> return type is required.
1909 They never actually return.
1910 
1911 The two forms differ only in that C<die_nocontext> does not take a thread
1912 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
1913 already have the thread context.
1914 
1915 =cut
1916 */
1917 
1918 #if defined(MULTIPLICITY)
1919 
1920 /* silence __declspec(noreturn) warnings */
1921 MSVC_DIAG_IGNORE(4646 4645)
1922 OP *
1923 Perl_die_nocontext(const char* pat, ...)
1924 {
1925     dTHX;
1926     va_list args;
1927     va_start(args, pat);
1928     vcroak(pat, &args);
1929     NOT_REACHED; /* NOTREACHED */
1930     va_end(args);
1931     NORETURN_FUNCTION_END;
1932 }
1933 MSVC_DIAG_RESTORE
1934 
1935 #endif /* MULTIPLICITY */
1936 
1937 /* silence __declspec(noreturn) warnings */
1938 MSVC_DIAG_IGNORE(4646 4645)
1939 OP *
1940 Perl_die(pTHX_ const char* pat, ...)
1941 {
1942     va_list args;
1943     va_start(args, pat);
1944     vcroak(pat, &args);
1945     NOT_REACHED; /* NOTREACHED */
1946     va_end(args);
1947     NORETURN_FUNCTION_END;
1948 }
1949 MSVC_DIAG_RESTORE
1950 
1951 /*
1952 =for apidoc croak_sv
1953 
1954 This is an XS interface to Perl's C<die> function.
1955 
1956 C<baseex> is the error message or object.  If it is a reference, it
1957 will be used as-is.  Otherwise it is used as a string, and if it does
1958 not end with a newline then it will be extended with some indication of
1959 the current location in the code, as described for L</mess_sv>.
1960 
1961 The error message or object will be used as an exception, by default
1962 returning control to the nearest enclosing C<eval>, but subject to
1963 modification by a C<$SIG{__DIE__}> handler.  In any case, the C<croak_sv>
1964 function never returns normally.
1965 
1966 To die with a simple string message, the L</croak> function may be
1967 more convenient.
1968 
1969 =cut
1970 */
1971 
1972 void
1973 Perl_croak_sv(pTHX_ SV *baseex)
1974 {
1975     SV *ex = with_queued_errors(mess_sv(baseex, 0));
1976     PERL_ARGS_ASSERT_CROAK_SV;
1977     invoke_exception_hook(ex, FALSE);
1978     die_unwind(ex);
1979 }
1980 
1981 /*
1982 =for apidoc vcroak
1983 
1984 This is an XS interface to Perl's C<die> function.
1985 
1986 C<pat> and C<args> are a sprintf-style format pattern and encapsulated
1987 argument list.  These are used to generate a string message.  If the
1988 message does not end with a newline, then it will be extended with
1989 some indication of the current location in the code, as described for
1990 L</mess_sv>.
1991 
1992 The error message will be used as an exception, by default
1993 returning control to the nearest enclosing C<eval>, but subject to
1994 modification by a C<$SIG{__DIE__}> handler.  In any case, the C<croak>
1995 function never returns normally.
1996 
1997 For historical reasons, if C<pat> is null then the contents of C<ERRSV>
1998 (C<$@>) will be used as an error message or object instead of building an
1999 error message from arguments.  If you want to throw a non-string object,
2000 or build an error message in an SV yourself, it is preferable to use
2001 the L</croak_sv> function, which does not involve clobbering C<ERRSV>.
2002 
2003 =cut
2004 */
2005 
2006 void
2007 Perl_vcroak(pTHX_ const char* pat, va_list *args)
2008 {
2009     SV *ex = with_queued_errors(pat ? vmess(pat, args) : mess_sv(ERRSV, 0));
2010     invoke_exception_hook(ex, FALSE);
2011     die_unwind(ex);
2012 }
2013 
2014 /*
2015 =for apidoc croak
2016 =for apidoc_item croak_nocontext
2017 
2018 These are XS interfaces to Perl's C<die> function.
2019 
2020 They take a sprintf-style format pattern and argument list, which are used to
2021 generate a string message.  If the message does not end with a newline, then it
2022 will be extended with some indication of the current location in the code, as
2023 described for C<L</mess_sv>>.
2024 
2025 The error message will be used as an exception, by default
2026 returning control to the nearest enclosing C<eval>, but subject to
2027 modification by a C<$SIG{__DIE__}> handler.  In any case, these croak
2028 functions never return normally.
2029 
2030 For historical reasons, if C<pat> is null then the contents of C<ERRSV>
2031 (C<$@>) will be used as an error message or object instead of building an
2032 error message from arguments.  If you want to throw a non-string object,
2033 or build an error message in an SV yourself, it is preferable to use
2034 the C<L</croak_sv>> function, which does not involve clobbering C<ERRSV>.
2035 
2036 The two forms differ only in that C<croak_nocontext> does not take a thread
2037 context (C<aTHX>) parameter.  It is usually preferred as it takes up fewer
2038 bytes of code than plain C<Perl_croak>, and time is rarely a critical resource
2039 when you are about to throw an exception.
2040 
2041 =cut
2042 */
2043 
2044 #if defined(MULTIPLICITY)
2045 void
2046 Perl_croak_nocontext(const char *pat, ...)
2047 {
2048     dTHX;
2049     va_list args;
2050     va_start(args, pat);
2051     vcroak(pat, &args);
2052     NOT_REACHED; /* NOTREACHED */
2053     va_end(args);
2054 }
2055 #endif /* MULTIPLICITY */
2056 
2057 /* saves machine code for a common noreturn idiom typically used in Newx*() */
2058 GCC_DIAG_IGNORE_DECL(-Wunused-function);
2059 void
2060 Perl_croak_memory_wrap(void)
2061 {
2062     Perl_croak_nocontext("%s",PL_memory_wrap);
2063 }
2064 GCC_DIAG_RESTORE_DECL;
2065 
2066 void
2067 Perl_croak(pTHX_ const char *pat, ...)
2068 {
2069     va_list args;
2070     va_start(args, pat);
2071     vcroak(pat, &args);
2072     NOT_REACHED; /* NOTREACHED */
2073     va_end(args);
2074 }
2075 
2076 /*
2077 =for apidoc croak_no_modify
2078 
2079 This encapsulates a common reason for dying, generating terser object code than
2080 using the generic C<Perl_croak>.  It is exactly equivalent to
2081 C<Perl_croak(aTHX_ "%s", PL_no_modify)> (which expands to something like
2082 "Modification of a read-only value attempted").
2083 
2084 Less code used on exception code paths reduces CPU cache pressure.
2085 
2086 =cut
2087 */
2088 
2089 void
2090 Perl_croak_no_modify(void)
2091 {
2092     Perl_croak_nocontext( "%s", PL_no_modify);
2093 }
2094 
2095 /* does not return, used in util.c perlio.c and win32.c
2096    This is typically called when malloc returns NULL.
2097 */
2098 void
2099 Perl_croak_no_mem(void)
2100 {
2101     dTHX;
2102 
2103     int fd = PerlIO_fileno(Perl_error_log);
2104     if (fd < 0)
2105         SETERRNO(EBADF,RMS_IFI);
2106     else {
2107         /* Can't use PerlIO to write as it allocates memory */
2108         PERL_UNUSED_RESULT(PerlLIO_write(fd, PL_no_mem, sizeof(PL_no_mem)-1));
2109     }
2110     my_exit(1);
2111 }
2112 
2113 /* does not return, used only in POPSTACK */
2114 void
2115 Perl_croak_popstack(void)
2116 {
2117     dTHX;
2118     PerlIO_printf(Perl_error_log, "panic: POPSTACK\n");
2119     my_exit(1);
2120 }
2121 
2122 /*
2123 =for apidoc warn_sv
2124 
2125 This is an XS interface to Perl's C<warn> function.
2126 
2127 C<baseex> is the error message or object.  If it is a reference, it
2128 will be used as-is.  Otherwise it is used as a string, and if it does
2129 not end with a newline then it will be extended with some indication of
2130 the current location in the code, as described for L</mess_sv>.
2131 
2132 The error message or object will by default be written to standard error,
2133 but this is subject to modification by a C<$SIG{__WARN__}> handler.
2134 
2135 To warn with a simple string message, the L</warn> function may be
2136 more convenient.
2137 
2138 =cut
2139 */
2140 
2141 void
2142 Perl_warn_sv(pTHX_ SV *baseex)
2143 {
2144     SV *ex = mess_sv(baseex, 0);
2145     PERL_ARGS_ASSERT_WARN_SV;
2146     if (!invoke_exception_hook(ex, TRUE))
2147         write_to_stderr(ex);
2148 }
2149 
2150 /*
2151 =for apidoc vwarn
2152 
2153 This is an XS interface to Perl's C<warn> function.
2154 
2155 This is like C<L</warn>>, but C<args> are an encapsulated
2156 argument list.
2157 
2158 Unlike with L</vcroak>, C<pat> is not permitted to be null.
2159 
2160 =cut
2161 */
2162 
2163 void
2164 Perl_vwarn(pTHX_ const char* pat, va_list *args)
2165 {
2166     SV *ex = vmess(pat, args);
2167     PERL_ARGS_ASSERT_VWARN;
2168     if (!invoke_exception_hook(ex, TRUE))
2169         write_to_stderr(ex);
2170 }
2171 
2172 /*
2173 =for apidoc warn
2174 =for apidoc_item warn_nocontext
2175 
2176 These are XS interfaces to Perl's C<warn> function.
2177 
2178 They take a sprintf-style format pattern and argument list, which  are used to
2179 generate a string message.  If the message does not end with a newline, then it
2180 will be extended with some indication of the current location in the code, as
2181 described for C<L</mess_sv>>.
2182 
2183 The error message or object will by default be written to standard error,
2184 but this is subject to modification by a C<$SIG{__WARN__}> handler.
2185 
2186 Unlike with C<L</croak>>, C<pat> is not permitted to be null.
2187 
2188 The two forms differ only in that C<warn_nocontext> does not take a thread
2189 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
2190 already have the thread context.
2191 
2192 =cut
2193 */
2194 
2195 #if defined(MULTIPLICITY)
2196 void
2197 Perl_warn_nocontext(const char *pat, ...)
2198 {
2199     dTHX;
2200     va_list args;
2201     PERL_ARGS_ASSERT_WARN_NOCONTEXT;
2202     va_start(args, pat);
2203     vwarn(pat, &args);
2204     va_end(args);
2205 }
2206 #endif /* MULTIPLICITY */
2207 
2208 void
2209 Perl_warn(pTHX_ const char *pat, ...)
2210 {
2211     va_list args;
2212     PERL_ARGS_ASSERT_WARN;
2213     va_start(args, pat);
2214     vwarn(pat, &args);
2215     va_end(args);
2216 }
2217 
2218 /*
2219 =for apidoc warner
2220 =for apidoc_item warner_nocontext
2221 
2222 These output a warning of the specified category (or categories) given by
2223 C<err>, using the sprintf-style format pattern C<pat>, and argument list.
2224 
2225 C<err> must be one of the C<L</packWARN>>, C<packWARN2>, C<packWARN3>,
2226 C<packWARN4> macros populated with the appropriate number of warning
2227 categories.  If any of the warning categories they specify is fatal, a fatal
2228 exception is thrown.
2229 
2230 In any event a message is generated by the pattern and arguments.  If the
2231 message does not end with a newline, then it will be extended with some
2232 indication of the current location in the code, as described for L</mess_sv>.
2233 
2234 The error message or object will by default be written to standard error,
2235 but this is subject to modification by a C<$SIG{__WARN__}> handler.
2236 
2237 C<pat> is not permitted to be null.
2238 
2239 The two forms differ only in that C<warner_nocontext> does not take a thread
2240 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
2241 already have the thread context.
2242 
2243 These functions differ from the similarly named C<L</warn>> functions, in that
2244 the latter are for XS code to unconditionally display a warning, whereas these
2245 are for code that may be compiling a perl program, and does extra checking to
2246 see if the warning should be fatal.
2247 
2248 =for apidoc ck_warner
2249 =for apidoc_item ck_warner_d
2250 If none of the warning categories given by C<err> are enabled, do nothing;
2251 otherwise call C<L</warner>>  or C<L</warner_nocontext>> with the passed-in
2252 parameters;.
2253 
2254 C<err> must be one of the C<L</packWARN>>, C<packWARN2>, C<packWARN3>,
2255 C<packWARN4> macros populated with the appropriate number of warning
2256 categories.
2257 
2258 The two forms differ only in that C<ck_warner_d> should be used if warnings for
2259 any of the categories are by default enabled.
2260 
2261 =for apidoc vwarner
2262 This is like C<L</warner>>, but C<args> are an encapsulated argument list.
2263 
2264 =cut
2265 */
2266 
2267 #if defined(MULTIPLICITY)
2268 void
2269 Perl_warner_nocontext(U32 err, const char *pat, ...)
2270 {
2271     dTHX;
2272     va_list args;
2273     PERL_ARGS_ASSERT_WARNER_NOCONTEXT;
2274     va_start(args, pat);
2275     vwarner(err, pat, &args);
2276     va_end(args);
2277 }
2278 #endif /* MULTIPLICITY */
2279 
2280 void
2281 Perl_ck_warner_d(pTHX_ U32 err, const char* pat, ...)
2282 {
2283     PERL_ARGS_ASSERT_CK_WARNER_D;
2284 
2285     if (Perl_ckwarn_d(aTHX_ err)) {
2286         va_list args;
2287         va_start(args, pat);
2288         vwarner(err, pat, &args);
2289         va_end(args);
2290     }
2291 }
2292 
2293 void
2294 Perl_ck_warner(pTHX_ U32 err, const char* pat, ...)
2295 {
2296     PERL_ARGS_ASSERT_CK_WARNER;
2297 
2298     if (Perl_ckwarn(aTHX_ err)) {
2299         va_list args;
2300         va_start(args, pat);
2301         vwarner(err, pat, &args);
2302         va_end(args);
2303     }
2304 }
2305 
2306 void
2307 Perl_warner(pTHX_ U32  err, const char* pat,...)
2308 {
2309     va_list args;
2310     PERL_ARGS_ASSERT_WARNER;
2311     va_start(args, pat);
2312     vwarner(err, pat, &args);
2313     va_end(args);
2314 }
2315 
2316 void
2317 Perl_vwarner(pTHX_ U32  err, const char* pat, va_list* args)
2318 {
2319     PERL_ARGS_ASSERT_VWARNER;
2320     if (
2321         (PL_warnhook == PERL_WARNHOOK_FATAL || ckDEAD(err)) &&
2322         !(PL_in_eval & EVAL_KEEPERR)
2323     ) {
2324         SV * const msv = vmess(pat, args);
2325 
2326         if (PL_parser && PL_parser->error_count) {
2327             qerror(msv);
2328         }
2329         else {
2330             invoke_exception_hook(msv, FALSE);
2331             die_unwind(msv);
2332         }
2333     }
2334     else {
2335         Perl_vwarn(aTHX_ pat, args);
2336     }
2337 }
2338 
2339 /* implements the ckWARN? macros */
2340 
2341 bool
2342 Perl_ckwarn(pTHX_ U32 w)
2343 {
2344     /* If lexical warnings have not been set, use $^W.  */
2345     if (isLEXWARN_off)
2346         return PL_dowarn & G_WARN_ON;
2347 
2348     return ckwarn_common(w);
2349 }
2350 
2351 /* implements the ckWARN?_d macro */
2352 
2353 bool
2354 Perl_ckwarn_d(pTHX_ U32 w)
2355 {
2356     /* If lexical warnings have not been set then default classes warn.  */
2357     if (isLEXWARN_off)
2358         return TRUE;
2359 
2360     return ckwarn_common(w);
2361 }
2362 
2363 static bool
2364 S_ckwarn_common(pTHX_ U32 w)
2365 {
2366     if (PL_curcop->cop_warnings == pWARN_ALL)
2367         return TRUE;
2368 
2369     if (PL_curcop->cop_warnings == pWARN_NONE)
2370         return FALSE;
2371 
2372     /* Check the assumption that at least the first slot is non-zero.  */
2373     assert(unpackWARN1(w));
2374 
2375     /* Check the assumption that it is valid to stop as soon as a zero slot is
2376        seen.  */
2377     if (!unpackWARN2(w)) {
2378         assert(!unpackWARN3(w));
2379         assert(!unpackWARN4(w));
2380     } else if (!unpackWARN3(w)) {
2381         assert(!unpackWARN4(w));
2382     }
2383 
2384     /* Right, dealt with all the special cases, which are implemented as non-
2385        pointers, so there is a pointer to a real warnings mask.  */
2386     do {
2387         if (isWARN_on(PL_curcop->cop_warnings, unpackWARN1(w)))
2388             return TRUE;
2389     } while (w >>= WARNshift);
2390 
2391     return FALSE;
2392 }
2393 
2394 /* Set buffer=NULL to get a new one.  */
2395 STRLEN *
2396 Perl_new_warnings_bitfield(pTHX_ STRLEN *buffer, const char *const bits,
2397                            STRLEN size) {
2398     const MEM_SIZE len_wanted =
2399         sizeof(STRLEN) + (size > WARNsize ? size : WARNsize);
2400     PERL_UNUSED_CONTEXT;
2401     PERL_ARGS_ASSERT_NEW_WARNINGS_BITFIELD;
2402 
2403     buffer = (STRLEN*)
2404         (specialWARN(buffer) ?
2405          PerlMemShared_malloc(len_wanted) :
2406          PerlMemShared_realloc(buffer, len_wanted));
2407     buffer[0] = size;
2408     Copy(bits, (buffer + 1), size, char);
2409     if (size < WARNsize)
2410         Zero((char *)(buffer + 1) + size, WARNsize - size, char);
2411     return buffer;
2412 }
2413 
2414 /* since we've already done strlen() for both nam and val
2415  * we can use that info to make things faster than
2416  * sprintf(s, "%s=%s", nam, val)
2417  */
2418 #define my_setenv_format(s, nam, nlen, val, vlen) \
2419    Copy(nam, s, nlen, char); \
2420    *(s+nlen) = '='; \
2421    Copy(val, s+(nlen+1), vlen, char); \
2422    *(s+(nlen+1+vlen)) = '\0'
2423 
2424 
2425 
2426 #ifdef USE_ENVIRON_ARRAY
2427 /* NB: VMS' my_setenv() is in vms.c */
2428 
2429 /* Configure doesn't test for HAS_SETENV yet, so decide based on platform.
2430  * For Solaris, setenv() and unsetenv() were introduced in Solaris 9, so
2431  * testing for HAS UNSETENV is sufficient.
2432  */
2433 #  if defined(__CYGWIN__)|| defined(__riscos__) || (defined(__sun) && defined(HAS_UNSETENV)) || defined(PERL_DARWIN)
2434 #    define MY_HAS_SETENV
2435 #  endif
2436 
2437 /* small wrapper for use by Perl_my_setenv that mallocs, or reallocs if
2438  * 'current' is non-null, with up to three sizes that are added together.
2439  * It handles integer overflow.
2440  */
2441 #  ifndef MY_HAS_SETENV
2442 static char *
2443 S_env_alloc(void *current, Size_t l1, Size_t l2, Size_t l3, Size_t size)
2444 {
2445     void *p;
2446     Size_t sl, l = l1 + l2;
2447 
2448     if (l < l2)
2449         goto panic;
2450     l += l3;
2451     if (l < l3)
2452         goto panic;
2453     sl = l * size;
2454     if (sl < l)
2455         goto panic;
2456 
2457     p = current
2458             ? safesysrealloc(current, sl)
2459             : safesysmalloc(sl);
2460     if (p)
2461         return (char*)p;
2462 
2463   panic:
2464     croak_memory_wrap();
2465 }
2466 #  endif
2467 
2468 
2469 #  if !defined(WIN32)
2470 
2471 /*
2472 =for apidoc_section $utility
2473 =for apidoc my_setenv
2474 
2475 A wrapper for the C library L<setenv(3)>.  Don't use the latter, as the perl
2476 version has desirable safeguards
2477 
2478 =cut
2479 */
2480 
2481 void
2482 Perl_my_setenv(pTHX_ const char *nam, const char *val)
2483 {
2484 #    ifdef __amigaos4__
2485   amigaos4_obtain_environ(__FUNCTION__);
2486 #    endif
2487 
2488 #    ifdef USE_ITHREADS
2489   /* only parent thread can modify process environment, so no need to use a
2490    * mutex */
2491   if (PL_curinterp == aTHX)
2492 #    endif
2493   {
2494 
2495 #    ifndef PERL_USE_SAFE_PUTENV
2496     if (!PL_use_safe_putenv) {
2497         /* most putenv()s leak, so we manipulate environ directly */
2498         UV i;
2499         Size_t vlen, nlen = strlen(nam);
2500 
2501         /* where does it go? */
2502         for (i = 0; environ[i]; i++) {
2503             if (strnEQ(environ[i], nam, nlen) && environ[i][nlen] == '=')
2504                 break;
2505         }
2506 
2507         if (environ == PL_origenviron) {   /* need we copy environment? */
2508             UV j, max;
2509             char **tmpenv;
2510 
2511             max = i;
2512             while (environ[max])
2513                 max++;
2514 
2515             /* XXX shouldn't that be max+1 rather than max+2 ??? - DAPM */
2516             tmpenv = (char**)S_env_alloc(NULL, max, 2, 0, sizeof(char*));
2517 
2518             for (j=0; j<max; j++) {         /* copy environment */
2519                 const Size_t len = strlen(environ[j]);
2520                 tmpenv[j] = S_env_alloc(NULL, len, 1, 0, 1);
2521                 Copy(environ[j], tmpenv[j], len+1, char);
2522             }
2523 
2524             tmpenv[max] = NULL;
2525             environ = tmpenv;               /* tell exec where it is now */
2526         }
2527 
2528         if (!val) {
2529             safesysfree(environ[i]);
2530             while (environ[i]) {
2531                 environ[i] = environ[i+1];
2532                 i++;
2533             }
2534 #      ifdef __amigaos4__
2535             goto my_setenv_out;
2536 #      else
2537             return;
2538 #      endif
2539         }
2540 
2541         if (!environ[i]) {                 /* does not exist yet */
2542             environ = (char**)S_env_alloc(environ, i, 2, 0, sizeof(char*));
2543             environ[i+1] = NULL;    /* make sure it's null terminated */
2544         }
2545         else
2546             safesysfree(environ[i]);
2547 
2548         vlen = strlen(val);
2549 
2550         environ[i] = S_env_alloc(NULL, nlen, vlen, 2, 1);
2551         /* all that work just for this */
2552         my_setenv_format(environ[i], nam, nlen, val, vlen);
2553     }
2554     else {
2555 
2556 #    endif /* !PERL_USE_SAFE_PUTENV */
2557 
2558 #    ifdef MY_HAS_SETENV
2559 #      if defined(HAS_UNSETENV)
2560         if (val == NULL) {
2561             (void)unsetenv(nam);
2562         } else {
2563             (void)setenv(nam, val, 1);
2564         }
2565 #      else /* ! HAS_UNSETENV */
2566         (void)setenv(nam, val, 1);
2567 #      endif /* HAS_UNSETENV */
2568 
2569 #    elif defined(HAS_UNSETENV)
2570 
2571         if (val == NULL) {
2572             if (environ) /* old glibc can crash with null environ */
2573                 (void)unsetenv(nam);
2574         } else {
2575             const Size_t nlen = strlen(nam);
2576             const Size_t vlen = strlen(val);
2577             char * const new_env = S_env_alloc(NULL, nlen, vlen, 2, 1);
2578             my_setenv_format(new_env, nam, nlen, val, vlen);
2579             (void)putenv(new_env);
2580         }
2581 
2582 #    else /* ! HAS_UNSETENV */
2583 
2584         char *new_env;
2585         const Size_t nlen = strlen(nam);
2586         Size_t vlen;
2587         if (!val) {
2588            val = "";
2589         }
2590         vlen = strlen(val);
2591         new_env = S_env_alloc(NULL, nlen, vlen, 2, 1);
2592         /* all that work just for this */
2593         my_setenv_format(new_env, nam, nlen, val, vlen);
2594         (void)putenv(new_env);
2595 
2596 #    endif /* MY_HAS_SETENV */
2597 
2598 #    ifndef PERL_USE_SAFE_PUTENV
2599     }
2600 #    endif
2601   }
2602 
2603 #    ifdef __amigaos4__
2604 my_setenv_out:
2605   amigaos4_release_environ(__FUNCTION__);
2606 #    endif
2607 }
2608 
2609 #  else /* WIN32 */
2610 
2611 void
2612 Perl_my_setenv(pTHX_ const char *nam, const char *val)
2613 {
2614     char *envstr;
2615     const Size_t nlen = strlen(nam);
2616     Size_t vlen;
2617 
2618     if (!val) {
2619        val = "";
2620     }
2621     vlen = strlen(val);
2622     envstr = S_env_alloc(NULL, nlen, vlen, 2, 1);
2623     my_setenv_format(envstr, nam, nlen, val, vlen);
2624     (void)PerlEnv_putenv(envstr);
2625     safesysfree(envstr);
2626 }
2627 
2628 #  endif /* WIN32 */
2629 
2630 #endif /* USE_ENVIRON_ARRAY */
2631 
2632 
2633 
2634 
2635 #ifdef UNLINK_ALL_VERSIONS
2636 I32
2637 Perl_unlnk(pTHX_ const char *f)	/* unlink all versions of a file */
2638 {
2639     I32 retries = 0;
2640 
2641     PERL_ARGS_ASSERT_UNLNK;
2642 
2643     while (PerlLIO_unlink(f) >= 0)
2644         retries++;
2645     return retries ? 0 : -1;
2646 }
2647 #endif
2648 
2649 #if defined(OEMVS)
2650   #if (__CHARSET_LIB == 1)
2651   static int chgfdccsid(int fd, unsigned short ccsid)
2652   {
2653     attrib_t attr;
2654     memset(&attr, 0, sizeof(attr));
2655     attr.att_filetagchg = 1;
2656     attr.att_filetag.ft_ccsid = ccsid;
2657     if (ccsid != FT_BINARY) {
2658       attr.att_filetag.ft_txtflag = 1;
2659     }
2660     return __fchattr(fd, &attr, sizeof(attr));
2661   }
2662   #endif
2663 #endif
2664 
2665 /*
2666 =for apidoc my_popen_list
2667 
2668 Implementing function on some systems for PerlProc_popen_list()
2669 
2670 =cut
2671 */
2672 
2673 PerlIO *
2674 Perl_my_popen_list(pTHX_ const char *mode, int n, SV **args)
2675 {
2676 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(OS2) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
2677     int p[2];
2678     I32 This, that;
2679     Pid_t pid;
2680     SV *sv;
2681     I32 did_pipes = 0;
2682     int pp[2];
2683 
2684     PERL_ARGS_ASSERT_MY_POPEN_LIST;
2685 
2686     PERL_FLUSHALL_FOR_CHILD;
2687     This = (*mode == 'w');
2688     that = !This;
2689     if (TAINTING_get) {
2690         taint_env();
2691         taint_proper("Insecure %s%s", "EXEC");
2692     }
2693     if (PerlProc_pipe_cloexec(p) < 0)
2694         return NULL;
2695     /* Try for another pipe pair for error return */
2696     if (PerlProc_pipe_cloexec(pp) >= 0)
2697         did_pipes = 1;
2698     while ((pid = PerlProc_fork()) < 0) {
2699         if (errno != EAGAIN) {
2700             PerlLIO_close(p[This]);
2701             PerlLIO_close(p[that]);
2702             if (did_pipes) {
2703                 PerlLIO_close(pp[0]);
2704                 PerlLIO_close(pp[1]);
2705             }
2706             return NULL;
2707         }
2708         Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds");
2709         sleep(5);
2710     }
2711     if (pid == 0) {
2712         /* Child */
2713 #undef THIS
2714 #undef THAT
2715 #define THIS that
2716 #define THAT This
2717         /* Close parent's end of error status pipe (if any) */
2718         if (did_pipes)
2719             PerlLIO_close(pp[0]);
2720 #if defined(OEMVS)
2721   #if (__CHARSET_LIB == 1)
2722         chgfdccsid(p[THIS], 819);
2723         chgfdccsid(p[THAT], 819);
2724   #endif
2725 #endif
2726         /* Now dup our end of _the_ pipe to right position */
2727         if (p[THIS] != (*mode == 'r')) {
2728             PerlLIO_dup2(p[THIS], *mode == 'r');
2729             PerlLIO_close(p[THIS]);
2730             if (p[THAT] != (*mode == 'r'))	/* if dup2() didn't close it */
2731                 PerlLIO_close(p[THAT]);	/* close parent's end of _the_ pipe */
2732         }
2733         else {
2734             setfd_cloexec_or_inhexec_by_sysfdness(p[THIS]);
2735             PerlLIO_close(p[THAT]);	/* close parent's end of _the_ pipe */
2736         }
2737 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
2738         /* No automatic close - do it by hand */
2739 #  ifndef NOFILE
2740 #  define NOFILE 20
2741 #  endif
2742         {
2743             int fd;
2744 
2745             for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) {
2746                 if (fd != pp[1])
2747                     PerlLIO_close(fd);
2748             }
2749         }
2750 #endif
2751         do_aexec5(NULL, args-1, args-1+n, pp[1], did_pipes);
2752         PerlProc__exit(1);
2753 #undef THIS
2754 #undef THAT
2755     }
2756     /* Parent */
2757     if (did_pipes)
2758         PerlLIO_close(pp[1]);
2759     /* Keep the lower of the two fd numbers */
2760     if (p[that] < p[This]) {
2761         PerlLIO_dup2_cloexec(p[This], p[that]);
2762         PerlLIO_close(p[This]);
2763         p[This] = p[that];
2764     }
2765     else
2766         PerlLIO_close(p[that]);		/* close child's end of pipe */
2767 
2768     sv = *av_fetch(PL_fdpid,p[This],TRUE);
2769     SvUPGRADE(sv,SVt_IV);
2770     SvIV_set(sv, pid);
2771     PL_forkprocess = pid;
2772     /* If we managed to get status pipe check for exec fail */
2773     if (did_pipes && pid > 0) {
2774         int errkid;
2775         unsigned read_total = 0;
2776 
2777         while (read_total < sizeof(int)) {
2778             const SSize_t n1 = PerlLIO_read(pp[0],
2779                               (void*)(((char*)&errkid)+read_total),
2780                               (sizeof(int)) - read_total);
2781             if (n1 <= 0)
2782                 break;
2783             read_total += n1;
2784         }
2785         PerlLIO_close(pp[0]);
2786         did_pipes = 0;
2787         if (read_total) {			/* Error */
2788             int pid2, status;
2789             PerlLIO_close(p[This]);
2790             if (read_total != sizeof(int))
2791                 Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", read_total);
2792             do {
2793                 pid2 = wait4pid(pid, &status, 0);
2794             } while (pid2 == -1 && errno == EINTR);
2795             errno = errkid;		/* Propagate errno from kid */
2796             return NULL;
2797         }
2798     }
2799     if (did_pipes)
2800          PerlLIO_close(pp[0]);
2801 #if defined(OEMVS)
2802   #if (__CHARSET_LIB == 1)
2803     PerlIO* io = PerlIO_fdopen(p[This], mode);
2804     if (io) {
2805       chgfdccsid(p[This], 819);
2806     }
2807     return io;
2808   #else
2809     return PerlIO_fdopen(p[This], mode);
2810   #endif
2811 #else
2812     return PerlIO_fdopen(p[This], mode);
2813 #endif
2814 
2815 #else
2816 #  if defined(OS2)	/* Same, without fork()ing and all extra overhead... */
2817     return my_syspopen4(aTHX_ NULL, mode, n, args);
2818 #  elif defined(WIN32)
2819     return win32_popenlist(mode, n, args);
2820 #  else
2821     Perl_croak(aTHX_ "List form of piped open not implemented");
2822     return (PerlIO *) NULL;
2823 #  endif
2824 #endif
2825 }
2826 
2827     /* VMS' my_popen() is in VMS.c, same with OS/2 and AmigaOS 4. */
2828 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
2829 
2830 /*
2831 =for apidoc_section $io
2832 =for apidoc my_popen
2833 
2834 A wrapper for the C library L<popen(3)>.  Don't use the latter, as the Perl
2835 version knows things that interact with the rest of the perl interpreter.
2836 
2837 =cut
2838 */
2839 
2840 PerlIO *
2841 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
2842 {
2843     int p[2];
2844     I32 This, that;
2845     Pid_t pid;
2846     SV *sv;
2847     const I32 doexec = !(*cmd == '-' && cmd[1] == '\0');
2848     I32 did_pipes = 0;
2849     int pp[2];
2850 
2851     PERL_ARGS_ASSERT_MY_POPEN;
2852 
2853     PERL_FLUSHALL_FOR_CHILD;
2854 #ifdef OS2
2855     if (doexec) {
2856         return my_syspopen(aTHX_ cmd,mode);
2857     }
2858 #endif
2859     This = (*mode == 'w');
2860     that = !This;
2861     if (doexec && TAINTING_get) {
2862         taint_env();
2863         taint_proper("Insecure %s%s", "EXEC");
2864     }
2865     if (PerlProc_pipe_cloexec(p) < 0)
2866         return NULL;
2867     if (doexec && PerlProc_pipe_cloexec(pp) >= 0)
2868         did_pipes = 1;
2869     while ((pid = PerlProc_fork()) < 0) {
2870         if (errno != EAGAIN) {
2871             PerlLIO_close(p[This]);
2872             PerlLIO_close(p[that]);
2873             if (did_pipes) {
2874                 PerlLIO_close(pp[0]);
2875                 PerlLIO_close(pp[1]);
2876             }
2877             if (!doexec)
2878                 Perl_croak(aTHX_ "Can't fork: %s", Strerror(errno));
2879             return NULL;
2880         }
2881         Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds");
2882         sleep(5);
2883     }
2884     if (pid == 0) {
2885 
2886 #undef THIS
2887 #undef THAT
2888 #define THIS that
2889 #define THAT This
2890         if (did_pipes)
2891             PerlLIO_close(pp[0]);
2892 #if defined(OEMVS)
2893   #if (__CHARSET_LIB == 1)
2894         chgfdccsid(p[THIS], 819);
2895         chgfdccsid(p[THAT], 819);
2896   #endif
2897 #endif
2898         if (p[THIS] != (*mode == 'r')) {
2899             PerlLIO_dup2(p[THIS], *mode == 'r');
2900             PerlLIO_close(p[THIS]);
2901             if (p[THAT] != (*mode == 'r'))	/* if dup2() didn't close it */
2902                 PerlLIO_close(p[THAT]);
2903         }
2904         else {
2905             setfd_cloexec_or_inhexec_by_sysfdness(p[THIS]);
2906             PerlLIO_close(p[THAT]);
2907         }
2908 #ifndef OS2
2909         if (doexec) {
2910 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
2911 #ifndef NOFILE
2912 #define NOFILE 20
2913 #endif
2914             {
2915                 int fd;
2916 
2917                 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++)
2918                     if (fd != pp[1])
2919                         PerlLIO_close(fd);
2920             }
2921 #endif
2922             /* may or may not use the shell */
2923             do_exec3(cmd, pp[1], did_pipes);
2924             PerlProc__exit(1);
2925         }
2926 #endif	/* defined OS2 */
2927 
2928 #ifdef PERLIO_USING_CRLF
2929    /* Since we circumvent IO layers when we manipulate low-level
2930       filedescriptors directly, need to manually switch to the
2931       default, binary, low-level mode; see PerlIOBuf_open(). */
2932    PerlLIO_setmode((*mode == 'r'), O_BINARY);
2933 #endif
2934         PL_forkprocess = 0;
2935 #ifdef PERL_USES_PL_PIDSTATUS
2936         hv_clear(PL_pidstatus);	/* we have no children */
2937 #endif
2938         return NULL;
2939 #undef THIS
2940 #undef THAT
2941     }
2942     if (did_pipes)
2943         PerlLIO_close(pp[1]);
2944     if (p[that] < p[This]) {
2945         PerlLIO_dup2_cloexec(p[This], p[that]);
2946         PerlLIO_close(p[This]);
2947         p[This] = p[that];
2948     }
2949     else
2950         PerlLIO_close(p[that]);
2951 
2952     sv = *av_fetch(PL_fdpid,p[This],TRUE);
2953     SvUPGRADE(sv,SVt_IV);
2954     SvIV_set(sv, pid);
2955     PL_forkprocess = pid;
2956     if (did_pipes && pid > 0) {
2957         int errkid;
2958         unsigned n = 0;
2959 
2960         while (n < sizeof(int)) {
2961             const SSize_t n1 = PerlLIO_read(pp[0],
2962                               (void*)(((char*)&errkid)+n),
2963                               (sizeof(int)) - n);
2964             if (n1 <= 0)
2965                 break;
2966             n += n1;
2967         }
2968         PerlLIO_close(pp[0]);
2969         did_pipes = 0;
2970         if (n) {			/* Error */
2971             int pid2, status;
2972             PerlLIO_close(p[This]);
2973             if (n != sizeof(int))
2974                 Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", n);
2975             do {
2976                 pid2 = wait4pid(pid, &status, 0);
2977             } while (pid2 == -1 && errno == EINTR);
2978             errno = errkid;		/* Propagate errno from kid */
2979             return NULL;
2980         }
2981     }
2982     if (did_pipes)
2983          PerlLIO_close(pp[0]);
2984 #if defined(OEMVS)
2985   #if (__CHARSET_LIB == 1)
2986     PerlIO* io = PerlIO_fdopen(p[This],	mode);
2987     if (io) {
2988       chgfdccsid(p[This], 819);
2989     }
2990     return io;
2991   #else
2992     return PerlIO_fdopen(p[This], mode);
2993   #endif
2994 #else
2995     return PerlIO_fdopen(p[This], mode);
2996 #endif
2997 }
2998 #elif defined(__LIBCATAMOUNT__)
2999 PerlIO *
3000 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
3001 {
3002     return NULL;
3003 }
3004 
3005 #endif /* !DOSISH */
3006 
3007 /* this is called in parent before the fork() */
3008 void
3009 Perl_atfork_lock(void)
3010 #if defined(USE_ITHREADS)
3011 #  ifdef USE_PERLIO
3012   PERL_TSA_ACQUIRE(PL_perlio_mutex)
3013 #  endif
3014 #  ifdef MYMALLOC
3015   PERL_TSA_ACQUIRE(PL_malloc_mutex)
3016 #  endif
3017   PERL_TSA_ACQUIRE(PL_op_mutex)
3018 #endif
3019 {
3020 #if defined(USE_ITHREADS)
3021     /* locks must be held in locking order (if any) */
3022 #  ifdef USE_PERLIO
3023     MUTEX_LOCK(&PL_perlio_mutex);
3024 #  endif
3025 #  ifdef MYMALLOC
3026     MUTEX_LOCK(&PL_malloc_mutex);
3027 #  endif
3028     OP_REFCNT_LOCK;
3029 #endif
3030 }
3031 
3032 /* this is called in both parent and child after the fork() */
3033 void
3034 Perl_atfork_unlock(void)
3035 #if defined(USE_ITHREADS)
3036 #  ifdef USE_PERLIO
3037   PERL_TSA_RELEASE(PL_perlio_mutex)
3038 #  endif
3039 #  ifdef MYMALLOC
3040   PERL_TSA_RELEASE(PL_malloc_mutex)
3041 #  endif
3042   PERL_TSA_RELEASE(PL_op_mutex)
3043 #endif
3044 {
3045 #if defined(USE_ITHREADS)
3046     /* locks must be released in same order as in atfork_lock() */
3047 #  ifdef USE_PERLIO
3048     MUTEX_UNLOCK(&PL_perlio_mutex);
3049 #  endif
3050 #  ifdef MYMALLOC
3051     MUTEX_UNLOCK(&PL_malloc_mutex);
3052 #  endif
3053     OP_REFCNT_UNLOCK;
3054 #endif
3055 }
3056 
3057 /*
3058 =for apidoc_section $concurrency
3059 =for apidoc my_fork
3060 
3061 This is for the use of C<PerlProc_fork> as a wrapper for the C library
3062 L<fork(2)> on some platforms to hide some platform quirks.  It should not be
3063 used except through C<PerlProc_fork>.
3064 
3065 =cut
3066 */
3067 
3068 
3069 Pid_t
3070 Perl_my_fork(void)
3071 {
3072 #if defined(HAS_FORK)
3073     Pid_t pid;
3074 #if defined(USE_ITHREADS) && !defined(HAS_PTHREAD_ATFORK)
3075     atfork_lock();
3076     pid = fork();
3077     atfork_unlock();
3078 #else
3079     /* atfork_lock() and atfork_unlock() are installed as pthread_atfork()
3080      * handlers elsewhere in the code */
3081     pid = fork();
3082 #endif
3083     return pid;
3084 #elif defined(__amigaos4__)
3085     return amigaos_fork();
3086 #else
3087     /* this "canna happen" since nothing should be calling here if !HAS_FORK */
3088     Perl_croak_nocontext("fork() not available");
3089     return 0;
3090 #endif /* HAS_FORK */
3091 }
3092 
3093 #ifndef HAS_DUP2
3094 int
3095 dup2(int oldfd, int newfd)
3096 {
3097 #if defined(HAS_FCNTL) && defined(F_DUPFD)
3098     if (oldfd == newfd)
3099         return oldfd;
3100     PerlLIO_close(newfd);
3101     return fcntl(oldfd, F_DUPFD, newfd);
3102 #else
3103 #define DUP2_MAX_FDS 256
3104     int fdtmp[DUP2_MAX_FDS];
3105     I32 fdx = 0;
3106     int fd;
3107 
3108     if (oldfd == newfd)
3109         return oldfd;
3110     PerlLIO_close(newfd);
3111     /* good enough for low fd's... */
3112     while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
3113         if (fdx >= DUP2_MAX_FDS) {
3114             PerlLIO_close(fd);
3115             fd = -1;
3116             break;
3117         }
3118         fdtmp[fdx++] = fd;
3119     }
3120     while (fdx > 0)
3121         PerlLIO_close(fdtmp[--fdx]);
3122     return fd;
3123 #endif
3124 }
3125 #endif
3126 
3127 #ifndef PERL_MICRO
3128 #ifdef HAS_SIGACTION
3129 
3130 /*
3131 =for apidoc_section $signals
3132 =for apidoc rsignal
3133 
3134 A wrapper for the C library functions L<sigaction(2)> or L<signal(2)>.
3135 Use this instead of those libc functions, as the Perl version gives the
3136 safest available implementation, and knows things that interact with the
3137 rest of the perl interpreter.
3138 
3139 =cut
3140 */
3141 
3142 Sighandler_t
3143 Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
3144 {
3145     struct sigaction act, oact;
3146 
3147 #ifdef USE_ITHREADS
3148     /* only "parent" interpreter can diddle signals */
3149     if (PL_curinterp != aTHX)
3150         return (Sighandler_t) SIG_ERR;
3151 #endif
3152 
3153     act.sa_handler = handler;
3154     sigemptyset(&act.sa_mask);
3155     act.sa_flags = 0;
3156 #ifdef SA_RESTART
3157     if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3158         act.sa_flags |= SA_RESTART;	/* SVR4, 4.3+BSD */
3159 #endif
3160 #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */
3161     if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN)
3162         act.sa_flags |= SA_NOCLDWAIT;
3163 #endif
3164     if (sigaction(signo, &act, &oact) == -1)
3165         return (Sighandler_t) SIG_ERR;
3166     else
3167         return (Sighandler_t) oact.sa_handler;
3168 }
3169 
3170 /*
3171 =for apidoc_section $signals
3172 =for apidoc rsignal_state
3173 
3174 Returns a the current signal handler for signal C<signo>.
3175 See L</C<rsignal>>.
3176 
3177 =cut
3178 */
3179 
3180 Sighandler_t
3181 Perl_rsignal_state(pTHX_ int signo)
3182 {
3183     struct sigaction oact;
3184     PERL_UNUSED_CONTEXT;
3185 
3186     if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
3187         return (Sighandler_t) SIG_ERR;
3188     else
3189         return (Sighandler_t) oact.sa_handler;
3190 }
3191 
3192 int
3193 Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
3194 {
3195     struct sigaction act;
3196 
3197     PERL_ARGS_ASSERT_RSIGNAL_SAVE;
3198 
3199 #ifdef USE_ITHREADS
3200     /* only "parent" interpreter can diddle signals */
3201     if (PL_curinterp != aTHX)
3202         return -1;
3203 #endif
3204 
3205     act.sa_handler = handler;
3206     sigemptyset(&act.sa_mask);
3207     act.sa_flags = 0;
3208 #ifdef SA_RESTART
3209     if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3210         act.sa_flags |= SA_RESTART;	/* SVR4, 4.3+BSD */
3211 #endif
3212 #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */
3213     if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN)
3214         act.sa_flags |= SA_NOCLDWAIT;
3215 #endif
3216     return sigaction(signo, &act, save);
3217 }
3218 
3219 int
3220 Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
3221 {
3222     PERL_UNUSED_CONTEXT;
3223 #ifdef USE_ITHREADS
3224     /* only "parent" interpreter can diddle signals */
3225     if (PL_curinterp != aTHX)
3226         return -1;
3227 #endif
3228 
3229     return sigaction(signo, save, (struct sigaction *)NULL);
3230 }
3231 
3232 #else /* !HAS_SIGACTION */
3233 
3234 Sighandler_t
3235 Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
3236 {
3237 #if defined(USE_ITHREADS) && !defined(WIN32)
3238     /* only "parent" interpreter can diddle signals */
3239     if (PL_curinterp != aTHX)
3240         return (Sighandler_t) SIG_ERR;
3241 #endif
3242 
3243     return PerlProc_signal(signo, handler);
3244 }
3245 
3246 static Signal_t
3247 sig_trap(int signo)
3248 {
3249     PL_sig_trapped++;
3250 }
3251 
3252 Sighandler_t
3253 Perl_rsignal_state(pTHX_ int signo)
3254 {
3255     Sighandler_t oldsig;
3256 
3257 #if defined(USE_ITHREADS) && !defined(WIN32)
3258     /* only "parent" interpreter can diddle signals */
3259     if (PL_curinterp != aTHX)
3260         return (Sighandler_t) SIG_ERR;
3261 #endif
3262 
3263     PL_sig_trapped = 0;
3264     oldsig = PerlProc_signal(signo, sig_trap);
3265     PerlProc_signal(signo, oldsig);
3266     if (PL_sig_trapped)
3267         PerlProc_kill(PerlProc_getpid(), signo);
3268     return oldsig;
3269 }
3270 
3271 int
3272 Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
3273 {
3274 #if defined(USE_ITHREADS) && !defined(WIN32)
3275     /* only "parent" interpreter can diddle signals */
3276     if (PL_curinterp != aTHX)
3277         return -1;
3278 #endif
3279     *save = PerlProc_signal(signo, handler);
3280     return (*save == (Sighandler_t) SIG_ERR) ? -1 : 0;
3281 }
3282 
3283 int
3284 Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
3285 {
3286 #if defined(USE_ITHREADS) && !defined(WIN32)
3287     /* only "parent" interpreter can diddle signals */
3288     if (PL_curinterp != aTHX)
3289         return -1;
3290 #endif
3291     return (PerlProc_signal(signo, *save) == (Sighandler_t) SIG_ERR) ? -1 : 0;
3292 }
3293 
3294 #endif /* !HAS_SIGACTION */
3295 #endif /* !PERL_MICRO */
3296 
3297     /* VMS' my_pclose() is in VMS.c */
3298 
3299 /*
3300 =for apidoc_section $io
3301 =for apidoc my_pclose
3302 
3303 A wrapper for the C library L<pclose(3)>.  Don't use the latter, as the Perl
3304 version knows things that interact with the rest of the perl interpreter.
3305 
3306 =cut
3307 */
3308 
3309 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
3310 I32
3311 Perl_my_pclose(pTHX_ PerlIO *ptr)
3312 {
3313     int status;
3314     SV **svp;
3315     Pid_t pid;
3316     Pid_t pid2 = 0;
3317     bool close_failed;
3318     dSAVEDERRNO;
3319     const int fd = PerlIO_fileno(ptr);
3320     bool should_wait;
3321 
3322     svp = av_fetch(PL_fdpid, fd, FALSE);
3323     if (svp) {
3324         pid = (SvTYPE(*svp) == SVt_IV) ? SvIVX(*svp) : -1;
3325         SvREFCNT_dec(*svp);
3326         *svp = NULL;
3327     } else {
3328         pid = -1;
3329     }
3330 
3331 #if defined(USE_PERLIO)
3332     /* Find out whether the refcount is low enough for us to wait for the
3333        child proc without blocking. */
3334     should_wait = PerlIOUnix_refcnt(fd) == 1 && pid > 0;
3335 #else
3336     should_wait = pid > 0;
3337 #endif
3338 
3339 #ifdef OS2
3340     if (pid == -2) {                    /* Opened by popen. */
3341         return my_syspclose(ptr);
3342     }
3343 #endif
3344     close_failed = (PerlIO_close(ptr) == EOF);
3345     SAVE_ERRNO;
3346     if (should_wait) do {
3347         pid2 = wait4pid(pid, &status, 0);
3348     } while (pid2 == -1 && errno == EINTR);
3349     if (close_failed) {
3350         RESTORE_ERRNO;
3351         return -1;
3352     }
3353     return(
3354       should_wait
3355        ? pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status)
3356        : 0
3357     );
3358 }
3359 #elif defined(__LIBCATAMOUNT__)
3360 I32
3361 Perl_my_pclose(pTHX_ PerlIO *ptr)
3362 {
3363     return -1;
3364 }
3365 #endif /* !DOSISH */
3366 
3367 #if  (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
3368 I32
3369 Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags)
3370 {
3371     I32 result = 0;
3372     PERL_ARGS_ASSERT_WAIT4PID;
3373 #ifdef PERL_USES_PL_PIDSTATUS
3374     if (!pid) {
3375         /* PERL_USES_PL_PIDSTATUS is only defined when neither
3376            waitpid() nor wait4() is available, or on OS/2, which
3377            doesn't appear to support waiting for a progress group
3378            member, so we can only treat a 0 pid as an unknown child.
3379         */
3380         errno = ECHILD;
3381         return -1;
3382     }
3383     {
3384         if (pid > 0) {
3385             /* The keys in PL_pidstatus are now the raw 4 (or 8) bytes of the
3386                pid, rather than a string form.  */
3387             SV * const * const svp = hv_fetch(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),FALSE);
3388             if (svp && *svp != &PL_sv_undef) {
3389                 *statusp = SvIVX(*svp);
3390                 (void)hv_delete(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),
3391                                 G_DISCARD);
3392                 return pid;
3393             }
3394         }
3395         else {
3396             HE *entry;
3397 
3398             hv_iterinit(PL_pidstatus);
3399             if ((entry = hv_iternext(PL_pidstatus))) {
3400                 SV * const sv = hv_iterval(PL_pidstatus,entry);
3401                 I32 len;
3402                 const char * const spid = hv_iterkey(entry,&len);
3403 
3404                 assert (len == sizeof(Pid_t));
3405                 memcpy((char *)&pid, spid, len);
3406                 *statusp = SvIVX(sv);
3407                 /* The hash iterator is currently on this entry, so simply
3408                    calling hv_delete would trigger the lazy delete, which on
3409                    aggregate does more work, because next call to hv_iterinit()
3410                    would spot the flag, and have to call the delete routine,
3411                    while in the meantime any new entries can't re-use that
3412                    memory.  */
3413                 hv_iterinit(PL_pidstatus);
3414                 (void)hv_delete(PL_pidstatus,spid,len,G_DISCARD);
3415                 return pid;
3416             }
3417         }
3418     }
3419 #endif
3420 #ifdef HAS_WAITPID
3421 #  ifdef HAS_WAITPID_RUNTIME
3422     if (!HAS_WAITPID_RUNTIME)
3423         goto hard_way;
3424 #  endif
3425     result = PerlProc_waitpid(pid,statusp,flags);
3426     goto finish;
3427 #endif
3428 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
3429     result = wait4(pid,statusp,flags,NULL);
3430     goto finish;
3431 #endif
3432 #ifdef PERL_USES_PL_PIDSTATUS
3433 #if defined(HAS_WAITPID) && defined(HAS_WAITPID_RUNTIME)
3434   hard_way:
3435 #endif
3436     {
3437         if (flags)
3438             Perl_croak(aTHX_ "Can't do waitpid with flags");
3439         else {
3440             while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
3441                 pidgone(result,*statusp);
3442             if (result < 0)
3443                 *statusp = -1;
3444         }
3445     }
3446 #endif
3447 #if defined(HAS_WAITPID) || defined(HAS_WAIT4)
3448   finish:
3449 #endif
3450     if (result < 0 && errno == EINTR) {
3451         PERL_ASYNC_CHECK();
3452         errno = EINTR; /* reset in case a signal handler changed $! */
3453     }
3454     return result;
3455 }
3456 #endif /* !DOSISH || OS2 || WIN32 */
3457 
3458 #ifdef PERL_USES_PL_PIDSTATUS
3459 void
3460 S_pidgone(pTHX_ Pid_t pid, int status)
3461 {
3462     SV *sv;
3463 
3464     sv = *hv_fetch(PL_pidstatus,(const char*)&pid,sizeof(Pid_t),TRUE);
3465     SvUPGRADE(sv,SVt_IV);
3466     SvIV_set(sv, status);
3467     return;
3468 }
3469 #endif
3470 
3471 #if defined(OS2)
3472 int pclose();
3473 #ifdef HAS_FORK
3474 int					/* Cannot prototype with I32
3475                                            in os2ish.h. */
3476 my_syspclose(PerlIO *ptr)
3477 #else
3478 I32
3479 Perl_my_pclose(pTHX_ PerlIO *ptr)
3480 #endif
3481 {
3482     /* Needs work for PerlIO ! */
3483     FILE * const f = PerlIO_findFILE(ptr);
3484     const I32 result = pclose(f);
3485     PerlIO_releaseFILE(ptr,f);
3486     return result;
3487 }
3488 #endif
3489 
3490 /*
3491 =for apidoc repeatcpy
3492 
3493 Make C<count> copies of the C<len> bytes beginning at C<from>, placing them
3494 into memory beginning at C<to>, which must be big enough to accommodate them
3495 all.
3496 
3497 =cut
3498 */
3499 
3500 #define PERL_REPEATCPY_LINEAR 4
3501 void
3502 Perl_repeatcpy(char *to, const char *from, I32 len, IV count)
3503 {
3504     PERL_ARGS_ASSERT_REPEATCPY;
3505 
3506     assert(len >= 0);
3507 
3508     if (count < 0)
3509         croak_memory_wrap();
3510 
3511     if (len == 1)
3512         memset(to, *from, count);
3513     else if (count) {
3514         char *p = to;
3515         IV items, linear, half;
3516 
3517         linear = count < PERL_REPEATCPY_LINEAR ? count : PERL_REPEATCPY_LINEAR;
3518         for (items = 0; items < linear; ++items) {
3519             const char *q = from;
3520             IV todo;
3521             for (todo = len; todo > 0; todo--)
3522                 *p++ = *q++;
3523         }
3524 
3525         half = count / 2;
3526         while (items <= half) {
3527             IV size = items * len;
3528             memcpy(p, to, size);
3529             p     += size;
3530             items *= 2;
3531         }
3532 
3533         if (count > items)
3534             memcpy(p, to, (count - items) * len);
3535     }
3536 }
3537 
3538 #ifndef HAS_RENAME
3539 I32
3540 Perl_same_dirent(pTHX_ const char *a, const char *b)
3541 {
3542     char *fa = strrchr(a,'/');
3543     char *fb = strrchr(b,'/');
3544     Stat_t tmpstatbuf1;
3545     Stat_t tmpstatbuf2;
3546     SV * const tmpsv = sv_newmortal();
3547 
3548     PERL_ARGS_ASSERT_SAME_DIRENT;
3549 
3550     if (fa)
3551         fa++;
3552     else
3553         fa = a;
3554     if (fb)
3555         fb++;
3556     else
3557         fb = b;
3558     if (strNE(a,b))
3559         return FALSE;
3560     if (fa == a)
3561         sv_setpvs(tmpsv, ".");
3562     else
3563         sv_setpvn(tmpsv, a, fa - a);
3564     if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf1) < 0)
3565         return FALSE;
3566     if (fb == b)
3567         sv_setpvs(tmpsv, ".");
3568     else
3569         sv_setpvn(tmpsv, b, fb - b);
3570     if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf2) < 0)
3571         return FALSE;
3572     return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
3573            tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
3574 }
3575 #endif /* !HAS_RENAME */
3576 
3577 char*
3578 Perl_find_script(pTHX_ const char *scriptname, bool dosearch,
3579                  const char *const *const search_ext, I32 flags)
3580 {
3581     const char *xfound = NULL;
3582     char *xfailed = NULL;
3583     char tmpbuf[MAXPATHLEN];
3584     char *s;
3585     I32 len = 0;
3586     int retval;
3587     char *bufend;
3588 #if defined(DOSISH) && !defined(OS2)
3589 #  define SEARCH_EXTS ".bat", ".cmd", NULL
3590 #  define MAX_EXT_LEN 4
3591 #endif
3592 #ifdef OS2
3593 #  define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
3594 #  define MAX_EXT_LEN 4
3595 #endif
3596 #ifdef VMS
3597 #  define SEARCH_EXTS ".pl", ".com", NULL
3598 #  define MAX_EXT_LEN 4
3599 #endif
3600     /* additional extensions to try in each dir if scriptname not found */
3601 #ifdef SEARCH_EXTS
3602     static const char *const exts[] = { SEARCH_EXTS };
3603     const char *const *const ext = search_ext ? search_ext : exts;
3604     int extidx = 0, i = 0;
3605     const char *curext = NULL;
3606 #else
3607     PERL_UNUSED_ARG(search_ext);
3608 #  define MAX_EXT_LEN 0
3609 #endif
3610 
3611     PERL_ARGS_ASSERT_FIND_SCRIPT;
3612 
3613     /*
3614      * If dosearch is true and if scriptname does not contain path
3615      * delimiters, search the PATH for scriptname.
3616      *
3617      * If SEARCH_EXTS is also defined, will look for each
3618      * scriptname{SEARCH_EXTS} whenever scriptname is not found
3619      * while searching the PATH.
3620      *
3621      * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
3622      * proceeds as follows:
3623      *   If DOSISH or VMSISH:
3624      *     + look for ./scriptname{,.foo,.bar}
3625      *     + search the PATH for scriptname{,.foo,.bar}
3626      *
3627      *   If !DOSISH:
3628      *     + look *only* in the PATH for scriptname{,.foo,.bar} (note
3629      *       this will not look in '.' if it's not in the PATH)
3630      */
3631     tmpbuf[0] = '\0';
3632 
3633 #ifdef VMS
3634 #  ifdef ALWAYS_DEFTYPES
3635     len = strlen(scriptname);
3636     if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
3637         int idx = 0, deftypes = 1;
3638         bool seen_dot = 1;
3639 
3640         const int hasdir = !dosearch || (strpbrk(scriptname,":[</") != NULL);
3641 #  else
3642     if (dosearch) {
3643         int idx = 0, deftypes = 1;
3644         bool seen_dot = 1;
3645 
3646         const int hasdir = (strpbrk(scriptname,":[</") != NULL);
3647 #  endif
3648         /* The first time through, just add SEARCH_EXTS to whatever we
3649          * already have, so we can check for default file types. */
3650         while (deftypes ||
3651                (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
3652         {
3653             Stat_t statbuf;
3654             if (deftypes) {
3655                 deftypes = 0;
3656                 *tmpbuf = '\0';
3657             }
3658             if ((strlen(tmpbuf) + strlen(scriptname)
3659                  + MAX_EXT_LEN) >= sizeof tmpbuf)
3660                 continue;	/* don't search dir with too-long name */
3661             my_strlcat(tmpbuf, scriptname, sizeof(tmpbuf));
3662 #else  /* !VMS */
3663 
3664 #ifdef DOSISH
3665     if (strEQ(scriptname, "-"))
3666         dosearch = 0;
3667     if (dosearch) {		/* Look in '.' first. */
3668         const char *cur = scriptname;
3669 #ifdef SEARCH_EXTS
3670         if ((curext = strrchr(scriptname,'.')))	/* possible current ext */
3671             while (ext[i])
3672                 if (strEQ(ext[i++],curext)) {
3673                     extidx = -1;		/* already has an ext */
3674                     break;
3675                 }
3676         do {
3677 #endif
3678             DEBUG_p(PerlIO_printf(Perl_debug_log,
3679                                   "Looking for %s\n",cur));
3680             {
3681                 Stat_t statbuf;
3682                 if (PerlLIO_stat(cur,&statbuf) >= 0
3683                     && !S_ISDIR(statbuf.st_mode)) {
3684                     dosearch = 0;
3685                     scriptname = cur;
3686 #ifdef SEARCH_EXTS
3687                     break;
3688 #endif
3689                 }
3690             }
3691 #ifdef SEARCH_EXTS
3692             if (cur == scriptname) {
3693                 len = strlen(scriptname);
3694                 if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
3695                     break;
3696                 my_strlcpy(tmpbuf, scriptname, sizeof(tmpbuf));
3697                 cur = tmpbuf;
3698             }
3699         } while (extidx >= 0 && ext[extidx]	/* try an extension? */
3700                  && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len));
3701 #endif
3702     }
3703 #endif
3704 
3705     if (dosearch && !strchr(scriptname, '/')
3706 #ifdef DOSISH
3707                  && !strchr(scriptname, '\\')
3708 #endif
3709                  && (s = PerlEnv_getenv("PATH")))
3710     {
3711         bool seen_dot = 0;
3712 
3713         bufend = s + strlen(s);
3714         while (s < bufend) {
3715             Stat_t statbuf;
3716 #  ifdef DOSISH
3717             for (len = 0; *s
3718                     && *s != ';'; len++, s++) {
3719                 if (len < sizeof tmpbuf)
3720                     tmpbuf[len] = *s;
3721             }
3722             if (len < sizeof tmpbuf)
3723                 tmpbuf[len] = '\0';
3724 #  else
3725             s = delimcpy_no_escape(tmpbuf, tmpbuf + sizeof tmpbuf, s, bufend,
3726                                    ':', &len);
3727 #  endif
3728             if (s < bufend)
3729                 s++;
3730             if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
3731                 continue;	/* don't search dir with too-long name */
3732             if (len
3733 #  ifdef DOSISH
3734                 && tmpbuf[len - 1] != '/'
3735                 && tmpbuf[len - 1] != '\\'
3736 #  endif
3737                )
3738                 tmpbuf[len++] = '/';
3739             if (len == 2 && tmpbuf[0] == '.')
3740                 seen_dot = 1;
3741             (void)my_strlcpy(tmpbuf + len, scriptname, sizeof(tmpbuf) - len);
3742 #endif  /* !VMS */
3743 
3744 #ifdef SEARCH_EXTS
3745             len = strlen(tmpbuf);
3746             if (extidx > 0)	/* reset after previous loop */
3747                 extidx = 0;
3748             do {
3749 #endif
3750                 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
3751                 retval = PerlLIO_stat(tmpbuf,&statbuf);
3752                 if (S_ISDIR(statbuf.st_mode)) {
3753                     retval = -1;
3754                 }
3755 #ifdef SEARCH_EXTS
3756             } while (  retval < 0		/* not there */
3757                     && extidx>=0 && ext[extidx]	/* try an extension? */
3758                     && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len)
3759                 );
3760 #endif
3761             if (retval < 0)
3762                 continue;
3763             if (S_ISREG(statbuf.st_mode)
3764                 && cando(S_IRUSR,TRUE,&statbuf)
3765 #if !defined(DOSISH)
3766                 && cando(S_IXUSR,TRUE,&statbuf)
3767 #endif
3768                 )
3769             {
3770                 xfound = tmpbuf;		/* bingo! */
3771                 break;
3772             }
3773             if (!xfailed)
3774                 xfailed = savepv(tmpbuf);
3775         }
3776 #ifndef DOSISH
3777         {
3778             Stat_t statbuf;
3779             if (!xfound && !seen_dot && !xfailed &&
3780                 (PerlLIO_stat(scriptname,&statbuf) < 0
3781                  || S_ISDIR(statbuf.st_mode)))
3782 #endif
3783                 seen_dot = 1;			/* Disable message. */
3784 #ifndef DOSISH
3785         }
3786 #endif
3787         if (!xfound) {
3788             if (flags & 1) {			/* do or die? */
3789                 /* diag_listed_as: Can't execute %s */
3790                 Perl_croak(aTHX_ "Can't %s %s%s%s",
3791                       (xfailed ? "execute" : "find"),
3792                       (xfailed ? xfailed : scriptname),
3793                       (xfailed ? "" : " on PATH"),
3794                       (xfailed || seen_dot) ? "" : ", '.' not in PATH");
3795             }
3796             scriptname = NULL;
3797         }
3798         Safefree(xfailed);
3799         scriptname = xfound;
3800     }
3801     return (scriptname ? savepv(scriptname) : NULL);
3802 }
3803 
3804 #ifndef PERL_GET_CONTEXT_DEFINED
3805 
3806 /*
3807 =for apidoc_section $embedding
3808 =for apidoc get_context
3809 
3810 Implements L<perlapi/C<PERL_GET_CONTEXT>>, which you should use instead.
3811 
3812 =cut
3813 */
3814 
3815 void *
3816 Perl_get_context(void)
3817 {
3818 #if defined(USE_ITHREADS)
3819 #  ifdef OLD_PTHREADS_API
3820     pthread_addr_t t;
3821     int error = pthread_getspecific(PL_thr_key, &t);
3822     if (error)
3823         Perl_croak_nocontext("panic: pthread_getspecific, error=%d", error);
3824     return (void*)t;
3825 #  elif defined(I_MACH_CTHREADS)
3826     return (void*)cthread_data(cthread_self());
3827 #  else
3828     return (void*)PTHREAD_GETSPECIFIC(PL_thr_key);
3829 #  endif
3830 #else
3831     return (void*)NULL;
3832 #endif
3833 }
3834 
3835 /*
3836 =for apidoc_section $embedding
3837 =for apidoc set_context
3838 
3839 Implements L<perlapi/C<PERL_SET_CONTEXT>>, which you should use instead.
3840 
3841 =cut
3842 */
3843 
3844 void
3845 Perl_set_context(void *t)
3846 {
3847     PERL_ARGS_ASSERT_SET_CONTEXT;
3848 #if defined(USE_ITHREADS)
3849 #  ifdef PERL_USE_THREAD_LOCAL
3850     PL_current_context = t;
3851 #  endif
3852 #  ifdef I_MACH_CTHREADS
3853     cthread_set_data(cthread_self(), t);
3854 #  else
3855     /* We set thread-specific value always, as C++ code has to read it with
3856      * pthreads, beacuse the declaration syntax for thread local storage for C11
3857      * is incompatible with C++, meaning that we can't expose the thread local
3858      * variable to C++ code. */
3859     {
3860         const int error = pthread_setspecific(PL_thr_key, t);
3861         if (error)
3862             Perl_croak_nocontext("panic: pthread_setspecific, error=%d", error);
3863     }
3864 #  endif
3865 #else
3866     PERL_UNUSED_ARG(t);
3867 #endif
3868 }
3869 
3870 #endif /* !PERL_GET_CONTEXT_DEFINED */
3871 
3872 char **
3873 Perl_get_op_names(pTHX)
3874 {
3875     PERL_UNUSED_CONTEXT;
3876     return (char **)PL_op_name;
3877 }
3878 
3879 char **
3880 Perl_get_op_descs(pTHX)
3881 {
3882     PERL_UNUSED_CONTEXT;
3883     return (char **)PL_op_desc;
3884 }
3885 
3886 const char *
3887 Perl_get_no_modify(pTHX)
3888 {
3889     PERL_UNUSED_CONTEXT;
3890     return PL_no_modify;
3891 }
3892 
3893 U32 *
3894 Perl_get_opargs(pTHX)
3895 {
3896     PERL_UNUSED_CONTEXT;
3897     return (U32 *)PL_opargs;
3898 }
3899 
3900 PPADDR_t*
3901 Perl_get_ppaddr(pTHX)
3902 {
3903     PERL_UNUSED_CONTEXT;
3904     return (PPADDR_t*)PL_ppaddr;
3905 }
3906 
3907 #ifndef HAS_GETENV_LEN
3908 char *
3909 Perl_getenv_len(pTHX_ const char *env_elem, unsigned long *len)
3910 {
3911     char * const env_trans = PerlEnv_getenv(env_elem);
3912     PERL_UNUSED_CONTEXT;
3913     PERL_ARGS_ASSERT_GETENV_LEN;
3914     if (env_trans)
3915         *len = strlen(env_trans);
3916     return env_trans;
3917 }
3918 #endif
3919 
3920 
3921 MGVTBL*
3922 Perl_get_vtbl(pTHX_ int vtbl_id)
3923 {
3924     PERL_UNUSED_CONTEXT;
3925 
3926     return (vtbl_id < 0 || vtbl_id >= magic_vtable_max)
3927         ? NULL : (MGVTBL*)PL_magic_vtables + vtbl_id;
3928 }
3929 
3930 /*
3931 =for apidoc_section $io
3932 =for apidoc my_fflush_all
3933 
3934 Implements C<PERL_FLUSHALL_FOR_CHILD> on some platforms.
3935 
3936 =cut
3937  */
3938 
3939 I32
3940 Perl_my_fflush_all(pTHX)
3941 {
3942 #if defined(USE_PERLIO) || defined(FFLUSH_NULL)
3943     return PerlIO_flush(NULL);
3944 #else
3945 # if defined(HAS__FWALK)
3946     extern int fflush(FILE *);
3947     /* undocumented, unprototyped, but very useful BSDism */
3948     extern void _fwalk(int (*)(FILE *));
3949     _fwalk(&fflush);
3950     return 0;
3951 # else
3952 #  if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY)
3953     long open_max = -1;
3954 #   ifdef PERL_FFLUSH_ALL_FOPEN_MAX
3955     open_max = PERL_FFLUSH_ALL_FOPEN_MAX;
3956 #   elif defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
3957     open_max = sysconf(_SC_OPEN_MAX);
3958 #   elif defined(FOPEN_MAX)
3959     open_max = FOPEN_MAX;
3960 #   elif defined(OPEN_MAX)
3961     open_max = OPEN_MAX;
3962 #   elif defined(_NFILE)
3963     open_max = _NFILE;
3964 #   endif
3965     if (open_max > 0) {
3966       long i;
3967       for (i = 0; i < open_max; i++)
3968             if (STDIO_STREAM_ARRAY[i]._file >= 0 &&
3969                 STDIO_STREAM_ARRAY[i]._file < open_max &&
3970                 STDIO_STREAM_ARRAY[i]._flag)
3971                 PerlIO_flush(&STDIO_STREAM_ARRAY[i]);
3972       return 0;
3973     }
3974 #  endif
3975     SETERRNO(EBADF,RMS_IFI);
3976     return EOF;
3977 # endif
3978 #endif
3979 }
3980 
3981 void
3982 Perl_report_wrongway_fh(pTHX_ const GV *gv, const char have)
3983 {
3984     if (ckWARN(WARN_IO)) {
3985         HEK * const name
3986            = gv && (isGV_with_GP(gv))
3987                 ? GvENAME_HEK((gv))
3988                 : NULL;
3989         const char * const direction = have == '>' ? "out" : "in";
3990 
3991         if (name && HEK_LEN(name))
3992             Perl_warner(aTHX_ packWARN(WARN_IO),
3993                         "Filehandle %" HEKf " opened only for %sput",
3994                         HEKfARG(name), direction);
3995         else
3996             Perl_warner(aTHX_ packWARN(WARN_IO),
3997                         "Filehandle opened only for %sput", direction);
3998     }
3999 }
4000 
4001 void
4002 Perl_report_evil_fh(pTHX_ const GV *gv)
4003 {
4004     const IO *io = gv ? GvIO(gv) : NULL;
4005     const PERL_BITFIELD16 op = PL_op->op_type;
4006     const char *vile;
4007     I32 warn_type;
4008 
4009     if (io && IoTYPE(io) == IoTYPE_CLOSED) {
4010         vile = "closed";
4011         warn_type = WARN_CLOSED;
4012     }
4013     else {
4014         vile = "unopened";
4015         warn_type = WARN_UNOPENED;
4016     }
4017 
4018     if (ckWARN(warn_type)) {
4019         SV * const name
4020             = gv && isGV_with_GP(gv) && GvENAMELEN(gv) ?
4021                                      sv_2mortal(newSVhek(GvENAME_HEK(gv))) : NULL;
4022         const char * const pars =
4023             (const char *)(OP_IS_FILETEST(op) ? "" : "()");
4024         const char * const func =
4025             (const char *)
4026             (op == OP_READLINE || op == OP_RCATLINE
4027                                  ? "readline"  :	/* "<HANDLE>" not nice */
4028              op == OP_LEAVEWRITE ? "write" :		/* "write exit" not nice */
4029              PL_op_desc[op]);
4030         const char * const type =
4031             (const char *)
4032             (OP_IS_SOCKET(op) || (io && IoTYPE(io) == IoTYPE_SOCKET)
4033              ? "socket" : "filehandle");
4034         const bool have_name = name && SvCUR(name);
4035         Perl_warner(aTHX_ packWARN(warn_type),
4036                    "%s%s on %s %s%s%" SVf, func, pars, vile, type,
4037                     have_name ? " " : "",
4038                     SVfARG(have_name ? name : &PL_sv_no));
4039         if (io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP))
4040                 Perl_warner(
4041                             aTHX_ packWARN(warn_type),
4042                         "\t(Are you trying to call %s%s on dirhandle%s%" SVf "?)\n",
4043                         func, pars, have_name ? " " : "",
4044                         SVfARG(have_name ? name : &PL_sv_no)
4045                             );
4046     }
4047 }
4048 
4049 /* To workaround core dumps from the uninitialised tm_zone we get the
4050  * system to give us a reasonable struct to copy.  This fix means that
4051  * strftime uses the tm_zone and tm_gmtoff values returned by
4052  * localtime(time()). That should give the desired result most of the
4053  * time. But probably not always!
4054  *
4055  * This does not address tzname aspects of NETaa14816.
4056  *
4057  */
4058 
4059 #ifdef __GLIBC__
4060 # ifndef STRUCT_TM_HASZONE
4061 #    define STRUCT_TM_HASZONE
4062 # endif
4063 #endif
4064 
4065 #ifdef STRUCT_TM_HASZONE /* Backward compat */
4066 # ifndef HAS_TM_TM_ZONE
4067 #    define HAS_TM_TM_ZONE
4068 # endif
4069 #endif
4070 
4071 void
4072 Perl_init_tm(pTHX_ struct tm *ptm)	/* see mktime, strftime and asctime */
4073 {
4074 #ifdef HAS_TM_TM_ZONE
4075     Time_t now;
4076     const struct tm* my_tm;
4077     PERL_UNUSED_CONTEXT;
4078     PERL_ARGS_ASSERT_INIT_TM;
4079     (void)time(&now);
4080     ENV_LOCALE_READ_LOCK;
4081     my_tm = localtime(&now);
4082     if (my_tm)
4083         Copy(my_tm, ptm, 1, struct tm);
4084     ENV_LOCALE_READ_UNLOCK;
4085 #else
4086     PERL_UNUSED_CONTEXT;
4087     PERL_ARGS_ASSERT_INIT_TM;
4088     PERL_UNUSED_ARG(ptm);
4089 #endif
4090 }
4091 
4092 /*
4093 =for apidoc_section $time
4094 =for apidoc mini_mktime
4095 normalise S<C<struct tm>> values without the localtime() semantics (and
4096 overhead) of mktime().
4097 
4098 =cut
4099  */
4100 void
4101 Perl_mini_mktime(struct tm *ptm)
4102 {
4103     int yearday;
4104     int secs;
4105     int month, mday, year, jday;
4106     int odd_cent, odd_year;
4107 
4108     PERL_ARGS_ASSERT_MINI_MKTIME;
4109 
4110 #define	DAYS_PER_YEAR	365
4111 #define	DAYS_PER_QYEAR	(4*DAYS_PER_YEAR+1)
4112 #define	DAYS_PER_CENT	(25*DAYS_PER_QYEAR-1)
4113 #define	DAYS_PER_QCENT	(4*DAYS_PER_CENT+1)
4114 #define	SECS_PER_HOUR	(60*60)
4115 #define	SECS_PER_DAY	(24*SECS_PER_HOUR)
4116 /* parentheses deliberately absent on these two, otherwise they don't work */
4117 #define	MONTH_TO_DAYS	153/5
4118 #define	DAYS_TO_MONTH	5/153
4119 /* offset to bias by March (month 4) 1st between month/mday & year finding */
4120 #define	YEAR_ADJUST	(4*MONTH_TO_DAYS+1)
4121 /* as used here, the algorithm leaves Sunday as day 1 unless we adjust it */
4122 #define	WEEKDAY_BIAS	6	/* (1+6)%7 makes Sunday 0 again */
4123 
4124 /*
4125  * Year/day algorithm notes:
4126  *
4127  * With a suitable offset for numeric value of the month, one can find
4128  * an offset into the year by considering months to have 30.6 (153/5) days,
4129  * using integer arithmetic (i.e., with truncation).  To avoid too much
4130  * messing about with leap days, we consider January and February to be
4131  * the 13th and 14th month of the previous year.  After that transformation,
4132  * we need the month index we use to be high by 1 from 'normal human' usage,
4133  * so the month index values we use run from 4 through 15.
4134  *
4135  * Given that, and the rules for the Gregorian calendar (leap years are those
4136  * divisible by 4 unless also divisible by 100, when they must be divisible
4137  * by 400 instead), we can simply calculate the number of days since some
4138  * arbitrary 'beginning of time' by futzing with the (adjusted) year number,
4139  * the days we derive from our month index, and adding in the day of the
4140  * month.  The value used here is not adjusted for the actual origin which
4141  * it normally would use (1 January A.D. 1), since we're not exposing it.
4142  * We're only building the value so we can turn around and get the
4143  * normalised values for the year, month, day-of-month, and day-of-year.
4144  *
4145  * For going backward, we need to bias the value we're using so that we find
4146  * the right year value.  (Basically, we don't want the contribution of
4147  * March 1st to the number to apply while deriving the year).  Having done
4148  * that, we 'count up' the contribution to the year number by accounting for
4149  * full quadracenturies (400-year periods) with their extra leap days, plus
4150  * the contribution from full centuries (to avoid counting in the lost leap
4151  * days), plus the contribution from full quad-years (to count in the normal
4152  * leap days), plus the leftover contribution from any non-leap years.
4153  * At this point, if we were working with an actual leap day, we'll have 0
4154  * days left over.  This is also true for March 1st, however.  So, we have
4155  * to special-case that result, and (earlier) keep track of the 'odd'
4156  * century and year contributions.  If we got 4 extra centuries in a qcent,
4157  * or 4 extra years in a qyear, then it's a leap day and we call it 29 Feb.
4158  * Otherwise, we add back in the earlier bias we removed (the 123 from
4159  * figuring in March 1st), find the month index (integer division by 30.6),
4160  * and the remainder is the day-of-month.  We then have to convert back to
4161  * 'real' months (including fixing January and February from being 14/15 in
4162  * the previous year to being in the proper year).  After that, to get
4163  * tm_yday, we work with the normalised year and get a new yearday value for
4164  * January 1st, which we subtract from the yearday value we had earlier,
4165  * representing the date we've re-built.  This is done from January 1
4166  * because tm_yday is 0-origin.
4167  *
4168  * Since POSIX time routines are only guaranteed to work for times since the
4169  * UNIX epoch (00:00:00 1 Jan 1970 UTC), the fact that this algorithm
4170  * applies Gregorian calendar rules even to dates before the 16th century
4171  * doesn't bother me.  Besides, you'd need cultural context for a given
4172  * date to know whether it was Julian or Gregorian calendar, and that's
4173  * outside the scope for this routine.  Since we convert back based on the
4174  * same rules we used to build the yearday, you'll only get strange results
4175  * for input which needed normalising, or for the 'odd' century years which
4176  * were leap years in the Julian calendar but not in the Gregorian one.
4177  * I can live with that.
4178  *
4179  * This algorithm also fails to handle years before A.D. 1 gracefully, but
4180  * that's still outside the scope for POSIX time manipulation, so I don't
4181  * care.
4182  *
4183  * - lwall
4184  */
4185 
4186     year = 1900 + ptm->tm_year;
4187     month = ptm->tm_mon;
4188     mday = ptm->tm_mday;
4189     jday = 0;
4190     if (month >= 2)
4191         month+=2;
4192     else
4193         month+=14, year--;
4194     yearday = DAYS_PER_YEAR * year + year/4 - year/100 + year/400;
4195     yearday += month*MONTH_TO_DAYS + mday + jday;
4196     /*
4197      * Note that we don't know when leap-seconds were or will be,
4198      * so we have to trust the user if we get something which looks
4199      * like a sensible leap-second.  Wild values for seconds will
4200      * be rationalised, however.
4201      */
4202     if ((unsigned) ptm->tm_sec <= 60) {
4203         secs = 0;
4204     }
4205     else {
4206         secs = ptm->tm_sec;
4207         ptm->tm_sec = 0;
4208     }
4209     secs += 60 * ptm->tm_min;
4210     secs += SECS_PER_HOUR * ptm->tm_hour;
4211     if (secs < 0) {
4212         if (secs-(secs/SECS_PER_DAY*SECS_PER_DAY) < 0) {
4213             /* got negative remainder, but need positive time */
4214             /* back off an extra day to compensate */
4215             yearday += (secs/SECS_PER_DAY)-1;
4216             secs -= SECS_PER_DAY * (secs/SECS_PER_DAY - 1);
4217         }
4218         else {
4219             yearday += (secs/SECS_PER_DAY);
4220             secs -= SECS_PER_DAY * (secs/SECS_PER_DAY);
4221         }
4222     }
4223     else if (secs >= SECS_PER_DAY) {
4224         yearday += (secs/SECS_PER_DAY);
4225         secs %= SECS_PER_DAY;
4226     }
4227     ptm->tm_hour = secs/SECS_PER_HOUR;
4228     secs %= SECS_PER_HOUR;
4229     ptm->tm_min = secs/60;
4230     secs %= 60;
4231     ptm->tm_sec += secs;
4232     /* done with time of day effects */
4233     /*
4234      * The algorithm for yearday has (so far) left it high by 428.
4235      * To avoid mistaking a legitimate Feb 29 as Mar 1, we need to
4236      * bias it by 123 while trying to figure out what year it
4237      * really represents.  Even with this tweak, the reverse
4238      * translation fails for years before A.D. 0001.
4239      * It would still fail for Feb 29, but we catch that one below.
4240      */
4241     jday = yearday;	/* save for later fixup vis-a-vis Jan 1 */
4242     yearday -= YEAR_ADJUST;
4243     year = (yearday / DAYS_PER_QCENT) * 400;
4244     yearday %= DAYS_PER_QCENT;
4245     odd_cent = yearday / DAYS_PER_CENT;
4246     year += odd_cent * 100;
4247     yearday %= DAYS_PER_CENT;
4248     year += (yearday / DAYS_PER_QYEAR) * 4;
4249     yearday %= DAYS_PER_QYEAR;
4250     odd_year = yearday / DAYS_PER_YEAR;
4251     year += odd_year;
4252     yearday %= DAYS_PER_YEAR;
4253     if (!yearday && (odd_cent==4 || odd_year==4)) { /* catch Feb 29 */
4254         month = 1;
4255         yearday = 29;
4256     }
4257     else {
4258         yearday += YEAR_ADJUST;	/* recover March 1st crock */
4259         month = yearday*DAYS_TO_MONTH;
4260         yearday -= month*MONTH_TO_DAYS;
4261         /* recover other leap-year adjustment */
4262         if (month > 13) {
4263             month-=14;
4264             year++;
4265         }
4266         else {
4267             month-=2;
4268         }
4269     }
4270     ptm->tm_year = year - 1900;
4271     if (yearday) {
4272       ptm->tm_mday = yearday;
4273       ptm->tm_mon = month;
4274     }
4275     else {
4276       ptm->tm_mday = 31;
4277       ptm->tm_mon = month - 1;
4278     }
4279     /* re-build yearday based on Jan 1 to get tm_yday */
4280     year--;
4281     yearday = year*DAYS_PER_YEAR + year/4 - year/100 + year/400;
4282     yearday += 14*MONTH_TO_DAYS + 1;
4283     ptm->tm_yday = jday - yearday;
4284     ptm->tm_wday = (jday + WEEKDAY_BIAS) % 7;
4285 }
4286 
4287 char *
4288 Perl_my_strftime(pTHX_ const char *fmt, int sec, int min, int hour, int mday, int mon, int year, int wday, int yday, int isdst)
4289 {
4290 #ifdef HAS_STRFTIME
4291 
4292 /*
4293 =for apidoc_section $time
4294 =for apidoc my_strftime
4295 strftime(), but with a different API so that the return value is a pointer
4296 to the formatted result (which MUST be arranged to be FREED BY THE
4297 CALLER).  This allows this function to increase the buffer size as needed,
4298 so that the caller doesn't have to worry about that.
4299 
4300 Note that yday and wday effectively are ignored by this function, as
4301 mini_mktime() overwrites them
4302 
4303 Also note that this is always executed in the underlying locale of the program,
4304 giving localized results.
4305 
4306 =cut
4307  */
4308 
4309   char *buf;
4310   int buflen;
4311   struct tm mytm;
4312   int len;
4313 
4314   PERL_ARGS_ASSERT_MY_STRFTIME;
4315 
4316   init_tm(&mytm);	/* XXX workaround - see init_tm() above */
4317   mytm.tm_sec = sec;
4318   mytm.tm_min = min;
4319   mytm.tm_hour = hour;
4320   mytm.tm_mday = mday;
4321   mytm.tm_mon = mon;
4322   mytm.tm_year = year;
4323   mytm.tm_wday = wday;
4324   mytm.tm_yday = yday;
4325   mytm.tm_isdst = isdst;
4326   mini_mktime(&mytm);
4327   /* use libc to get the values for tm_gmtoff and tm_zone [perl #18238] */
4328 #if defined(HAS_MKTIME) && (defined(HAS_TM_TM_GMTOFF) || defined(HAS_TM_TM_ZONE))
4329   STMT_START {
4330     struct tm mytm2;
4331     mytm2 = mytm;
4332     mktime(&mytm2);
4333 #ifdef HAS_TM_TM_GMTOFF
4334     mytm.tm_gmtoff = mytm2.tm_gmtoff;
4335 #endif
4336 #ifdef HAS_TM_TM_ZONE
4337     mytm.tm_zone = mytm2.tm_zone;
4338 #endif
4339   } STMT_END;
4340 #endif
4341   buflen = 64;
4342   Newx(buf, buflen, char);
4343 
4344   GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral); /* fmt checked by caller */
4345 
4346   len = strftime(buf, buflen, fmt, &mytm);
4347 
4348   GCC_DIAG_RESTORE_STMT;
4349 
4350   /*
4351   ** The following is needed to handle the situation where
4352   ** tmpbuf overflows.  Basically we want to allocate a buffer
4353   ** and try repeatedly, until it's large enough.  The reason why it is so
4354   ** complicated ** is that getting a return value of 0 from strftime can
4355   ** indicate one of the following:
4356   ** 1. buffer overflowed,
4357   ** 2. illegal conversion specifier, or
4358   ** 3. the format string specifies nothing to be returned (which isn't an
4359   **    an error).  This could be because the format is an empty string
4360   **    or it specifies %p which yields an empty string in some locales.
4361   ** If there is a better way to make it portable, go ahead by
4362   ** all means.
4363   */
4364   if (inRANGE(len, 1, buflen - 1) || (len == 0 && *fmt == '\0'))
4365     return buf;
4366   else {
4367     /* Possibly buf overflowed - try again with a bigger buf */
4368     const int fmtlen = strlen(fmt);
4369     int bufsize = fmtlen + buflen;
4370 
4371     Renew(buf, bufsize, char);
4372     while (buf) {
4373 
4374       GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral); /* fmt checked by caller */
4375       buflen = strftime(buf, bufsize, fmt, &mytm);
4376       GCC_DIAG_RESTORE_STMT;
4377 
4378       if (inRANGE(buflen, 1, bufsize - 1))
4379         break;
4380       /* heuristic to prevent out-of-memory errors */
4381       if (bufsize > 100*fmtlen) {
4382 
4383         /* "%p" can legally return nothing, assume that was the case if we
4384          * can't make the buffer large enough to get a non-zero return.  For
4385          * any other formats, assume it is an error (probably it is an illegal
4386          * conversion specifier.) */
4387         if (strEQ(fmt, "%p")) {
4388             Renew(buf, 1, char);
4389             *buf = '\0';
4390         }
4391         else {
4392             Safefree(buf);
4393             buf = NULL;
4394         }
4395         break;
4396       }
4397       bufsize *= 2;
4398       Renew(buf, bufsize, char);
4399     }
4400     return buf;
4401   }
4402 #else
4403   Perl_croak(aTHX_ "panic: no strftime");
4404   return NULL;
4405 #endif
4406 }
4407 
4408 
4409 #define SV_CWD_RETURN_UNDEF \
4410     sv_set_undef(sv); \
4411     return FALSE
4412 
4413 #define SV_CWD_ISDOT(dp) \
4414     (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
4415         (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
4416 
4417 /*
4418 =for apidoc_section $utility
4419 
4420 =for apidoc getcwd_sv
4421 
4422 Fill C<sv> with current working directory
4423 
4424 =cut
4425 */
4426 
4427 /* Originally written in Perl by John Bazik; rewritten in C by Ben Sugars.
4428  * rewritten again by dougm, optimized for use with xs TARG, and to prefer
4429  * getcwd(3) if available
4430  * Comments from the original:
4431  *     This is a faster version of getcwd.  It's also more dangerous
4432  *     because you might chdir out of a directory that you can't chdir
4433  *     back into. */
4434 
4435 int
4436 Perl_getcwd_sv(pTHX_ SV *sv)
4437 {
4438 #ifndef PERL_MICRO
4439     SvTAINTED_on(sv);
4440 
4441     PERL_ARGS_ASSERT_GETCWD_SV;
4442 
4443 #ifdef HAS_GETCWD
4444     {
4445         char buf[MAXPATHLEN];
4446 
4447         /* Some getcwd()s automatically allocate a buffer of the given
4448          * size from the heap if they are given a NULL buffer pointer.
4449          * The problem is that this behaviour is not portable. */
4450         if (getcwd(buf, sizeof(buf) - 1)) {
4451             sv_setpv(sv, buf);
4452             return TRUE;
4453         }
4454         else {
4455             SV_CWD_RETURN_UNDEF;
4456         }
4457     }
4458 
4459 #else
4460 
4461     Stat_t statbuf;
4462     int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
4463     int pathlen=0;
4464     Direntry_t *dp;
4465 
4466     SvUPGRADE(sv, SVt_PV);
4467 
4468     if (PerlLIO_lstat(".", &statbuf) < 0) {
4469         SV_CWD_RETURN_UNDEF;
4470     }
4471 
4472     orig_cdev = statbuf.st_dev;
4473     orig_cino = statbuf.st_ino;
4474     cdev = orig_cdev;
4475     cino = orig_cino;
4476 
4477     for (;;) {
4478         DIR *dir;
4479         int namelen;
4480         odev = cdev;
4481         oino = cino;
4482 
4483         if (PerlDir_chdir("..") < 0) {
4484             SV_CWD_RETURN_UNDEF;
4485         }
4486         if (PerlLIO_stat(".", &statbuf) < 0) {
4487             SV_CWD_RETURN_UNDEF;
4488         }
4489 
4490         cdev = statbuf.st_dev;
4491         cino = statbuf.st_ino;
4492 
4493         if (odev == cdev && oino == cino) {
4494             break;
4495         }
4496         if (!(dir = PerlDir_open("."))) {
4497             SV_CWD_RETURN_UNDEF;
4498         }
4499 
4500         while ((dp = PerlDir_read(dir)) != NULL) {
4501 #ifdef DIRNAMLEN
4502             namelen = dp->d_namlen;
4503 #else
4504             namelen = strlen(dp->d_name);
4505 #endif
4506             /* skip . and .. */
4507             if (SV_CWD_ISDOT(dp)) {
4508                 continue;
4509             }
4510 
4511             if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
4512                 SV_CWD_RETURN_UNDEF;
4513             }
4514 
4515             tdev = statbuf.st_dev;
4516             tino = statbuf.st_ino;
4517             if (tino == oino && tdev == odev) {
4518                 break;
4519             }
4520         }
4521 
4522         if (!dp) {
4523             SV_CWD_RETURN_UNDEF;
4524         }
4525 
4526         if (pathlen + namelen + 1 >= MAXPATHLEN) {
4527             SV_CWD_RETURN_UNDEF;
4528         }
4529 
4530         SvGROW(sv, pathlen + namelen + 1);
4531 
4532         if (pathlen) {
4533             /* shift down */
4534             Move(SvPVX_const(sv), SvPVX(sv) + namelen + 1, pathlen, char);
4535         }
4536 
4537         /* prepend current directory to the front */
4538         *SvPVX(sv) = '/';
4539         Move(dp->d_name, SvPVX(sv)+1, namelen, char);
4540         pathlen += (namelen + 1);
4541 
4542 #ifdef VOID_CLOSEDIR
4543         PerlDir_close(dir);
4544 #else
4545         if (PerlDir_close(dir) < 0) {
4546             SV_CWD_RETURN_UNDEF;
4547         }
4548 #endif
4549     }
4550 
4551     if (pathlen) {
4552         SvCUR_set(sv, pathlen);
4553         *SvEND(sv) = '\0';
4554         SvPOK_only(sv);
4555 
4556         if (PerlDir_chdir(SvPVX_const(sv)) < 0) {
4557             SV_CWD_RETURN_UNDEF;
4558         }
4559     }
4560     if (PerlLIO_stat(".", &statbuf) < 0) {
4561         SV_CWD_RETURN_UNDEF;
4562     }
4563 
4564     cdev = statbuf.st_dev;
4565     cino = statbuf.st_ino;
4566 
4567     if (cdev != orig_cdev || cino != orig_cino) {
4568         Perl_croak(aTHX_ "Unstable directory path, "
4569                    "current directory changed unexpectedly");
4570     }
4571 
4572     return TRUE;
4573 #endif
4574 
4575 #else
4576     return FALSE;
4577 #endif
4578 }
4579 
4580 #include "vutil.c"
4581 
4582 #if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET) && defined(SOCK_DGRAM) && defined(HAS_SELECT)
4583 #   define EMULATE_SOCKETPAIR_UDP
4584 #endif
4585 
4586 #ifdef EMULATE_SOCKETPAIR_UDP
4587 static int
4588 S_socketpair_udp (int fd[2]) {
4589     dTHX;
4590     /* Fake a datagram socketpair using UDP to localhost.  */
4591     int sockets[2] = {-1, -1};
4592     struct sockaddr_in addresses[2];
4593     int i;
4594     Sock_size_t size = sizeof(struct sockaddr_in);
4595     unsigned short port;
4596     int got;
4597 
4598     memset(&addresses, 0, sizeof(addresses));
4599     i = 1;
4600     do {
4601         sockets[i] = PerlSock_socket(AF_INET, SOCK_DGRAM, PF_INET);
4602         if (sockets[i] == -1)
4603             goto tidy_up_and_fail;
4604 
4605         addresses[i].sin_family = AF_INET;
4606         addresses[i].sin_addr.s_addr = htonl(INADDR_LOOPBACK);
4607         addresses[i].sin_port = 0;	/* kernel choses port.  */
4608         if (PerlSock_bind(sockets[i], (struct sockaddr *) &addresses[i],
4609                 sizeof(struct sockaddr_in)) == -1)
4610             goto tidy_up_and_fail;
4611     } while (i--);
4612 
4613     /* Now have 2 UDP sockets. Find out which port each is connected to, and
4614        for each connect the other socket to it.  */
4615     i = 1;
4616     do {
4617         if (PerlSock_getsockname(sockets[i], (struct sockaddr *) &addresses[i],
4618                 &size) == -1)
4619             goto tidy_up_and_fail;
4620         if (size != sizeof(struct sockaddr_in))
4621             goto abort_tidy_up_and_fail;
4622         /* !1 is 0, !0 is 1 */
4623         if (PerlSock_connect(sockets[!i], (struct sockaddr *) &addresses[i],
4624                 sizeof(struct sockaddr_in)) == -1)
4625             goto tidy_up_and_fail;
4626     } while (i--);
4627 
4628     /* Now we have 2 sockets connected to each other. I don't trust some other
4629        process not to have already sent a packet to us (by random) so send
4630        a packet from each to the other.  */
4631     i = 1;
4632     do {
4633         /* I'm going to send my own port number.  As a short.
4634            (Who knows if someone somewhere has sin_port as a bitfield and needs
4635            this routine. (I'm assuming crays have socketpair)) */
4636         port = addresses[i].sin_port;
4637         got = PerlLIO_write(sockets[i], &port, sizeof(port));
4638         if (got != sizeof(port)) {
4639             if (got == -1)
4640                 goto tidy_up_and_fail;
4641             goto abort_tidy_up_and_fail;
4642         }
4643     } while (i--);
4644 
4645     /* Packets sent. I don't trust them to have arrived though.
4646        (As I understand it Solaris TCP stack is multithreaded. Non-blocking
4647        connect to localhost will use a second kernel thread. In 2.6 the
4648        first thread running the connect() returns before the second completes,
4649        so EINPROGRESS> In 2.7 the improved stack is faster and connect()
4650        returns 0. Poor programs have tripped up. One poor program's authors'
4651        had a 50-1 reverse stock split. Not sure how connected these were.)
4652        So I don't trust someone not to have an unpredictable UDP stack.
4653     */
4654 
4655     {
4656         struct timeval waitfor = {0, 100000}; /* You have 0.1 seconds */
4657         int max = sockets[1] > sockets[0] ? sockets[1] : sockets[0];
4658         fd_set rset;
4659 
4660         FD_ZERO(&rset);
4661         FD_SET((unsigned int)sockets[0], &rset);
4662         FD_SET((unsigned int)sockets[1], &rset);
4663 
4664         got = PerlSock_select(max + 1, &rset, NULL, NULL, &waitfor);
4665         if (got != 2 || !FD_ISSET(sockets[0], &rset)
4666                 || !FD_ISSET(sockets[1], &rset)) {
4667             /* I hope this is portable and appropriate.  */
4668             if (got == -1)
4669                 goto tidy_up_and_fail;
4670             goto abort_tidy_up_and_fail;
4671         }
4672     }
4673 
4674     /* And the paranoia department even now doesn't trust it to have arrive
4675        (hence MSG_DONTWAIT). Or that what arrives was sent by us.  */
4676     {
4677         struct sockaddr_in readfrom;
4678         unsigned short buffer[2];
4679 
4680         i = 1;
4681         do {
4682 #ifdef MSG_DONTWAIT
4683             got = PerlSock_recvfrom(sockets[i], (char *) &buffer,
4684                     sizeof(buffer), MSG_DONTWAIT,
4685                     (struct sockaddr *) &readfrom, &size);
4686 #else
4687             got = PerlSock_recvfrom(sockets[i], (char *) &buffer,
4688                     sizeof(buffer), 0,
4689                     (struct sockaddr *) &readfrom, &size);
4690 #endif
4691 
4692             if (got == -1)
4693                 goto tidy_up_and_fail;
4694             if (got != sizeof(port)
4695                     || size != sizeof(struct sockaddr_in)
4696                     /* Check other socket sent us its port.  */
4697                     || buffer[0] != (unsigned short) addresses[!i].sin_port
4698                     /* Check kernel says we got the datagram from that socket */
4699                     || readfrom.sin_family != addresses[!i].sin_family
4700                     || readfrom.sin_addr.s_addr != addresses[!i].sin_addr.s_addr
4701                     || readfrom.sin_port != addresses[!i].sin_port)
4702                 goto abort_tidy_up_and_fail;
4703         } while (i--);
4704     }
4705     /* My caller (my_socketpair) has validated that this is non-NULL  */
4706     fd[0] = sockets[0];
4707     fd[1] = sockets[1];
4708     /* I hereby declare this connection open.  May God bless all who cross
4709        her.  */
4710     return 0;
4711 
4712   abort_tidy_up_and_fail:
4713     errno = ECONNABORTED;
4714   tidy_up_and_fail:
4715     {
4716         dSAVE_ERRNO;
4717         if (sockets[0] != -1)
4718             PerlLIO_close(sockets[0]);
4719         if (sockets[1] != -1)
4720             PerlLIO_close(sockets[1]);
4721         RESTORE_ERRNO;
4722         return -1;
4723     }
4724 }
4725 #endif /*  EMULATE_SOCKETPAIR_UDP */
4726 
4727 #if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET)
4728 
4729 /*
4730 =for apidoc my_socketpair
4731 
4732 Emulates L<socketpair(2)> on systems that don't have it, but which do have
4733 enough functionality for the emulation.
4734 
4735 =cut
4736 */
4737 
4738 int
4739 Perl_my_socketpair (int family, int type, int protocol, int fd[2]) {
4740     /* Stevens says that family must be AF_LOCAL, protocol 0.
4741        I'm going to enforce that, then ignore it, and use TCP (or UDP).  */
4742     dTHXa(NULL);
4743     int listener = -1;
4744     int connector = -1;
4745     int acceptor = -1;
4746     struct sockaddr_in listen_addr;
4747     struct sockaddr_in connect_addr;
4748     Sock_size_t size;
4749 
4750     if (protocol
4751 #ifdef AF_UNIX
4752         || family != AF_UNIX
4753 #endif
4754     ) {
4755         errno = EAFNOSUPPORT;
4756         return -1;
4757     }
4758     if (!fd) {
4759         errno = EINVAL;
4760         return -1;
4761     }
4762 
4763 #ifdef SOCK_CLOEXEC
4764     type &= ~SOCK_CLOEXEC;
4765 #endif
4766 
4767 #ifdef EMULATE_SOCKETPAIR_UDP
4768     if (type == SOCK_DGRAM)
4769         return S_socketpair_udp(fd);
4770 #endif
4771 
4772     aTHXa(PERL_GET_THX);
4773     listener = PerlSock_socket(AF_INET, type, 0);
4774     if (listener == -1)
4775         return -1;
4776     memset(&listen_addr, 0, sizeof(listen_addr));
4777     listen_addr.sin_family = AF_INET;
4778     listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
4779     listen_addr.sin_port = 0;	/* kernel choses port.  */
4780     if (PerlSock_bind(listener, (struct sockaddr *) &listen_addr,
4781             sizeof(listen_addr)) == -1)
4782         goto tidy_up_and_fail;
4783     if (PerlSock_listen(listener, 1) == -1)
4784         goto tidy_up_and_fail;
4785 
4786     connector = PerlSock_socket(AF_INET, type, 0);
4787     if (connector == -1)
4788         goto tidy_up_and_fail;
4789     /* We want to find out the port number to connect to.  */
4790     size = sizeof(connect_addr);
4791     if (PerlSock_getsockname(listener, (struct sockaddr *) &connect_addr,
4792             &size) == -1)
4793         goto tidy_up_and_fail;
4794     if (size != sizeof(connect_addr))
4795         goto abort_tidy_up_and_fail;
4796     if (PerlSock_connect(connector, (struct sockaddr *) &connect_addr,
4797             sizeof(connect_addr)) == -1)
4798         goto tidy_up_and_fail;
4799 
4800     size = sizeof(listen_addr);
4801     acceptor = PerlSock_accept(listener, (struct sockaddr *) &listen_addr,
4802             &size);
4803     if (acceptor == -1)
4804         goto tidy_up_and_fail;
4805     if (size != sizeof(listen_addr))
4806         goto abort_tidy_up_and_fail;
4807     PerlLIO_close(listener);
4808     /* Now check we are talking to ourself by matching port and host on the
4809        two sockets.  */
4810     if (PerlSock_getsockname(connector, (struct sockaddr *) &connect_addr,
4811             &size) == -1)
4812         goto tidy_up_and_fail;
4813     if (size != sizeof(connect_addr)
4814             || listen_addr.sin_family != connect_addr.sin_family
4815             || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
4816             || listen_addr.sin_port != connect_addr.sin_port) {
4817         goto abort_tidy_up_and_fail;
4818     }
4819     fd[0] = connector;
4820     fd[1] = acceptor;
4821     return 0;
4822 
4823   abort_tidy_up_and_fail:
4824 #ifdef ECONNABORTED
4825   errno = ECONNABORTED;	/* This would be the standard thing to do. */
4826 #elif defined(ECONNREFUSED)
4827   errno = ECONNREFUSED;	/* some OSes might not have ECONNABORTED. */
4828 #else
4829   errno = ETIMEDOUT;	/* Desperation time. */
4830 #endif
4831   tidy_up_and_fail:
4832     {
4833         dSAVE_ERRNO;
4834         if (listener != -1)
4835             PerlLIO_close(listener);
4836         if (connector != -1)
4837             PerlLIO_close(connector);
4838         if (acceptor != -1)
4839             PerlLIO_close(acceptor);
4840         RESTORE_ERRNO;
4841         return -1;
4842     }
4843 }
4844 #else
4845 /* In any case have a stub so that there's code corresponding
4846  * to the my_socketpair in embed.fnc. */
4847 int
4848 Perl_my_socketpair (int family, int type, int protocol, int fd[2]) {
4849 #ifdef HAS_SOCKETPAIR
4850     return socketpair(family, type, protocol, fd);
4851 #else
4852     return -1;
4853 #endif
4854 }
4855 #endif
4856 
4857 /*
4858 
4859 =for apidoc sv_nosharing
4860 
4861 Dummy routine which "shares" an SV when there is no sharing module present.
4862 Or "locks" it.  Or "unlocks" it.  In other
4863 words, ignores its single SV argument.
4864 Exists to avoid test for a C<NULL> function pointer and because it could
4865 potentially warn under some level of strict-ness.
4866 
4867 =cut
4868 */
4869 
4870 void
4871 Perl_sv_nosharing(pTHX_ SV *sv)
4872 {
4873     PERL_UNUSED_CONTEXT;
4874     PERL_UNUSED_ARG(sv);
4875 }
4876 
4877 /*
4878 
4879 =for apidoc sv_destroyable
4880 
4881 Dummy routine which reports that object can be destroyed when there is no
4882 sharing module present.  It ignores its single SV argument, and returns
4883 'true'.  Exists to avoid test for a C<NULL> function pointer and because it
4884 could potentially warn under some level of strict-ness.
4885 
4886 =cut
4887 */
4888 
4889 bool
4890 Perl_sv_destroyable(pTHX_ SV *sv)
4891 {
4892     PERL_UNUSED_CONTEXT;
4893     PERL_UNUSED_ARG(sv);
4894     return TRUE;
4895 }
4896 
4897 U32
4898 Perl_parse_unicode_opts(pTHX_ const char **popt)
4899 {
4900   const char *p = *popt;
4901   U32 opt = 0;
4902 
4903   PERL_ARGS_ASSERT_PARSE_UNICODE_OPTS;
4904 
4905   if (*p) {
4906        if (isDIGIT(*p)) {
4907             const char* endptr = p + strlen(p);
4908             UV uv;
4909             if (grok_atoUV(p, &uv, &endptr) && uv <= U32_MAX) {
4910                 opt = (U32)uv;
4911                 p = endptr;
4912                 if (p && *p && *p != '\n' && *p != '\r') {
4913                     if (isSPACE(*p))
4914                         goto the_end_of_the_opts_parser;
4915                     else
4916                         Perl_croak(aTHX_ "Unknown Unicode option letter '%c'", *p);
4917                 }
4918             }
4919             else {
4920                 Perl_croak(aTHX_ "Invalid number '%s' for -C option.\n", p);
4921             }
4922         }
4923         else {
4924             for (; *p; p++) {
4925                  switch (*p) {
4926                  case PERL_UNICODE_STDIN:
4927                       opt |= PERL_UNICODE_STDIN_FLAG;	break;
4928                  case PERL_UNICODE_STDOUT:
4929                       opt |= PERL_UNICODE_STDOUT_FLAG;	break;
4930                  case PERL_UNICODE_STDERR:
4931                       opt |= PERL_UNICODE_STDERR_FLAG;	break;
4932                  case PERL_UNICODE_STD:
4933                       opt |= PERL_UNICODE_STD_FLAG;    	break;
4934                  case PERL_UNICODE_IN:
4935                       opt |= PERL_UNICODE_IN_FLAG;	break;
4936                  case PERL_UNICODE_OUT:
4937                       opt |= PERL_UNICODE_OUT_FLAG;	break;
4938                  case PERL_UNICODE_INOUT:
4939                       opt |= PERL_UNICODE_INOUT_FLAG;	break;
4940                  case PERL_UNICODE_LOCALE:
4941                       opt |= PERL_UNICODE_LOCALE_FLAG;	break;
4942                  case PERL_UNICODE_ARGV:
4943                       opt |= PERL_UNICODE_ARGV_FLAG;	break;
4944                  case PERL_UNICODE_UTF8CACHEASSERT:
4945                       opt |= PERL_UNICODE_UTF8CACHEASSERT_FLAG; break;
4946                  default:
4947                       if (*p != '\n' && *p != '\r') {
4948                         if(isSPACE(*p)) goto the_end_of_the_opts_parser;
4949                         else
4950                           Perl_croak(aTHX_
4951                                      "Unknown Unicode option letter '%c'", *p);
4952                       }
4953                  }
4954             }
4955        }
4956   }
4957   else
4958        opt = PERL_UNICODE_DEFAULT_FLAGS;
4959 
4960   the_end_of_the_opts_parser:
4961 
4962   if (opt & ~PERL_UNICODE_ALL_FLAGS)
4963        Perl_croak(aTHX_ "Unknown Unicode option value %" UVuf,
4964                   (UV) (opt & ~PERL_UNICODE_ALL_FLAGS));
4965 
4966   *popt = p;
4967 
4968   return opt;
4969 }
4970 
4971 #ifdef VMS
4972 #  include <starlet.h>
4973 #endif
4974 
4975 U32
4976 Perl_seed(pTHX)
4977 {
4978 #if defined(__OpenBSD__)
4979 	return arc4random();
4980 #else
4981     /*
4982      * This is really just a quick hack which grabs various garbage
4983      * values.  It really should be a real hash algorithm which
4984      * spreads the effect of every input bit onto every output bit,
4985      * if someone who knows about such things would bother to write it.
4986      * Might be a good idea to add that function to CORE as well.
4987      * No numbers below come from careful analysis or anything here,
4988      * except they are primes and SEED_C1 > 1E6 to get a full-width
4989      * value from (tv_sec * SEED_C1 + tv_usec).  The multipliers should
4990      * probably be bigger too.
4991      */
4992 #if RANDBITS > 16
4993 #  define SEED_C1	1000003
4994 #define   SEED_C4	73819
4995 #else
4996 #  define SEED_C1	25747
4997 #define   SEED_C4	20639
4998 #endif
4999 #define   SEED_C2	3
5000 #define   SEED_C3	269
5001 #define   SEED_C5	26107
5002 
5003 #ifndef PERL_NO_DEV_RANDOM
5004     int fd;
5005 #endif
5006     U32 u;
5007 #ifdef HAS_GETTIMEOFDAY
5008     struct timeval when;
5009 #else
5010     Time_t when;
5011 #endif
5012 
5013 /* This test is an escape hatch, this symbol isn't set by Configure. */
5014 #ifndef PERL_NO_DEV_RANDOM
5015 #ifndef PERL_RANDOM_DEVICE
5016    /* /dev/random isn't used by default because reads from it will block
5017     * if there isn't enough entropy available.  You can compile with
5018     * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
5019     * is enough real entropy to fill the seed. */
5020 #  ifdef __amigaos4__
5021 #    define PERL_RANDOM_DEVICE "RANDOM:SIZE=4"
5022 #  else
5023 #    define PERL_RANDOM_DEVICE "/dev/urandom"
5024 #  endif
5025 #endif
5026     fd = PerlLIO_open_cloexec(PERL_RANDOM_DEVICE, 0);
5027     if (fd != -1) {
5028         if (PerlLIO_read(fd, (void*)&u, sizeof u) != sizeof u)
5029             u = 0;
5030         PerlLIO_close(fd);
5031         if (u)
5032             return u;
5033     }
5034 #endif
5035 
5036 #ifdef HAS_GETTIMEOFDAY
5037     PerlProc_gettimeofday(&when,NULL);
5038     u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
5039 #else
5040     (void)time(&when);
5041     u = (U32)SEED_C1 * when;
5042 #endif
5043     u += SEED_C3 * (U32)PerlProc_getpid();
5044     u += SEED_C4 * (U32)PTR2UV(PL_stack_sp);
5045 #ifndef PLAN9           /* XXX Plan9 assembler chokes on this; fix needed  */
5046     u += SEED_C5 * (U32)PTR2UV(&when);
5047 #endif
5048     return u;
5049 #endif
5050 }
5051 
5052 void
5053 Perl_get_hash_seed(pTHX_ unsigned char * const seed_buffer)
5054 {
5055 #ifndef NO_PERL_HASH_ENV
5056     const char *env_pv;
5057 #endif
5058     unsigned long i;
5059 
5060     PERL_ARGS_ASSERT_GET_HASH_SEED;
5061 
5062     Zero(seed_buffer, PERL_HASH_SEED_BYTES, U8);
5063     Zero((U8*)PL_hash_state_w, PERL_HASH_STATE_BYTES, U8);
5064 
5065 #ifndef NO_PERL_HASH_ENV
5066     env_pv= PerlEnv_getenv("PERL_HASH_SEED");
5067 
5068     if ( env_pv )
5069     {
5070         if (DEBUG_h_TEST)
5071             PerlIO_printf(Perl_debug_log,"Got PERL_HASH_SEED=<%s>\n", env_pv);
5072         /* ignore leading spaces */
5073         while (isSPACE(*env_pv))
5074             env_pv++;
5075 #    ifdef USE_PERL_PERTURB_KEYS
5076         /* if they set it to "0" we disable key traversal randomization completely */
5077         if (strEQ(env_pv,"0")) {
5078             PL_hash_rand_bits_enabled= 0;
5079         } else {
5080             /* otherwise switch to deterministic mode */
5081             PL_hash_rand_bits_enabled= 2;
5082         }
5083 #    endif
5084         /* ignore a leading 0x... if it is there */
5085         if (env_pv[0] == '0' && env_pv[1] == 'x')
5086             env_pv += 2;
5087 
5088         for( i = 0; isXDIGIT(*env_pv) && i < PERL_HASH_SEED_BYTES; i++ ) {
5089             seed_buffer[i] = READ_XDIGIT(env_pv) << 4;
5090             if ( isXDIGIT(*env_pv)) {
5091                 seed_buffer[i] |= READ_XDIGIT(env_pv);
5092             }
5093         }
5094         while (isSPACE(*env_pv))
5095             env_pv++;
5096 
5097         if (*env_pv && !isXDIGIT(*env_pv)) {
5098             Perl_warn(aTHX_ "perl: warning: Non hex character in '$ENV{PERL_HASH_SEED}', seed only partially set\n");
5099         }
5100         /* should we check for unparsed crap? */
5101         /* should we warn about unused hex? */
5102         /* should we warn about insufficient hex? */
5103     }
5104     else
5105 #endif /* NO_PERL_HASH_ENV */
5106     {
5107         for( i = 0; i < PERL_HASH_SEED_BYTES; i++ ) {
5108             seed_buffer[i] = (unsigned char)(Perl_internal_drand48() * (U8_MAX+1));
5109         }
5110     }
5111 #ifdef USE_PERL_PERTURB_KEYS
5112 #  ifndef NO_PERL_HASH_ENV
5113     env_pv= PerlEnv_getenv("PERL_PERTURB_KEYS");
5114     if (env_pv) {
5115         if (DEBUG_h_TEST)
5116             PerlIO_printf(Perl_debug_log,
5117                 "Got PERL_PERTURB_KEYS=<%s>\n", env_pv);
5118         if (strEQ(env_pv,"0") || strEQ(env_pv,"NO")) {
5119             PL_hash_rand_bits_enabled= 0;
5120         } else if (strEQ(env_pv,"1") || strEQ(env_pv,"RANDOM")) {
5121             PL_hash_rand_bits_enabled= 1;
5122         } else if (strEQ(env_pv,"2") || strEQ(env_pv,"DETERMINISTIC")) {
5123             PL_hash_rand_bits_enabled= 2;
5124         } else {
5125             Perl_warn(aTHX_ "perl: warning: strange setting in '$ENV{PERL_PERTURB_KEYS}': '%s'\n", env_pv);
5126         }
5127     }
5128 #  endif
5129     {   /* initialize PL_hash_rand_bits from the hash seed.
5130          * This value is highly volatile, it is updated every
5131          * hash insert, and is used as part of hash bucket chain
5132          * randomization and hash iterator randomization. */
5133         if (PL_hash_rand_bits_enabled == 1) {
5134             /* random mode initialize from seed() like we would our RNG() */
5135             PL_hash_rand_bits= seed();
5136         }
5137         else {
5138             /* Use a constant */
5139             PL_hash_rand_bits= 0xbe49d17f; /* I just picked a number */
5140             /* and then mix in the leading bytes of the hash seed */
5141             for( i = 0; i < sizeof(UV) ; i++ ) {
5142                 PL_hash_rand_bits ^= seed_buffer[i % PERL_HASH_SEED_BYTES];
5143                 PL_hash_rand_bits = ROTL_UV(PL_hash_rand_bits,8);
5144             }
5145         }
5146         if (!PL_hash_rand_bits) {
5147             /* we use an XORSHIFT RNG to munge PL_hash_rand_bits,
5148              * which means it cannot be 0 or it will stay 0 for the
5149              * lifetime of the process, so if by some insane chance we
5150              * ended up with a 0 after the above initialization
5151              * then set it to this. This really should not happen, or
5152              * very very very rarely.
5153              */
5154             PL_hash_rand_bits = 0x8110ba9d; /* a randomly chosen prime */
5155         }
5156     }
5157 #endif
5158 }
5159 
5160 void
5161 Perl_debug_hash_seed(pTHX_ bool via_debug_h)
5162 {
5163     PERL_ARGS_ASSERT_DEBUG_HASH_SEED;
5164 #if (defined(USE_HASH_SEED) || defined(USE_HASH_SEED_DEBUG)) && !defined(NO_PERL_HASH_SEED_DEBUG)
5165     {
5166         const char * const s = PerlEnv_getenv("PERL_HASH_SEED_DEBUG");
5167         bool via_env = cBOOL(s && strNE(s, "0") && strNE(s,""));
5168 
5169         if ( via_env != via_debug_h ) {
5170             const unsigned char *seed= PERL_HASH_SEED;
5171             const unsigned char *seed_end= PERL_HASH_SEED + PERL_HASH_SEED_BYTES;
5172             PerlIO_printf(Perl_debug_log, "HASH_FUNCTION = %s HASH_SEED = 0x", PERL_HASH_FUNC);
5173             while (seed < seed_end) {
5174                 PerlIO_printf(Perl_debug_log, "%02x", *seed++);
5175             }
5176 #ifdef PERL_HASH_RANDOMIZE_KEYS
5177             PerlIO_printf(Perl_debug_log, " PERTURB_KEYS = %d (%s)",
5178                     PL_HASH_RAND_BITS_ENABLED,
5179                     PL_HASH_RAND_BITS_ENABLED == 0 ? "NO" :
5180                     PL_HASH_RAND_BITS_ENABLED == 1 ? "RANDOM"
5181                                                    : "DETERMINISTIC");
5182             if (DEBUG_h_TEST)
5183                 PerlIO_printf(Perl_debug_log,
5184                         " RAND_BITS=0x%" UVxf, PL_hash_rand_bits);
5185 #endif
5186             PerlIO_printf(Perl_debug_log, "\n");
5187         }
5188     }
5189 #endif /* #if (defined(USE_HASH_SEED) ... */
5190 }
5191 
5192 
5193 
5194 
5195 #ifdef PERL_MEM_LOG
5196 
5197 /* -DPERL_MEM_LOG: the Perl_mem_log_..() is compiled, including
5198  * the default implementation, unless -DPERL_MEM_LOG_NOIMPL is also
5199  * given, and you supply your own implementation.
5200  *
5201  * The default implementation reads a single env var, PERL_MEM_LOG,
5202  * expecting one or more of the following:
5203  *
5204  *    \d+ - fd		fd to write to		: must be 1st (grok_atoUV)
5205  *    'm' - memlog	was PERL_MEM_LOG=1
5206  *    's' - svlog	was PERL_SV_LOG=1
5207  *    't' - timestamp	was PERL_MEM_LOG_TIMESTAMP=1
5208  *
5209  * This makes the logger controllable enough that it can reasonably be
5210  * added to the system perl.
5211  */
5212 
5213 /* -DPERL_MEM_LOG_SPRINTF_BUF_SIZE=X: size of a (stack-allocated) buffer
5214  * the Perl_mem_log_...() will use (either via sprintf or snprintf).
5215  */
5216 #define PERL_MEM_LOG_SPRINTF_BUF_SIZE 128
5217 
5218 /* -DPERL_MEM_LOG_FD=N: the file descriptor the Perl_mem_log_...()
5219  * writes to.  In the default logger, this is settable at runtime.
5220  */
5221 #ifndef PERL_MEM_LOG_FD
5222 #  define PERL_MEM_LOG_FD 2 /* If STDERR is too boring for you. */
5223 #endif
5224 
5225 #ifndef PERL_MEM_LOG_NOIMPL
5226 
5227 # ifdef DEBUG_LEAKING_SCALARS
5228 #   define SV_LOG_SERIAL_FMT	    " [%lu]"
5229 #   define _SV_LOG_SERIAL_ARG(sv)   , (unsigned long) (sv)->sv_debug_serial
5230 # else
5231 #   define SV_LOG_SERIAL_FMT
5232 #   define _SV_LOG_SERIAL_ARG(sv)
5233 # endif
5234 
5235 static void
5236 S_mem_log_common(enum mem_log_type mlt, const UV n,
5237                  const UV typesize, const char *type_name, const SV *sv,
5238                  Malloc_t oldalloc, Malloc_t newalloc,
5239                  const char *filename, const int linenumber,
5240                  const char *funcname)
5241 {
5242     const char *pmlenv;
5243     dTHX;
5244 
5245     PERL_ARGS_ASSERT_MEM_LOG_COMMON;
5246 
5247     PL_mem_log[0] |= 0x2;   /* Flag that the call is from this code */
5248     pmlenv = PerlEnv_getenv("PERL_MEM_LOG");
5249     PL_mem_log[0] &= ~0x2;
5250     if (!pmlenv)
5251         return;
5252     if (mlt < MLT_NEW_SV ? strchr(pmlenv,'m') : strchr(pmlenv,'s'))
5253     {
5254         /* We can't use SVs or PerlIO for obvious reasons,
5255          * so we'll use stdio and low-level IO instead. */
5256         char buf[PERL_MEM_LOG_SPRINTF_BUF_SIZE];
5257 
5258 #   ifdef HAS_GETTIMEOFDAY
5259 #     define MEM_LOG_TIME_FMT	"%10d.%06d: "
5260 #     define MEM_LOG_TIME_ARG	(int)tv.tv_sec, (int)tv.tv_usec
5261         struct timeval tv;
5262         PerlProc_gettimeofday(&tv, 0);
5263 #   else
5264 #     define MEM_LOG_TIME_FMT	"%10d: "
5265 #     define MEM_LOG_TIME_ARG	(int)when
5266         Time_t when;
5267         (void)time(&when);
5268 #   endif
5269         /* If there are other OS specific ways of hires time than
5270          * gettimeofday() (see dist/Time-HiRes), the easiest way is
5271          * probably that they would be used to fill in the struct
5272          * timeval. */
5273         {
5274             STRLEN len;
5275             const char* endptr = pmlenv + strlen(pmlenv);
5276             int fd;
5277             UV uv;
5278             if (grok_atoUV(pmlenv, &uv, &endptr) /* Ignore endptr. */
5279                 && uv && uv <= PERL_INT_MAX
5280             ) {
5281                 fd = (int)uv;
5282             } else {
5283                 fd = PERL_MEM_LOG_FD;
5284             }
5285 
5286             if (strchr(pmlenv, 't')) {
5287                 len = my_snprintf(buf, sizeof(buf),
5288                                 MEM_LOG_TIME_FMT, MEM_LOG_TIME_ARG);
5289                 PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len));
5290             }
5291             switch (mlt) {
5292             case MLT_ALLOC:
5293                 len = my_snprintf(buf, sizeof(buf),
5294                         "alloc: %s:%d:%s: %" IVdf " %" UVuf
5295                         " %s = %" IVdf ": %" UVxf "\n",
5296                         filename, linenumber, funcname, n, typesize,
5297                         type_name, n * typesize, PTR2UV(newalloc));
5298                 break;
5299             case MLT_REALLOC:
5300                 len = my_snprintf(buf, sizeof(buf),
5301                         "realloc: %s:%d:%s: %" IVdf " %" UVuf
5302                         " %s = %" IVdf ": %" UVxf " -> %" UVxf "\n",
5303                         filename, linenumber, funcname, n, typesize,
5304                         type_name, n * typesize, PTR2UV(oldalloc),
5305                         PTR2UV(newalloc));
5306                 break;
5307             case MLT_FREE:
5308                 len = my_snprintf(buf, sizeof(buf),
5309                         "free: %s:%d:%s: %" UVxf "\n",
5310                         filename, linenumber, funcname,
5311                         PTR2UV(oldalloc));
5312                 break;
5313             case MLT_NEW_SV:
5314             case MLT_DEL_SV:
5315                 len = my_snprintf(buf, sizeof(buf),
5316                         "%s_SV: %s:%d:%s: %" UVxf SV_LOG_SERIAL_FMT "\n",
5317                         mlt == MLT_NEW_SV ? "new" : "del",
5318                         filename, linenumber, funcname,
5319                         PTR2UV(sv) _SV_LOG_SERIAL_ARG(sv));
5320                 break;
5321             default:
5322                 len = 0;
5323             }
5324             PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len));
5325         }
5326     }
5327 }
5328 #endif /* !PERL_MEM_LOG_NOIMPL */
5329 
5330 #ifndef PERL_MEM_LOG_NOIMPL
5331 # define \
5332     mem_log_common_if(alty, num, tysz, tynm, sv, oal, nal, flnm, ln, fnnm) \
5333     mem_log_common   (alty, num, tysz, tynm, sv, oal, nal, flnm, ln, fnnm)
5334 #else
5335 /* this is suboptimal, but bug compatible.  User is providing their
5336    own implementation, but is getting these functions anyway, and they
5337    do nothing. But _NOIMPL users should be able to cope or fix */
5338 # define \
5339     mem_log_common_if(alty, num, tysz, tynm, u, oal, nal, flnm, ln, fnnm) \
5340     /* mem_log_common_if_PERL_MEM_LOG_NOIMPL */
5341 #endif
5342 
5343 Malloc_t
5344 Perl_mem_log_alloc(const UV n, const UV typesize, const char *type_name,
5345                    Malloc_t newalloc,
5346                    const char *filename, const int linenumber,
5347                    const char *funcname)
5348 {
5349     PERL_ARGS_ASSERT_MEM_LOG_ALLOC;
5350 
5351     mem_log_common_if(MLT_ALLOC, n, typesize, type_name,
5352                       NULL, NULL, newalloc,
5353                       filename, linenumber, funcname);
5354     return newalloc;
5355 }
5356 
5357 Malloc_t
5358 Perl_mem_log_realloc(const UV n, const UV typesize, const char *type_name,
5359                      Malloc_t oldalloc, Malloc_t newalloc,
5360                      const char *filename, const int linenumber,
5361                      const char *funcname)
5362 {
5363     PERL_ARGS_ASSERT_MEM_LOG_REALLOC;
5364 
5365     mem_log_common_if(MLT_REALLOC, n, typesize, type_name,
5366                       NULL, oldalloc, newalloc,
5367                       filename, linenumber, funcname);
5368     return newalloc;
5369 }
5370 
5371 Malloc_t
5372 Perl_mem_log_free(Malloc_t oldalloc,
5373                   const char *filename, const int linenumber,
5374                   const char *funcname)
5375 {
5376     PERL_ARGS_ASSERT_MEM_LOG_FREE;
5377 
5378     mem_log_common_if(MLT_FREE, 0, 0, "", NULL, oldalloc, NULL,
5379                       filename, linenumber, funcname);
5380     return oldalloc;
5381 }
5382 
5383 void
5384 Perl_mem_log_new_sv(const SV *sv,
5385                     const char *filename, const int linenumber,
5386                     const char *funcname)
5387 {
5388     PERL_ARGS_ASSERT_MEM_LOG_NEW_SV;
5389 
5390     mem_log_common_if(MLT_NEW_SV, 0, 0, "", sv, NULL, NULL,
5391                       filename, linenumber, funcname);
5392 }
5393 
5394 void
5395 Perl_mem_log_del_sv(const SV *sv,
5396                     const char *filename, const int linenumber,
5397                     const char *funcname)
5398 {
5399     PERL_ARGS_ASSERT_MEM_LOG_DEL_SV;
5400 
5401     mem_log_common_if(MLT_DEL_SV, 0, 0, "", sv, NULL, NULL,
5402                       filename, linenumber, funcname);
5403 }
5404 
5405 #endif /* PERL_MEM_LOG */
5406 
5407 /*
5408 =for apidoc_section $string
5409 =for apidoc quadmath_format_valid
5410 
5411 C<quadmath_snprintf()> is very strict about its C<format> string and will
5412 fail, returning -1, if the format is invalid.  It accepts exactly
5413 one format spec.
5414 
5415 C<quadmath_format_valid()> checks that the intended single spec looks
5416 sane: begins with C<%>, has only one C<%>, ends with C<[efgaEFGA]>,
5417 and has C<Q> before it.  This is not a full "printf syntax check",
5418 just the basics.
5419 
5420 Returns true if it is valid, false if not.
5421 
5422 See also L</quadmath_format_needed>.
5423 
5424 =cut
5425 */
5426 #ifdef USE_QUADMATH
5427 bool
5428 Perl_quadmath_format_valid(const char* format)
5429 {
5430     STRLEN len;
5431 
5432     PERL_ARGS_ASSERT_QUADMATH_FORMAT_VALID;
5433 
5434     if (format[0] != '%' || strchr(format + 1, '%'))
5435         return FALSE;
5436     len = strlen(format);
5437     /* minimum length three: %Qg */
5438     if (len < 3 || memCHRs("efgaEFGA", format[len - 1]) == NULL)
5439         return FALSE;
5440     if (format[len - 2] != 'Q')
5441         return FALSE;
5442     return TRUE;
5443 }
5444 #endif
5445 
5446 /*
5447 =for apidoc quadmath_format_needed
5448 
5449 C<quadmath_format_needed()> returns true if the C<format> string seems to
5450 contain at least one non-Q-prefixed C<%[efgaEFGA]> format specifier,
5451 or returns false otherwise.
5452 
5453 The format specifier detection is not complete printf-syntax detection,
5454 but it should catch most common cases.
5455 
5456 If true is returned, those arguments B<should> in theory be processed
5457 with C<quadmath_snprintf()>, but in case there is more than one such
5458 format specifier (see L</quadmath_format_valid>), and if there is
5459 anything else beyond that one (even just a single byte), they
5460 B<cannot> be processed because C<quadmath_snprintf()> is very strict,
5461 accepting only one format spec, and nothing else.
5462 In this case, the code should probably fail.
5463 
5464 =cut
5465 */
5466 #ifdef USE_QUADMATH
5467 bool
5468 Perl_quadmath_format_needed(const char* format)
5469 {
5470   const char *p = format;
5471   const char *q;
5472 
5473   PERL_ARGS_ASSERT_QUADMATH_FORMAT_NEEDED;
5474 
5475   while ((q = strchr(p, '%'))) {
5476     q++;
5477     if (*q == '+') /* plus */
5478       q++;
5479     if (*q == '#') /* alt */
5480       q++;
5481     if (*q == '*') /* width */
5482       q++;
5483     else {
5484       if (isDIGIT(*q)) {
5485         while (isDIGIT(*q)) q++;
5486       }
5487     }
5488     if (*q == '.' && (q[1] == '*' || isDIGIT(q[1]))) { /* prec */
5489       q++;
5490       if (*q == '*')
5491         q++;
5492       else
5493         while (isDIGIT(*q)) q++;
5494     }
5495     if (memCHRs("efgaEFGA", *q)) /* Would have needed 'Q' in front. */
5496       return TRUE;
5497     p = q + 1;
5498   }
5499   return FALSE;
5500 }
5501 #endif
5502 
5503 /*
5504 =for apidoc my_snprintf
5505 
5506 The C library C<snprintf> functionality, if available and
5507 standards-compliant (uses C<vsnprintf>, actually).  However, if the
5508 C<vsnprintf> is not available, will unfortunately use the unsafe
5509 C<vsprintf> which can overrun the buffer (there is an overrun check,
5510 but that may be too late).  Consider using C<sv_vcatpvf> instead, or
5511 getting C<vsnprintf>.
5512 
5513 =cut
5514 */
5515 int
5516 Perl_my_snprintf(char *buffer, const Size_t len, const char *format, ...)
5517 {
5518     int retval = -1;
5519     va_list ap;
5520     PERL_ARGS_ASSERT_MY_SNPRINTF;
5521 #ifndef HAS_VSNPRINTF
5522     PERL_UNUSED_VAR(len);
5523 #endif
5524     va_start(ap, format);
5525 #ifdef USE_QUADMATH
5526     {
5527         bool quadmath_valid = FALSE;
5528         if (quadmath_format_valid(format)) {
5529             /* If the format looked promising, use it as quadmath. */
5530             retval = quadmath_snprintf(buffer, len, format, va_arg(ap, NV));
5531             if (retval == -1) {
5532                 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", format);
5533             }
5534             quadmath_valid = TRUE;
5535         }
5536         /* quadmath_format_single() will return false for example for
5537          * "foo = %g", or simply "%g".  We could handle the %g by
5538          * using quadmath for the NV args.  More complex cases of
5539          * course exist: "foo = %g, bar = %g", or "foo=%Qg" (otherwise
5540          * quadmath-valid but has stuff in front).
5541          *
5542          * Handling the "Q-less" cases right would require walking
5543          * through the va_list and rewriting the format, calling
5544          * quadmath for the NVs, building a new va_list, and then
5545          * letting vsnprintf/vsprintf to take care of the other
5546          * arguments.  This may be doable.
5547          *
5548          * We do not attempt that now.  But for paranoia, we here try
5549          * to detect some common (but not all) cases where the
5550          * "Q-less" %[efgaEFGA] formats are present, and die if
5551          * detected.  This doesn't fix the problem, but it stops the
5552          * vsnprintf/vsprintf pulling doubles off the va_list when
5553          * __float128 NVs should be pulled off instead.
5554          *
5555          * If quadmath_format_needed() returns false, we are reasonably
5556          * certain that we can call vnsprintf() or vsprintf() safely. */
5557         if (!quadmath_valid && quadmath_format_needed(format))
5558           Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", format);
5559 
5560     }
5561 #endif
5562     if (retval == -1)
5563 #ifdef HAS_VSNPRINTF
5564         retval = vsnprintf(buffer, len, format, ap);
5565 #else
5566         retval = vsprintf(buffer, format, ap);
5567 #endif
5568     va_end(ap);
5569     /* vsprintf() shows failure with < 0 */
5570     if (retval < 0
5571 #ifdef HAS_VSNPRINTF
5572     /* vsnprintf() shows failure with >= len */
5573         ||
5574         (len > 0 && (Size_t)retval >= len)
5575 #endif
5576     )
5577         Perl_croak_nocontext("panic: my_snprintf buffer overflow");
5578     return retval;
5579 }
5580 
5581 /*
5582 =for apidoc my_vsnprintf
5583 
5584 The C library C<vsnprintf> if available and standards-compliant.
5585 However, if the C<vsnprintf> is not available, will unfortunately
5586 use the unsafe C<vsprintf> which can overrun the buffer (there is an
5587 overrun check, but that may be too late).  Consider using
5588 C<sv_vcatpvf> instead, or getting C<vsnprintf>.
5589 
5590 =cut
5591 */
5592 int
5593 Perl_my_vsnprintf(char *buffer, const Size_t len, const char *format, va_list ap)
5594 {
5595 #ifdef USE_QUADMATH
5596     PERL_UNUSED_ARG(buffer);
5597     PERL_UNUSED_ARG(len);
5598     PERL_UNUSED_ARG(format);
5599     /* the cast is to avoid gcc -Wsizeof-array-argument complaining */
5600     PERL_UNUSED_ARG((void*)ap);
5601     Perl_croak_nocontext("panic: my_vsnprintf not available with quadmath");
5602     return 0;
5603 #else
5604     int retval;
5605 #ifdef NEED_VA_COPY
5606     va_list apc;
5607 
5608     PERL_ARGS_ASSERT_MY_VSNPRINTF;
5609     Perl_va_copy(ap, apc);
5610 # ifdef HAS_VSNPRINTF
5611     retval = vsnprintf(buffer, len, format, apc);
5612 # else
5613     PERL_UNUSED_ARG(len);
5614     retval = vsprintf(buffer, format, apc);
5615 # endif
5616     va_end(apc);
5617 #else
5618 # ifdef HAS_VSNPRINTF
5619     retval = vsnprintf(buffer, len, format, ap);
5620 # else
5621     PERL_UNUSED_ARG(len);
5622     retval = vsprintf(buffer, format, ap);
5623 # endif
5624 #endif /* #ifdef NEED_VA_COPY */
5625     /* vsprintf() shows failure with < 0 */
5626     if (retval < 0
5627 #ifdef HAS_VSNPRINTF
5628     /* vsnprintf() shows failure with >= len */
5629         ||
5630         (len > 0 && (Size_t)retval >= len)
5631 #endif
5632     )
5633         Perl_croak_nocontext("panic: my_vsnprintf buffer overflow");
5634     return retval;
5635 #endif
5636 }
5637 
5638 void
5639 Perl_my_clearenv(pTHX)
5640 {
5641 #if ! defined(PERL_MICRO)
5642 #  if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
5643     PerlEnv_clearenv();
5644 #  else /* ! (PERL_IMPLICIT_SYS || WIN32) */
5645 #    if defined(USE_ENVIRON_ARRAY)
5646 #      if defined(USE_ITHREADS)
5647     /* only the parent thread can clobber the process environment, so no need
5648      * to use a mutex */
5649     if (PL_curinterp == aTHX)
5650 #      endif /* USE_ITHREADS */
5651     {
5652 #      if ! defined(PERL_USE_SAFE_PUTENV)
5653     if ( !PL_use_safe_putenv) {
5654       I32 i;
5655       if (environ == PL_origenviron)
5656         environ = (char**)safesysmalloc(sizeof(char*));
5657       else
5658         for (i = 0; environ[i]; i++)
5659           (void)safesysfree(environ[i]);
5660     }
5661     environ[0] = NULL;
5662 #      else /* PERL_USE_SAFE_PUTENV */
5663 #        if defined(HAS_CLEARENV)
5664     (void)clearenv();
5665 #        elif defined(HAS_UNSETENV)
5666     int bsiz = 80; /* Most envvar names will be shorter than this. */
5667     char *buf = (char*)safesysmalloc(bsiz);
5668     while (*environ != NULL) {
5669       char *e = strchr(*environ, '=');
5670       int l = e ? e - *environ : (int)strlen(*environ);
5671       if (bsiz < l + 1) {
5672         (void)safesysfree(buf);
5673         bsiz = l + 1; /* + 1 for the \0. */
5674         buf = (char*)safesysmalloc(bsiz);
5675       }
5676       memcpy(buf, *environ, l);
5677       buf[l] = '\0';
5678       (void)unsetenv(buf);
5679     }
5680     (void)safesysfree(buf);
5681 #        else /* ! HAS_CLEARENV && ! HAS_UNSETENV */
5682     /* Just null environ and accept the leakage. */
5683     *environ = NULL;
5684 #        endif /* HAS_CLEARENV || HAS_UNSETENV */
5685 #      endif /* ! PERL_USE_SAFE_PUTENV */
5686     }
5687 #    endif /* USE_ENVIRON_ARRAY */
5688 #  endif /* PERL_IMPLICIT_SYS || WIN32 */
5689 #endif /* PERL_MICRO */
5690 }
5691 
5692 #ifdef MULTIPLICITY
5693 
5694 /*
5695 =for apidoc my_cxt_init
5696 
5697 Implements the L<perlxs/C<MY_CXT_INIT>> macro, which you should use instead.
5698 
5699 The first time a module is loaded, the global C<PL_my_cxt_index> is incremented,
5700 and that value is assigned to that module's static C<my_cxt_index> (whose
5701 address is passed as an arg).  Then, for each interpreter this function is
5702 called for, it makes sure a C<void*> slot is available to hang the static data
5703 off, by allocating or extending the interpreter's C<PL_my_cxt_list> array
5704 
5705 =cut
5706 */
5707 
5708 void *
5709 Perl_my_cxt_init(pTHX_ int *indexp, size_t size)
5710 {
5711     void *p;
5712     int index;
5713 
5714     PERL_ARGS_ASSERT_MY_CXT_INIT;
5715 
5716     index = *indexp;
5717     /* do initial check without locking.
5718      * -1:    not allocated or another thread currently allocating
5719      *  other: already allocated by another thread
5720      */
5721     if (index == -1) {
5722         MUTEX_LOCK(&PL_my_ctx_mutex);
5723         /*now a stricter check with locking */
5724         index = *indexp;
5725         if (index == -1)
5726             /* this module hasn't been allocated an index yet */
5727             *indexp = PL_my_cxt_index++;
5728         index = *indexp;
5729         MUTEX_UNLOCK(&PL_my_ctx_mutex);
5730     }
5731 
5732     /* make sure the array is big enough */
5733     if (PL_my_cxt_size <= index) {
5734         if (PL_my_cxt_size) {
5735             IV new_size = PL_my_cxt_size;
5736             while (new_size <= index)
5737                 new_size *= 2;
5738             Renew(PL_my_cxt_list, new_size, void *);
5739             PL_my_cxt_size = new_size;
5740         }
5741         else {
5742             PL_my_cxt_size = 16;
5743             Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
5744         }
5745     }
5746     /* newSV() allocates one more than needed */
5747     p = (void*)SvPVX(newSV(size-1));
5748     PL_my_cxt_list[index] = p;
5749     Zero(p, size, char);
5750     return p;
5751 }
5752 
5753 #endif /* MULTIPLICITY */
5754 
5755 
5756 /* Perl_xs_handshake():
5757    implement the various XS_*_BOOTCHECK macros, which are added to .c
5758    files by ExtUtils::ParseXS, to check that the perl the module was built
5759    with is binary compatible with the running perl.
5760 
5761    usage:
5762        Perl_xs_handshake(U32 key, void * v_my_perl, const char * file,
5763             [U32 items, U32 ax], [char * api_version], [char * xs_version])
5764 
5765    The meaning of the varargs is determined the U32 key arg (which is not
5766    a format string). The fields of key are assembled by using HS_KEY().
5767 
5768    Under PERL_IMPLICIT_CONTEX, the v_my_perl arg is of type
5769    "PerlInterpreter *" and represents the callers context; otherwise it is
5770    of type "CV *", and is the boot xsub's CV.
5771 
5772    v_my_perl will catch where a threaded future perl526.dll calling IO.dll
5773    for example, and IO.dll was linked with threaded perl524.dll, and both
5774    perl526.dll and perl524.dll are in %PATH and the Win32 DLL loader
5775    successfully can load IO.dll into the process but simultaneously it
5776    loaded an interpreter of a different version into the process, and XS
5777    code will naturally pass SV*s created by perl524.dll for perl526.dll to
5778    use through perl526.dll's my_perl->Istack_base.
5779 
5780    v_my_perl cannot be the first arg, since then 'key' will be out of
5781    place in a threaded vs non-threaded mixup; and analyzing the key
5782    number's bitfields won't reveal the problem, since it will be a valid
5783    key (unthreaded perl) on interp side, but croak will report the XS mod's
5784    key as gibberish (it is really a my_perl ptr) (threaded XS mod); or if
5785    it's a threaded perl and an unthreaded XS module, threaded perl will
5786    look at an uninit C stack or an uninit register to get 'key'
5787    (remember that it assumes that the 1st arg is the interp cxt).
5788 
5789    'file' is the source filename of the caller.
5790 */
5791 
5792 I32
5793 Perl_xs_handshake(const U32 key, void * v_my_perl, const char * file, ...)
5794 {
5795     va_list args;
5796     U32 items, ax;
5797     void * got;
5798     void * need;
5799     const char *stage = "first";
5800 #ifdef MULTIPLICITY
5801     dTHX;
5802     tTHX xs_interp;
5803 #else
5804     CV* cv;
5805     SV *** xs_spp;
5806 #endif
5807     PERL_ARGS_ASSERT_XS_HANDSHAKE;
5808     va_start(args, file);
5809 
5810     got = INT2PTR(void*, (UV)(key & HSm_KEY_MATCH));
5811     need = (void *)(HS_KEY(FALSE, FALSE, "", "") & HSm_KEY_MATCH);
5812     if (UNLIKELY(got != need))
5813         goto bad_handshake;
5814 /* try to catch where a 2nd threaded perl interp DLL is loaded into a process
5815    by a XS DLL compiled against the wrong interl DLL b/c of bad @INC, and the
5816    2nd threaded perl interp DLL never initialized its TLS/PERL_SYS_INIT3 so
5817    dTHX call from 2nd interp DLL can't return the my_perl that pp_entersub
5818    passed to the XS DLL */
5819 #ifdef MULTIPLICITY
5820     xs_interp = (tTHX)v_my_perl;
5821     got = xs_interp;
5822     need = my_perl;
5823 #else
5824 /* try to catch where an unthreaded perl interp DLL (for ex. perl522.dll) is
5825    loaded into a process by a XS DLL built by an unthreaded perl522.dll perl,
5826    but the DynaLoder/Perl that started the process and loaded the XS DLL is
5827    unthreaded perl524.dll, since unthreadeds don't pass my_perl (a unique *)
5828    through pp_entersub, use a unique value (which is a pointer to PL_stack_sp's
5829    location in the unthreaded perl binary) stored in CV * to figure out if this
5830    Perl_xs_handshake was called by the same pp_entersub */
5831     cv = (CV*)v_my_perl;
5832     xs_spp = (SV***)CvHSCXT(cv);
5833     got = xs_spp;
5834     need = &PL_stack_sp;
5835 #endif
5836     stage = "second";
5837     if(UNLIKELY(got != need)) {
5838         bad_handshake:/* recycle branch and string from above */
5839         if(got != (void *)HSf_NOCHK)
5840             noperl_die("%s: loadable library and perl binaries are mismatched"
5841                        " (got %s handshake key %p, needed %p)\n",
5842                        file, stage, got, need);
5843     }
5844 
5845     if(key & HSf_SETXSUBFN) {     /* this might be called from a module bootstrap */
5846         SAVEPPTR(PL_xsubfilename);/* which was require'd from a XSUB BEGIN */
5847         PL_xsubfilename = file;   /* so the old name must be restored for
5848                                      additional XSUBs to register themselves */
5849         /* XSUBs can't be perl lang/perl5db.pl debugged
5850         if (PERLDB_LINE_OR_SAVESRC)
5851             (void)gv_fetchfile(file); */
5852     }
5853 
5854     if(key & HSf_POPMARK) {
5855         ax = POPMARK;
5856         {   SV **mark = PL_stack_base + ax++;
5857             {   dSP;
5858                 items = (I32)(SP - MARK);
5859             }
5860         }
5861     } else {
5862         items = va_arg(args, U32);
5863         ax = va_arg(args, U32);
5864     }
5865     {
5866         U32 apiverlen;
5867         assert(HS_GETAPIVERLEN(key) <= UCHAR_MAX);
5868         if((apiverlen = HS_GETAPIVERLEN(key))) {
5869             char * api_p = va_arg(args, char*);
5870             if(apiverlen != sizeof("v" PERL_API_VERSION_STRING)-1
5871                 || memNE(api_p, "v" PERL_API_VERSION_STRING,
5872                          sizeof("v" PERL_API_VERSION_STRING)-1))
5873                 Perl_croak_nocontext("Perl API version %s of %" SVf " does not match %s",
5874                                     api_p, SVfARG(PL_stack_base[ax + 0]),
5875                                     "v" PERL_API_VERSION_STRING);
5876         }
5877     }
5878     {
5879         U32 xsverlen;
5880         assert(HS_GETXSVERLEN(key) <= UCHAR_MAX && HS_GETXSVERLEN(key) <= HS_APIVERLEN_MAX);
5881         if((xsverlen = HS_GETXSVERLEN(key)))
5882             S_xs_version_bootcheck(aTHX_
5883                 items, ax, va_arg(args, char*), xsverlen);
5884     }
5885     va_end(args);
5886     return ax;
5887 }
5888 
5889 
5890 STATIC void
5891 S_xs_version_bootcheck(pTHX_ U32 items, U32 ax, const char *xs_p,
5892                           STRLEN xs_len)
5893 {
5894     SV *sv;
5895     const char *vn = NULL;
5896     SV *const module = PL_stack_base[ax];
5897 
5898     PERL_ARGS_ASSERT_XS_VERSION_BOOTCHECK;
5899 
5900     if (items >= 2)	 /* version supplied as bootstrap arg */
5901         sv = PL_stack_base[ax + 1];
5902     else {
5903         /* XXX GV_ADDWARN */
5904         vn = "XS_VERSION";
5905         sv = get_sv(Perl_form(aTHX_ "%" SVf "::%s", SVfARG(module), vn), 0);
5906         if (!sv || !SvOK(sv)) {
5907             vn = "VERSION";
5908             sv = get_sv(Perl_form(aTHX_ "%" SVf "::%s", SVfARG(module), vn), 0);
5909         }
5910     }
5911     if (sv) {
5912         SV *xssv = Perl_newSVpvn_flags(aTHX_ xs_p, xs_len, SVs_TEMP);
5913         SV *pmsv = sv_isobject(sv) && sv_derived_from(sv, "version")
5914             ? sv : sv_2mortal(new_version(sv));
5915         xssv = upg_version(xssv, 0);
5916         if ( vcmp(pmsv,xssv) ) {
5917             SV *string = vstringify(xssv);
5918             SV *xpt = Perl_newSVpvf(aTHX_ "%" SVf " object version %" SVf
5919                                     " does not match ", SVfARG(module), SVfARG(string));
5920 
5921             SvREFCNT_dec(string);
5922             string = vstringify(pmsv);
5923 
5924             if (vn) {
5925                 Perl_sv_catpvf(aTHX_ xpt, "$%" SVf "::%s %" SVf, SVfARG(module), vn,
5926                                SVfARG(string));
5927             } else {
5928                 Perl_sv_catpvf(aTHX_ xpt, "bootstrap parameter %" SVf, SVfARG(string));
5929             }
5930             SvREFCNT_dec(string);
5931 
5932             Perl_sv_2mortal(aTHX_ xpt);
5933             Perl_croak_sv(aTHX_ xpt);
5934         }
5935     }
5936 }
5937 
5938 /*
5939 =for apidoc my_strlcat
5940 
5941 The C library C<strlcat> if available, or a Perl implementation of it.
5942 This operates on C C<NUL>-terminated strings.
5943 
5944 C<my_strlcat()> appends string C<src> to the end of C<dst>.  It will append at
5945 most S<C<size - strlen(dst) - 1>> characters.  It will then C<NUL>-terminate,
5946 unless C<size> is 0 or the original C<dst> string was longer than C<size> (in
5947 practice this should not happen as it means that either C<size> is incorrect or
5948 that C<dst> is not a proper C<NUL>-terminated string).
5949 
5950 Note that C<size> is the full size of the destination buffer and
5951 the result is guaranteed to be C<NUL>-terminated if there is room.  Note that
5952 room for the C<NUL> should be included in C<size>.
5953 
5954 The return value is the total length that C<dst> would have if C<size> is
5955 sufficiently large.  Thus it is the initial length of C<dst> plus the length of
5956 C<src>.  If C<size> is smaller than the return, the excess was not appended.
5957 
5958 =cut
5959 
5960 Description stolen from http://man.openbsd.org/strlcat.3
5961 */
5962 #ifndef HAS_STRLCAT
5963 Size_t
5964 Perl_my_strlcat(char *dst, const char *src, Size_t size)
5965 {
5966     Size_t used, length, copy;
5967 
5968     used = strlen(dst);
5969     length = strlen(src);
5970     if (size > 0 && used < size - 1) {
5971         copy = (length >= size - used) ? size - used - 1 : length;
5972         memcpy(dst + used, src, copy);
5973         dst[used + copy] = '\0';
5974     }
5975     return used + length;
5976 }
5977 #endif
5978 
5979 
5980 /*
5981 =for apidoc my_strlcpy
5982 
5983 The C library C<strlcpy> if available, or a Perl implementation of it.
5984 This operates on C C<NUL>-terminated strings.
5985 
5986 C<my_strlcpy()> copies up to S<C<size - 1>> characters from the string C<src>
5987 to C<dst>, C<NUL>-terminating the result if C<size> is not 0.
5988 
5989 The return value is the total length C<src> would be if the copy completely
5990 succeeded.  If it is larger than C<size>, the excess was not copied.
5991 
5992 =cut
5993 
5994 Description stolen from http://man.openbsd.org/strlcpy.3
5995 */
5996 #ifndef HAS_STRLCPY
5997 Size_t
5998 Perl_my_strlcpy(char *dst, const char *src, Size_t size)
5999 {
6000     Size_t length, copy;
6001 
6002     length = strlen(src);
6003     if (size > 0) {
6004         copy = (length >= size) ? size - 1 : length;
6005         memcpy(dst, src, copy);
6006         dst[copy] = '\0';
6007     }
6008     return length;
6009 }
6010 #endif
6011 
6012 PERL_STATIC_INLINE bool
6013 S_gv_has_usable_name(pTHX_ GV *gv)
6014 {
6015     GV **gvp;
6016     return GvSTASH(gv)
6017         && HvENAME(GvSTASH(gv))
6018         && (gvp = (GV **)hv_fetchhek(
6019                         GvSTASH(gv), GvNAME_HEK(gv), 0
6020            ))
6021         && *gvp == gv;
6022 }
6023 
6024 void
6025 Perl_get_db_sub(pTHX_ SV **svp, CV *cv)
6026 {
6027     SV * const dbsv = GvSVn(PL_DBsub);
6028     const bool save_taint = TAINT_get;
6029 
6030     /* When we are called from pp_goto (svp is null),
6031      * we do not care about using dbsv to call CV;
6032      * it's for informational purposes only.
6033      */
6034 
6035     PERL_ARGS_ASSERT_GET_DB_SUB;
6036 
6037     TAINT_set(FALSE);
6038     save_item(dbsv);
6039     if (!PERLDB_SUB_NN) {
6040         GV *gv = CvGV(cv);
6041 
6042         if (!svp && !CvLEXICAL(cv)) {
6043             gv_efullname3(dbsv, gv, NULL);
6044         }
6045         else if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED)) || CvLEXICAL(cv)
6046              || strEQ(GvNAME(gv), "END")
6047              || ( /* Could be imported, and old sub redefined. */
6048                  (GvCV(gv) != cv || !S_gv_has_usable_name(aTHX_ gv))
6049                  &&
6050                  !( (SvTYPE(*svp) == SVt_PVGV)
6051                     && (GvCV((const GV *)*svp) == cv)
6052                     /* Use GV from the stack as a fallback. */
6053                     && S_gv_has_usable_name(aTHX_ gv = (GV *)*svp)
6054                   )
6055                 )
6056         ) {
6057             /* GV is potentially non-unique, or contain different CV. */
6058             SV * const tmp = newRV(MUTABLE_SV(cv));
6059             sv_setsv(dbsv, tmp);
6060             SvREFCNT_dec(tmp);
6061         }
6062         else {
6063             sv_sethek(dbsv, HvENAME_HEK(GvSTASH(gv)));
6064             sv_catpvs(dbsv, "::");
6065             sv_cathek(dbsv, GvNAME_HEK(gv));
6066         }
6067     }
6068     else {
6069         const int type = SvTYPE(dbsv);
6070         if (type < SVt_PVIV && type != SVt_IV)
6071             sv_upgrade(dbsv, SVt_PVIV);
6072         (void)SvIOK_on(dbsv);
6073         SvIV_set(dbsv, PTR2IV(cv));	/* Do it the quickest way  */
6074     }
6075     SvSETMAGIC(dbsv);
6076     TAINT_IF(save_taint);
6077 #ifdef NO_TAINT_SUPPORT
6078     PERL_UNUSED_VAR(save_taint);
6079 #endif
6080 }
6081 
6082 /*
6083 =for apidoc_section $io
6084 =for apidoc my_dirfd
6085 
6086 The C library C<L<dirfd(3)>> if available, or a Perl implementation of it, or die
6087 if not easily emulatable.
6088 
6089 =cut
6090 */
6091 
6092 int
6093 Perl_my_dirfd(DIR * dir) {
6094 
6095     /* Most dirfd implementations have problems when passed NULL. */
6096     if(!dir)
6097         return -1;
6098 #ifdef HAS_DIRFD
6099     return dirfd(dir);
6100 #elif defined(HAS_DIR_DD_FD)
6101     return dir->dd_fd;
6102 #else
6103     Perl_croak_nocontext(PL_no_func, "dirfd");
6104     NOT_REACHED; /* NOTREACHED */
6105     return 0;
6106 #endif
6107 }
6108 
6109 #if !defined(HAS_MKOSTEMP) || !defined(HAS_MKSTEMP)
6110 
6111 #define TEMP_FILE_CH "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789"
6112 #define TEMP_FILE_CH_COUNT (sizeof(TEMP_FILE_CH)-1)
6113 
6114 static int
6115 S_my_mkostemp(char *templte, int flags) {
6116     dTHX;
6117     STRLEN len = strlen(templte);
6118     int fd;
6119     int attempts = 0;
6120 #ifdef VMS
6121     int delete_on_close = flags & O_VMS_DELETEONCLOSE;
6122 
6123     flags &= ~O_VMS_DELETEONCLOSE;
6124 #endif
6125 
6126     if (len < 6 ||
6127         templte[len-1] != 'X' || templte[len-2] != 'X' || templte[len-3] != 'X' ||
6128         templte[len-4] != 'X' || templte[len-5] != 'X' || templte[len-6] != 'X') {
6129         SETERRNO(EINVAL, LIB_INVARG);
6130         return -1;
6131     }
6132 
6133     do {
6134         int i;
6135         for (i = 1; i <= 6; ++i) {
6136             templte[len-i] = TEMP_FILE_CH[(int)(Perl_internal_drand48() * TEMP_FILE_CH_COUNT)];
6137         }
6138 #ifdef VMS
6139         if (delete_on_close) {
6140             fd = open(templte, O_RDWR | O_CREAT | O_EXCL | flags, 0600, "fop=dlt");
6141         }
6142         else
6143 #endif
6144         {
6145             fd = PerlLIO_open3(templte, O_RDWR | O_CREAT | O_EXCL | flags, 0600);
6146         }
6147     } while (fd == -1 && errno == EEXIST && ++attempts <= 100);
6148 
6149     return fd;
6150 }
6151 
6152 #endif
6153 
6154 #ifndef HAS_MKOSTEMP
6155 
6156 /*
6157 =for apidoc my_mkostemp
6158 
6159 The C library C<L<mkostemp(3)>> if available, or a Perl implementation of it.
6160 
6161 =cut
6162 */
6163 
6164 int
6165 Perl_my_mkostemp(char *templte, int flags)
6166 {
6167     PERL_ARGS_ASSERT_MY_MKOSTEMP;
6168     return S_my_mkostemp(templte, flags);
6169 }
6170 #endif
6171 
6172 #ifndef HAS_MKSTEMP
6173 
6174 /*
6175 =for apidoc my_mkstemp
6176 
6177 The C library C<L<mkstemp(3)>> if available, or a Perl implementation of it.
6178 
6179 =cut
6180 */
6181 
6182 int
6183 Perl_my_mkstemp(char *templte)
6184 {
6185     PERL_ARGS_ASSERT_MY_MKSTEMP;
6186     return S_my_mkostemp(templte, 0);
6187 }
6188 #endif
6189 
6190 REGEXP *
6191 Perl_get_re_arg(pTHX_ SV *sv) {
6192 
6193     if (sv) {
6194         if (SvMAGICAL(sv))
6195             mg_get(sv);
6196         if (SvROK(sv))
6197             sv = MUTABLE_SV(SvRV(sv));
6198         if (SvTYPE(sv) == SVt_REGEXP)
6199             return (REGEXP*) sv;
6200     }
6201 
6202     return NULL;
6203 }
6204 
6205 /*
6206  * This code is derived from drand48() implementation from FreeBSD,
6207  * found in lib/libc/gen/_rand48.c.
6208  *
6209  * The U64 implementation is original, based on the POSIX
6210  * specification for drand48().
6211  */
6212 
6213 /*
6214 * Copyright (c) 1993 Martin Birgmeier
6215 * All rights reserved.
6216 *
6217 * You may redistribute unmodified or modified versions of this source
6218 * code provided that the above copyright notice and this and the
6219 * following conditions are retained.
6220 *
6221 * This software is provided ``as is'', and comes with no warranties
6222 * of any kind. I shall in no event be liable for anything that happens
6223 * to anyone/anything when using this software.
6224 */
6225 
6226 #define FREEBSD_DRAND48_SEED_0   (0x330e)
6227 
6228 #ifdef PERL_DRAND48_QUAD
6229 
6230 #define DRAND48_MULT UINT64_C(0x5deece66d)
6231 #define DRAND48_ADD  0xb
6232 #define DRAND48_MASK UINT64_C(0xffffffffffff)
6233 
6234 #else
6235 
6236 #define FREEBSD_DRAND48_SEED_1   (0xabcd)
6237 #define FREEBSD_DRAND48_SEED_2   (0x1234)
6238 #define FREEBSD_DRAND48_MULT_0   (0xe66d)
6239 #define FREEBSD_DRAND48_MULT_1   (0xdeec)
6240 #define FREEBSD_DRAND48_MULT_2   (0x0005)
6241 #define FREEBSD_DRAND48_ADD      (0x000b)
6242 
6243 const unsigned short _rand48_mult[3] = {
6244                 FREEBSD_DRAND48_MULT_0,
6245                 FREEBSD_DRAND48_MULT_1,
6246                 FREEBSD_DRAND48_MULT_2
6247 };
6248 const unsigned short _rand48_add = FREEBSD_DRAND48_ADD;
6249 
6250 #endif
6251 
6252 void
6253 Perl_drand48_init_r(perl_drand48_t *random_state, U32 seed)
6254 {
6255     PERL_ARGS_ASSERT_DRAND48_INIT_R;
6256 
6257 #ifdef PERL_DRAND48_QUAD
6258     *random_state = FREEBSD_DRAND48_SEED_0 + ((U64)seed << 16);
6259 #else
6260     random_state->seed[0] = FREEBSD_DRAND48_SEED_0;
6261     random_state->seed[1] = (U16) seed;
6262     random_state->seed[2] = (U16) (seed >> 16);
6263 #endif
6264 }
6265 
6266 double
6267 Perl_drand48_r(perl_drand48_t *random_state)
6268 {
6269     PERL_ARGS_ASSERT_DRAND48_R;
6270 
6271 #ifdef PERL_DRAND48_QUAD
6272     *random_state = (*random_state * DRAND48_MULT + DRAND48_ADD)
6273         & DRAND48_MASK;
6274 
6275     return ldexp((double)*random_state, -48);
6276 #else
6277     {
6278     U32 accu;
6279     U16 temp[2];
6280 
6281     accu = (U32) _rand48_mult[0] * (U32) random_state->seed[0]
6282          + (U32) _rand48_add;
6283     temp[0] = (U16) accu;        /* lower 16 bits */
6284     accu >>= sizeof(U16) * 8;
6285     accu += (U32) _rand48_mult[0] * (U32) random_state->seed[1]
6286           + (U32) _rand48_mult[1] * (U32) random_state->seed[0];
6287     temp[1] = (U16) accu;        /* middle 16 bits */
6288     accu >>= sizeof(U16) * 8;
6289     accu += _rand48_mult[0] * random_state->seed[2]
6290           + _rand48_mult[1] * random_state->seed[1]
6291           + _rand48_mult[2] * random_state->seed[0];
6292     random_state->seed[0] = temp[0];
6293     random_state->seed[1] = temp[1];
6294     random_state->seed[2] = (U16) accu;
6295 
6296     return ldexp((double) random_state->seed[0], -48) +
6297            ldexp((double) random_state->seed[1], -32) +
6298            ldexp((double) random_state->seed[2], -16);
6299     }
6300 #endif
6301 }
6302 
6303 #ifdef USE_C_BACKTRACE
6304 
6305 /* Possibly move all this USE_C_BACKTRACE code into a new file. */
6306 
6307 #ifdef USE_BFD
6308 
6309 typedef struct {
6310     /* abfd is the BFD handle. */
6311     bfd* abfd;
6312     /* bfd_syms is the BFD symbol table. */
6313     asymbol** bfd_syms;
6314     /* bfd_text is handle to the the ".text" section of the object file. */
6315     asection* bfd_text;
6316     /* Since opening the executable and scanning its symbols is quite
6317      * heavy operation, we remember the filename we used the last time,
6318      * and do the opening and scanning only if the filename changes.
6319      * This removes most (but not all) open+scan cycles. */
6320     const char* fname_prev;
6321 } bfd_context;
6322 
6323 /* Given a dl_info, update the BFD context if necessary. */
6324 static void bfd_update(bfd_context* ctx, Dl_info* dl_info)
6325 {
6326     /* BFD open and scan only if the filename changed. */
6327     if (ctx->fname_prev == NULL ||
6328         strNE(dl_info->dli_fname, ctx->fname_prev)) {
6329         if (ctx->abfd) {
6330             bfd_close(ctx->abfd);
6331         }
6332         ctx->abfd = bfd_openr(dl_info->dli_fname, 0);
6333         if (ctx->abfd) {
6334             if (bfd_check_format(ctx->abfd, bfd_object)) {
6335                 IV symbol_size = bfd_get_symtab_upper_bound(ctx->abfd);
6336                 if (symbol_size > 0) {
6337                     Safefree(ctx->bfd_syms);
6338                     Newx(ctx->bfd_syms, symbol_size, asymbol*);
6339                     ctx->bfd_text =
6340                         bfd_get_section_by_name(ctx->abfd, ".text");
6341                 }
6342                 else
6343                     ctx->abfd = NULL;
6344             }
6345             else
6346                 ctx->abfd = NULL;
6347         }
6348         ctx->fname_prev = dl_info->dli_fname;
6349     }
6350 }
6351 
6352 /* Given a raw frame, try to symbolize it and store
6353  * symbol information (source file, line number) away. */
6354 static void bfd_symbolize(bfd_context* ctx,
6355                           void* raw_frame,
6356                           char** symbol_name,
6357                           STRLEN* symbol_name_size,
6358                           char** source_name,
6359                           STRLEN* source_name_size,
6360                           STRLEN* source_line)
6361 {
6362     *symbol_name = NULL;
6363     *symbol_name_size = 0;
6364     if (ctx->abfd) {
6365         IV offset = PTR2IV(raw_frame) - PTR2IV(ctx->bfd_text->vma);
6366         if (offset > 0 &&
6367             bfd_canonicalize_symtab(ctx->abfd, ctx->bfd_syms) > 0) {
6368             const char *file;
6369             const char *func;
6370             unsigned int line = 0;
6371             if (bfd_find_nearest_line(ctx->abfd, ctx->bfd_text,
6372                                       ctx->bfd_syms, offset,
6373                                       &file, &func, &line) &&
6374                 file && func && line > 0) {
6375                 /* Size and copy the source file, use only
6376                  * the basename of the source file.
6377                  *
6378                  * NOTE: the basenames are fine for the
6379                  * Perl source files, but may not always
6380                  * be the best idea for XS files. */
6381                 const char *p, *b = NULL;
6382                 /* Look for the last slash. */
6383                 for (p = file; *p; p++) {
6384                     if (*p == '/')
6385                         b = p + 1;
6386                 }
6387                 if (b == NULL || *b == 0) {
6388                     b = file;
6389                 }
6390                 *source_name_size = p - b + 1;
6391                 Newx(*source_name, *source_name_size + 1, char);
6392                 Copy(b, *source_name, *source_name_size + 1, char);
6393 
6394                 *symbol_name_size = strlen(func);
6395                 Newx(*symbol_name, *symbol_name_size + 1, char);
6396                 Copy(func, *symbol_name, *symbol_name_size + 1, char);
6397 
6398                 *source_line = line;
6399             }
6400         }
6401     }
6402 }
6403 
6404 #endif /* #ifdef USE_BFD */
6405 
6406 #ifdef PERL_DARWIN
6407 
6408 /* OS X has no public API for for 'symbolicating' (Apple official term)
6409  * stack addresses to {function_name, source_file, line_number}.
6410  * Good news: there is command line utility atos(1) which does that.
6411  * Bad news 1: it's a command line utility.
6412  * Bad news 2: one needs to have the Developer Tools installed.
6413  * Bad news 3: in newer releases it needs to be run as 'xcrun atos'.
6414  *
6415  * To recap: we need to open a pipe for reading for a utility which
6416  * might not exist, or exists in different locations, and then parse
6417  * the output.  And since this is all for a low-level API, we cannot
6418  * use high-level stuff.  Thanks, Apple. */
6419 
6420 typedef struct {
6421     /* tool is set to the absolute pathname of the tool to use:
6422      * xcrun or atos. */
6423     const char* tool;
6424     /* format is set to a printf format string used for building
6425      * the external command to run. */
6426     const char* format;
6427     /* unavail is set if e.g. xcrun cannot be found, or something
6428      * else happens that makes getting the backtrace dubious.  Note,
6429      * however, that the context isn't persistent, the next call to
6430      * get_c_backtrace() will start from scratch. */
6431     bool unavail;
6432     /* fname is the current object file name. */
6433     const char* fname;
6434     /* object_base_addr is the base address of the shared object. */
6435     void* object_base_addr;
6436 } atos_context;
6437 
6438 /* Given |dl_info|, updates the context.  If the context has been
6439  * marked unavailable, return immediately.  If not but the tool has
6440  * not been set, set it to either "xcrun atos" or "atos" (also set the
6441  * format to use for creating commands for piping), or if neither is
6442  * unavailable (one needs the Developer Tools installed), mark the context
6443  * an unavailable.  Finally, update the filename (object name),
6444  * and its base address. */
6445 
6446 static void atos_update(atos_context* ctx,
6447                         Dl_info* dl_info)
6448 {
6449     if (ctx->unavail)
6450         return;
6451     if (ctx->tool == NULL) {
6452         const char* tools[] = {
6453             "/usr/bin/xcrun",
6454             "/usr/bin/atos"
6455         };
6456         const char* formats[] = {
6457             "/usr/bin/xcrun atos -o '%s' -l %08x %08x 2>&1",
6458             "/usr/bin/atos -d -o '%s' -l %08x %08x 2>&1"
6459         };
6460         struct stat st;
6461         UV i;
6462         for (i = 0; i < C_ARRAY_LENGTH(tools); i++) {
6463             if (stat(tools[i], &st) == 0 && S_ISREG(st.st_mode)) {
6464                 ctx->tool = tools[i];
6465                 ctx->format = formats[i];
6466                 break;
6467             }
6468         }
6469         if (ctx->tool == NULL) {
6470             ctx->unavail = TRUE;
6471             return;
6472         }
6473     }
6474     if (ctx->fname == NULL ||
6475         strNE(dl_info->dli_fname, ctx->fname)) {
6476         ctx->fname = dl_info->dli_fname;
6477         ctx->object_base_addr = dl_info->dli_fbase;
6478     }
6479 }
6480 
6481 /* Given an output buffer end |p| and its |start|, matches
6482  * for the atos output, extracting the source code location
6483  * and returning non-NULL if possible, returning NULL otherwise. */
6484 static const char* atos_parse(const char* p,
6485                               const char* start,
6486                               STRLEN* source_name_size,
6487                               STRLEN* source_line) {
6488     /* atos() output is something like:
6489      * perl_parse (in miniperl) (perl.c:2314)\n\n".
6490      * We cannot use Perl regular expressions, because we need to
6491      * stay low-level.  Therefore here we have a rolled-out version
6492      * of a state machine which matches _backwards_from_the_end_ and
6493      * if there's a success, returns the starts of the filename,
6494      * also setting the filename size and the source line number.
6495      * The matched regular expression is roughly "\(.*:\d+\)\s*$" */
6496     const char* source_number_start;
6497     const char* source_name_end;
6498     const char* source_line_end = start;
6499     const char* close_paren;
6500     UV uv;
6501 
6502     /* Skip trailing whitespace. */
6503     while (p > start && isSPACE(*p)) p--;
6504     /* Now we should be at the close paren. */
6505     if (p == start || *p != ')')
6506         return NULL;
6507     close_paren = p;
6508     p--;
6509     /* Now we should be in the line number. */
6510     if (p == start || !isDIGIT(*p))
6511         return NULL;
6512     /* Skip over the digits. */
6513     while (p > start && isDIGIT(*p))
6514         p--;
6515     /* Now we should be at the colon. */
6516     if (p == start || *p != ':')
6517         return NULL;
6518     source_number_start = p + 1;
6519     source_name_end = p; /* Just beyond the end. */
6520     p--;
6521     /* Look for the open paren. */
6522     while (p > start && *p != '(')
6523         p--;
6524     if (p == start)
6525         return NULL;
6526     p++;
6527     *source_name_size = source_name_end - p;
6528     if (grok_atoUV(source_number_start, &uv,  &source_line_end)
6529         && source_line_end == close_paren
6530         && uv <= PERL_INT_MAX
6531     ) {
6532         *source_line = (STRLEN)uv;
6533         return p;
6534     }
6535     return NULL;
6536 }
6537 
6538 /* Given a raw frame, read a pipe from the symbolicator (that's the
6539  * technical term) atos, reads the result, and parses the source code
6540  * location.  We must stay low-level, so we use snprintf(), pipe(),
6541  * and fread(), and then also parse the output ourselves. */
6542 static void atos_symbolize(atos_context* ctx,
6543                            void* raw_frame,
6544                            char** source_name,
6545                            STRLEN* source_name_size,
6546                            STRLEN* source_line)
6547 {
6548     char cmd[1024];
6549     const char* p;
6550     Size_t cnt;
6551 
6552     if (ctx->unavail)
6553         return;
6554     /* Simple security measure: if there's any funny business with
6555      * the object name (used as "-o '%s'" ), leave since at least
6556      * partially the user controls it. */
6557     for (p = ctx->fname; *p; p++) {
6558         if (*p == '\'' || isCNTRL(*p)) {
6559             ctx->unavail = TRUE;
6560             return;
6561         }
6562     }
6563     cnt = snprintf(cmd, sizeof(cmd), ctx->format,
6564                    ctx->fname, ctx->object_base_addr, raw_frame);
6565     if (cnt < sizeof(cmd)) {
6566         /* Undo nostdio.h #defines that disable stdio.
6567          * This is somewhat naughty, but is used elsewhere
6568          * in the core, and affects only OS X. */
6569 #undef FILE
6570 #undef popen
6571 #undef fread
6572 #undef pclose
6573         FILE* fp = popen(cmd, "r");
6574         /* At the moment we open a new pipe for each stack frame.
6575          * This is naturally somewhat slow, but hopefully generating
6576          * stack traces is never going to in a performance critical path.
6577          *
6578          * We could play tricks with atos by batching the stack
6579          * addresses to be resolved: atos can either take multiple
6580          * addresses from the command line, or read addresses from
6581          * a file (though the mess of creating temporary files would
6582          * probably negate much of any possible speedup).
6583          *
6584          * Normally there are only two objects present in the backtrace:
6585          * perl itself, and the libdyld.dylib.  (Note that the object
6586          * filenames contain the full pathname, so perl may not always
6587          * be in the same place.)  Whenever the object in the
6588          * backtrace changes, the base address also changes.
6589          *
6590          * The problem with batching the addresses, though, would be
6591          * matching the results with the addresses: the parsing of
6592          * the results is already painful enough with a single address. */
6593         if (fp) {
6594             char out[1024];
6595             UV cnt = fread(out, 1, sizeof(out), fp);
6596             if (cnt < sizeof(out)) {
6597                 const char* p = atos_parse(out + cnt - 1, out,
6598                                            source_name_size,
6599                                            source_line);
6600                 if (p) {
6601                     Newx(*source_name,
6602                          *source_name_size, char);
6603                     Copy(p, *source_name,
6604                          *source_name_size,  char);
6605                 }
6606             }
6607             pclose(fp);
6608         }
6609     }
6610 }
6611 
6612 #endif /* #ifdef PERL_DARWIN */
6613 
6614 /*
6615 =for apidoc_section $debugging
6616 =for apidoc get_c_backtrace
6617 
6618 Collects the backtrace (aka "stacktrace") into a single linear
6619 malloced buffer, which the caller B<must> C<Perl_free_c_backtrace()>.
6620 
6621 Scans the frames back by S<C<depth + skip>>, then drops the C<skip> innermost,
6622 returning at most C<depth> frames.
6623 
6624 =cut
6625 */
6626 
6627 Perl_c_backtrace*
6628 Perl_get_c_backtrace(pTHX_ int depth, int skip)
6629 {
6630     /* Note that here we must stay as low-level as possible: Newx(),
6631      * Copy(), Safefree(); since we may be called from anywhere,
6632      * so we should avoid higher level constructs like SVs or AVs.
6633      *
6634      * Since we are using safesysmalloc() via Newx(), don't try
6635      * getting backtrace() there, unless you like deep recursion. */
6636 
6637     /* Currently only implemented with backtrace() and dladdr(),
6638      * for other platforms NULL is returned. */
6639 
6640 #if defined(HAS_BACKTRACE) && defined(HAS_DLADDR)
6641     /* backtrace() is available via <execinfo.h> in glibc and in most
6642      * modern BSDs; dladdr() is available via <dlfcn.h>. */
6643 
6644     /* We try fetching this many frames total, but then discard
6645      * the |skip| first ones.  For the remaining ones we will try
6646      * retrieving more information with dladdr(). */
6647     int try_depth = skip +  depth;
6648 
6649     /* The addresses (program counters) returned by backtrace(). */
6650     void** raw_frames;
6651 
6652     /* Retrieved with dladdr() from the addresses returned by backtrace(). */
6653     Dl_info* dl_infos;
6654 
6655     /* Sizes _including_ the terminating \0 of the object name
6656      * and symbol name strings. */
6657     STRLEN* object_name_sizes;
6658     STRLEN* symbol_name_sizes;
6659 
6660 #ifdef USE_BFD
6661     /* The symbol names comes either from dli_sname,
6662      * or if using BFD, they can come from BFD. */
6663     char** symbol_names;
6664 #endif
6665 
6666     /* The source code location information.  Dug out with e.g. BFD. */
6667     char** source_names;
6668     STRLEN* source_name_sizes;
6669     STRLEN* source_lines;
6670 
6671     Perl_c_backtrace* bt = NULL;  /* This is what will be returned. */
6672     int got_depth; /* How many frames were returned from backtrace(). */
6673     UV frame_count = 0; /* How many frames we return. */
6674     UV total_bytes = 0; /* The size of the whole returned backtrace. */
6675 
6676 #ifdef USE_BFD
6677     bfd_context bfd_ctx;
6678 #endif
6679 #ifdef PERL_DARWIN
6680     atos_context atos_ctx;
6681 #endif
6682 
6683     /* Here are probably possibilities for optimizing.  We could for
6684      * example have a struct that contains most of these and then
6685      * allocate |try_depth| of them, saving a bunch of malloc calls.
6686      * Note, however, that |frames| could not be part of that struct
6687      * because backtrace() will want an array of just them.  Also be
6688      * careful about the name strings. */
6689     Newx(raw_frames, try_depth, void*);
6690     Newx(dl_infos, try_depth, Dl_info);
6691     Newx(object_name_sizes, try_depth, STRLEN);
6692     Newx(symbol_name_sizes, try_depth, STRLEN);
6693     Newx(source_names, try_depth, char*);
6694     Newx(source_name_sizes, try_depth, STRLEN);
6695     Newx(source_lines, try_depth, STRLEN);
6696 #ifdef USE_BFD
6697     Newx(symbol_names, try_depth, char*);
6698 #endif
6699 
6700     /* Get the raw frames. */
6701     got_depth = (int)backtrace(raw_frames, try_depth);
6702 
6703     /* We use dladdr() instead of backtrace_symbols() because we want
6704      * the full details instead of opaque strings.  This is useful for
6705      * two reasons: () the details are needed for further symbolic
6706      * digging, for example in OS X (2) by having the details we fully
6707      * control the output, which in turn is useful when more platforms
6708      * are added: we can keep out output "portable". */
6709 
6710     /* We want a single linear allocation, which can then be freed
6711      * with a single swoop.  We will do the usual trick of first
6712      * walking over the structure and seeing how much we need to
6713      * allocate, then allocating, and then walking over the structure
6714      * the second time and populating it. */
6715 
6716     /* First we must compute the total size of the buffer. */
6717     total_bytes = sizeof(Perl_c_backtrace_header);
6718     if (got_depth > skip) {
6719         int i;
6720 #ifdef USE_BFD
6721         bfd_init(); /* Is this safe to call multiple times? */
6722         Zero(&bfd_ctx, 1, bfd_context);
6723 #endif
6724 #ifdef PERL_DARWIN
6725         Zero(&atos_ctx, 1, atos_context);
6726 #endif
6727         for (i = skip; i < try_depth; i++) {
6728             Dl_info* dl_info = &dl_infos[i];
6729 
6730             object_name_sizes[i] = 0;
6731             source_names[i] = NULL;
6732             source_name_sizes[i] = 0;
6733             source_lines[i] = 0;
6734 
6735             /* Yes, zero from dladdr() is failure. */
6736             if (dladdr(raw_frames[i], dl_info)) {
6737                 total_bytes += sizeof(Perl_c_backtrace_frame);
6738 
6739                 object_name_sizes[i] =
6740                     dl_info->dli_fname ? strlen(dl_info->dli_fname) : 0;
6741                 symbol_name_sizes[i] =
6742                     dl_info->dli_sname ? strlen(dl_info->dli_sname) : 0;
6743 #ifdef USE_BFD
6744                 bfd_update(&bfd_ctx, dl_info);
6745                 bfd_symbolize(&bfd_ctx, raw_frames[i],
6746                               &symbol_names[i],
6747                               &symbol_name_sizes[i],
6748                               &source_names[i],
6749                               &source_name_sizes[i],
6750                               &source_lines[i]);
6751 #endif
6752 #if PERL_DARWIN
6753                 atos_update(&atos_ctx, dl_info);
6754                 atos_symbolize(&atos_ctx,
6755                                raw_frames[i],
6756                                &source_names[i],
6757                                &source_name_sizes[i],
6758                                &source_lines[i]);
6759 #endif
6760 
6761                 /* Plus ones for the terminating \0. */
6762                 total_bytes += object_name_sizes[i] + 1;
6763                 total_bytes += symbol_name_sizes[i] + 1;
6764                 total_bytes += source_name_sizes[i] + 1;
6765 
6766                 frame_count++;
6767             } else {
6768                 break;
6769             }
6770         }
6771 #ifdef USE_BFD
6772         Safefree(bfd_ctx.bfd_syms);
6773 #endif
6774     }
6775 
6776     /* Now we can allocate and populate the result buffer. */
6777     Newxc(bt, total_bytes, char, Perl_c_backtrace);
6778     Zero(bt, total_bytes, char);
6779     bt->header.frame_count = frame_count;
6780     bt->header.total_bytes = total_bytes;
6781     if (frame_count > 0) {
6782         Perl_c_backtrace_frame* frame = bt->frame_info;
6783         char* name_base = (char *)(frame + frame_count);
6784         char* name_curr = name_base; /* Outputting the name strings here. */
6785         UV i;
6786         for (i = skip; i < skip + frame_count; i++) {
6787             Dl_info* dl_info = &dl_infos[i];
6788 
6789             frame->addr = raw_frames[i];
6790             frame->object_base_addr = dl_info->dli_fbase;
6791             frame->symbol_addr = dl_info->dli_saddr;
6792 
6793             /* Copies a string, including the \0, and advances the name_curr.
6794              * Also copies the start and the size to the frame. */
6795 #define PERL_C_BACKTRACE_STRCPY(frame, doffset, src, dsize, size) \
6796             if (size && src) \
6797                 Copy(src, name_curr, size, char); \
6798             frame->doffset = name_curr - (char*)bt; \
6799             frame->dsize = size; \
6800             name_curr += size; \
6801             *name_curr++ = 0;
6802 
6803             PERL_C_BACKTRACE_STRCPY(frame, object_name_offset,
6804                                     dl_info->dli_fname,
6805                                     object_name_size, object_name_sizes[i]);
6806 
6807 #ifdef USE_BFD
6808             PERL_C_BACKTRACE_STRCPY(frame, symbol_name_offset,
6809                                     symbol_names[i],
6810                                     symbol_name_size, symbol_name_sizes[i]);
6811             Safefree(symbol_names[i]);
6812 #else
6813             PERL_C_BACKTRACE_STRCPY(frame, symbol_name_offset,
6814                                     dl_info->dli_sname,
6815                                     symbol_name_size, symbol_name_sizes[i]);
6816 #endif
6817 
6818             PERL_C_BACKTRACE_STRCPY(frame, source_name_offset,
6819                                     source_names[i],
6820                                     source_name_size, source_name_sizes[i]);
6821             Safefree(source_names[i]);
6822 
6823 #undef PERL_C_BACKTRACE_STRCPY
6824 
6825             frame->source_line_number = source_lines[i];
6826 
6827             frame++;
6828         }
6829         assert(total_bytes ==
6830                (UV)(sizeof(Perl_c_backtrace_header) +
6831                     frame_count * sizeof(Perl_c_backtrace_frame) +
6832                     name_curr - name_base));
6833     }
6834 #ifdef USE_BFD
6835     Safefree(symbol_names);
6836     if (bfd_ctx.abfd) {
6837         bfd_close(bfd_ctx.abfd);
6838     }
6839 #endif
6840     Safefree(source_lines);
6841     Safefree(source_name_sizes);
6842     Safefree(source_names);
6843     Safefree(symbol_name_sizes);
6844     Safefree(object_name_sizes);
6845     /* Assuming the strings returned by dladdr() are pointers
6846      * to read-only static memory (the object file), so that
6847      * they do not need freeing (and cannot be). */
6848     Safefree(dl_infos);
6849     Safefree(raw_frames);
6850     return bt;
6851 #else
6852     PERL_UNUSED_ARG(depth);
6853     PERL_UNUSED_ARG(skip);
6854     return NULL;
6855 #endif
6856 }
6857 
6858 /*
6859 =for apidoc free_c_backtrace
6860 
6861 Deallocates a backtrace received from get_c_backtrace.
6862 
6863 =cut
6864 */
6865 
6866 /*
6867 =for apidoc get_c_backtrace_dump
6868 
6869 Returns a SV containing a dump of C<depth> frames of the call stack, skipping
6870 the C<skip> innermost ones.  C<depth> of 20 is usually enough.
6871 
6872 The appended output looks like:
6873 
6874  ...
6875  1   10e004812:0082   Perl_croak   util.c:1716    /usr/bin/perl
6876  2   10df8d6d2:1d72   perl_parse   perl.c:3975    /usr/bin/perl
6877  ...
6878 
6879 The fields are tab-separated.  The first column is the depth (zero
6880 being the innermost non-skipped frame).  In the hex:offset, the hex is
6881 where the program counter was in C<S_parse_body>, and the :offset (might
6882 be missing) tells how much inside the C<S_parse_body> the program counter was.
6883 
6884 The C<util.c:1716> is the source code file and line number.
6885 
6886 The F</usr/bin/perl> is obvious (hopefully).
6887 
6888 Unknowns are C<"-">.  Unknowns can happen unfortunately quite easily:
6889 if the platform doesn't support retrieving the information;
6890 if the binary is missing the debug information;
6891 if the optimizer has transformed the code by for example inlining.
6892 
6893 =cut
6894 */
6895 
6896 SV*
6897 Perl_get_c_backtrace_dump(pTHX_ int depth, int skip)
6898 {
6899     Perl_c_backtrace* bt;
6900 
6901     bt = get_c_backtrace(depth, skip + 1 /* Hide ourselves. */);
6902     if (bt) {
6903         Perl_c_backtrace_frame* frame;
6904         SV* dsv = newSVpvs("");
6905         UV i;
6906         for (i = 0, frame = bt->frame_info;
6907              i < bt->header.frame_count; i++, frame++) {
6908             Perl_sv_catpvf(aTHX_ dsv, "%d", (int)i);
6909             Perl_sv_catpvf(aTHX_ dsv, "\t%p", frame->addr ? frame->addr : "-");
6910             /* Symbol (function) names might disappear without debug info.
6911              *
6912              * The source code location might disappear in case of the
6913              * optimizer inlining or otherwise rearranging the code. */
6914             if (frame->symbol_addr) {
6915                 Perl_sv_catpvf(aTHX_ dsv, ":%04x",
6916                                (int)
6917                                ((char*)frame->addr - (char*)frame->symbol_addr));
6918             }
6919             Perl_sv_catpvf(aTHX_ dsv, "\t%s",
6920                            frame->symbol_name_size &&
6921                            frame->symbol_name_offset ?
6922                            (char*)bt + frame->symbol_name_offset : "-");
6923             if (frame->source_name_size &&
6924                 frame->source_name_offset &&
6925                 frame->source_line_number) {
6926                 Perl_sv_catpvf(aTHX_ dsv, "\t%s:%" UVuf,
6927                                (char*)bt + frame->source_name_offset,
6928                                (UV)frame->source_line_number);
6929             } else {
6930                 Perl_sv_catpvf(aTHX_ dsv, "\t-");
6931             }
6932             Perl_sv_catpvf(aTHX_ dsv, "\t%s",
6933                            frame->object_name_size &&
6934                            frame->object_name_offset ?
6935                            (char*)bt + frame->object_name_offset : "-");
6936             /* The frame->object_base_addr is not output,
6937              * but it is used for symbolizing/symbolicating. */
6938             sv_catpvs(dsv, "\n");
6939         }
6940 
6941         Perl_free_c_backtrace(bt);
6942 
6943         return dsv;
6944     }
6945 
6946     return NULL;
6947 }
6948 
6949 /*
6950 =for apidoc dump_c_backtrace
6951 
6952 Dumps the C backtrace to the given C<fp>.
6953 
6954 Returns true if a backtrace could be retrieved, false if not.
6955 
6956 =cut
6957 */
6958 
6959 bool
6960 Perl_dump_c_backtrace(pTHX_ PerlIO* fp, int depth, int skip)
6961 {
6962     SV* sv;
6963 
6964     PERL_ARGS_ASSERT_DUMP_C_BACKTRACE;
6965 
6966     sv = Perl_get_c_backtrace_dump(aTHX_ depth, skip);
6967     if (sv) {
6968         sv_2mortal(sv);
6969         PerlIO_printf(fp, "%s", SvPV_nolen(sv));
6970         return TRUE;
6971     }
6972     return FALSE;
6973 }
6974 
6975 #endif /* #ifdef USE_C_BACKTRACE */
6976 
6977 #if defined(USE_ITHREADS) && defined(I_PTHREAD)
6978 
6979 /* pthread_mutex_t and perl_mutex are typedef equivalent
6980  * so casting the pointers is fine. */
6981 
6982 int perl_tsa_mutex_lock(perl_mutex* mutex)
6983 {
6984     return pthread_mutex_lock((pthread_mutex_t *) mutex);
6985 }
6986 
6987 int perl_tsa_mutex_unlock(perl_mutex* mutex)
6988 {
6989     return pthread_mutex_unlock((pthread_mutex_t *) mutex);
6990 }
6991 
6992 int perl_tsa_mutex_destroy(perl_mutex* mutex)
6993 {
6994     return pthread_mutex_destroy((pthread_mutex_t *) mutex);
6995 }
6996 
6997 #endif
6998 
6999 #ifdef USE_DTRACE
7000 
7001 /* log a sub call or return */
7002 
7003 void
7004 Perl_dtrace_probe_call(pTHX_ CV *cv, bool is_call)
7005 {
7006     const char *func;
7007     const char *file;
7008     const char *stash;
7009     const COP  *start;
7010     line_t      line;
7011 
7012     PERL_ARGS_ASSERT_DTRACE_PROBE_CALL;
7013 
7014     if (CvNAMED(cv)) {
7015         HEK *hek = CvNAME_HEK(cv);
7016         func = HEK_KEY(hek);
7017     }
7018     else {
7019         GV  *gv = CvGV(cv);
7020         func = GvENAME(gv);
7021     }
7022     start = (const COP *)CvSTART(cv);
7023     file  = CopFILE(start);
7024     line  = CopLINE(start);
7025     stash = CopSTASHPV(start);
7026 
7027     if (is_call) {
7028         PERL_SUB_ENTRY(func, file, line, stash);
7029     }
7030     else {
7031         PERL_SUB_RETURN(func, file, line, stash);
7032     }
7033 }
7034 
7035 
7036 /* log a require file loading/loaded  */
7037 
7038 void
7039 Perl_dtrace_probe_load(pTHX_ const char *name, bool is_loading)
7040 {
7041     PERL_ARGS_ASSERT_DTRACE_PROBE_LOAD;
7042 
7043     if (is_loading) {
7044         PERL_LOADING_FILE(name);
7045     }
7046     else {
7047         PERL_LOADED_FILE(name);
7048     }
7049 }
7050 
7051 
7052 /* log an op execution */
7053 
7054 void
7055 Perl_dtrace_probe_op(pTHX_ const OP *op)
7056 {
7057     PERL_ARGS_ASSERT_DTRACE_PROBE_OP;
7058 
7059     PERL_OP_ENTRY(OP_NAME(op));
7060 }
7061 
7062 
7063 /* log a compile/run phase change */
7064 
7065 void
7066 Perl_dtrace_probe_phase(pTHX_ enum perl_phase phase)
7067 {
7068     const char *ph_old = PL_phase_names[PL_phase];
7069     const char *ph_new = PL_phase_names[phase];
7070 
7071     PERL_PHASE_CHANGE(ph_new, ph_old);
7072 }
7073 
7074 #endif
7075 
7076 /*
7077  * ex: set ts=8 sts=4 sw=4 et:
7078  */
7079