Skip to content

Commit

Permalink
de-mathom Perl_sv_taint() part 3 (SvTAINT() branch remove)
Browse files Browse the repository at this point in the history
"SvTAINT();" contains "if(PL_tainting && PL_tainted) sv_taint(sv);"
that is 2 One Byte reads and 2 branches. Collapse the 2 bool chars, to a
U16, so it is exactly 1 read, and 1 branch. Strips complexity from the
very bottom of the very hot newSVuv/newSViv/newSVuv, and other callers.
sv_taint(sv) has 62 callers, not sure how many do the 2 reads, 2 branches
SvTAINT(sv);, but the change decreased the size of miniperl.exe and
therefore perl541.dll, and branches were removed from the
newSVuv/newSViv/newSVuv trio.

Delta machine code bytes, between part 2 & 3 (this commit).

previous miniperl.exe Win64 .text section, VC 2022 -O1
0x1240AC bytes long

after
0x12408C bytes long
  • Loading branch information
bulk88 committed Oct 17, 2024
1 parent 7dd92ee commit 7f29878
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
3 changes: 1 addition & 2 deletions embedvar.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions intrpvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ PERLVAR(I, multideref_pc, UNOP_AUX_item *)
PERLVAR(I, curpm, PMOP *) /* what to do \ interps in REs from */
PERLVAR(I, curpm_under, PMOP *) /* what to do \ interps in REs from */

PERLVAR(I, tainting, bool) /* ? doing taint checks */
PERLVARI(I, tainted, bool, FALSE) /* using variables controlled by $< */
/* bool PL_tainting --- ? doing taint checks */
/* bool PL_tainted --- using variables controlled by $< */
PERLVAR(I, taint, TAINT_U)

/* PL_delaymagic is currently used for two purposes: to assure simultaneous
* updates in ($<,$>) = ..., and to assure atomic update in push/unshift
Expand Down
11 changes: 11 additions & 0 deletions perl.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,17 @@ perl_construct(pTHXx)
SvREADONLY_on(&PL_sv_placeholder);
SvREFCNT(&PL_sv_placeholder) = SvREFCNT_IMMORTAL;

STATIC_ASSERT_STMT(
sizeof(((TAINT_U *)0)->both)
== (sizeof(((TAINT_U *)0)->u.tainting) + sizeof(((TAINT_U *)0)->u.tainted))
);
STATIC_ASSERT_STMT(
sizeof(((TAINT_U *)0)->both)
== (STRUCT_OFFSET(TAINT_U, u.tainted) + sizeof(((TAINT_U *)0)->u.tainted))
);
STATIC_ASSERT_STMT(STRUCT_OFFSET(TAINT_U, both) == STRUCT_OFFSET(TAINT_U, u.tainting));
/* PL_taint.u.both = 0; */

PL_sighandlerp = Perl_sighandler;
PL_sighandler1p = Perl_sighandler1;
PL_sighandler3p = Perl_sighandler3;
Expand Down
17 changes: 17 additions & 0 deletions perl.h
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,8 @@ symbol would not be defined on C<L</EBCDIC>> platforms.
* know what you're doing: tests and CPAN modules' tests are bound to fail.
*/
#ifdef NO_TAINT_SUPPORT
# define PL_tainting PL_taint.u.tainting
# define PL_tainted PL_taint.u.tainted
# define TAINT NOOP
# define TAINT_NOT NOOP
# define TAINT_IF(c) NOOP
Expand All @@ -948,6 +950,7 @@ symbol would not be defined on C<L</EBCDIC>> platforms.
# define TAINT_set(s) NOOP
# define TAINT_get 0
# define TAINTING_get 0
# define TAINT_AND_TAINTING_get 0
# define TAINTING_set(s) NOOP
# define TAINT_WARN_get 0
# define TAINT_WARN_set(s) NOOP
Expand Down Expand Up @@ -1014,6 +1017,10 @@ violations are fatal.
=cut
*/

#define PL_tainting PL_taint.u.tainting
#define PL_tainted PL_taint.u.tainted

/* Set to tainted if we are running under tainting mode */
# define TAINT (PL_tainted = PL_tainting)

Expand All @@ -1027,6 +1034,8 @@ violations are fatal.
# define TAINT_set(s) (PL_tainted = cBOOL(s))
# define TAINT_get (cBOOL(UNLIKELY(PL_tainted))) /* Is something tainted? */
# define TAINTING_get (cBOOL(UNLIKELY(PL_tainting)))
/* Efficient version of (PL_tainted && PL_tainting) */
# define TAINT_AND_TAINTING_get (UNLIKELY(PL_taint.both == (TRUE | (TRUE << 8))))
# define TAINTING_set(s) (PL_tainting = cBOOL(s))
# define TAINT_WARN_get (PL_taint_warn)
# define TAINT_WARN_set(s) (PL_taint_warn = cBOOL(s))
Expand Down Expand Up @@ -3309,6 +3318,14 @@ typedef struct padname PADNAME;
#include "handy.h"
#include "charclass_invlists.h"

typedef union {
U16 both;
struct {
bool tainting;
bool tainted;
} u;
} TAINT_U;

#if defined(USE_LARGE_FILES) && !defined(NO_64_BIT_RAWIO)
# if LSEEKSIZE == 8 && !defined(USE_64_BIT_RAWIO)
# define USE_64_BIT_RAWIO /* implicit */
Expand Down
4 changes: 2 additions & 2 deletions sv.c
Original file line number Diff line number Diff line change
Expand Up @@ -15912,7 +15912,7 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags,

#ifndef NO_TAINT_SUPPORT
/* Set tainting stuff before PerlIO_debug can possibly get called */
PL_tainting = proto_perl->Itainting;
PL_tainting = proto_perl->Itaint.u.tainting;
PL_taint_warn = proto_perl->Itaint_warn;
#else
PL_tainting = FALSE;
Expand Down Expand Up @@ -16057,7 +16057,7 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags,
PL_statcache = proto_perl->Istatcache;

#ifndef NO_TAINT_SUPPORT
PL_tainted = proto_perl->Itainted;
PL_tainted = proto_perl->Itaint.u.tainted;
#else
PL_tainted = FALSE;
#endif
Expand Down
4 changes: 2 additions & 2 deletions sv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1735,8 +1735,8 @@ attention to precisely which outputs are influenced by which inputs.
#define SvTAINT(sv) \
STMT_START { \
assert(TAINTING_get || !TAINT_get); \
if (UNLIKELY(TAINT_get)) \
SvTAINTED_on(sv); \
if (TAINT_AND_TAINTING_get) \
sv_taint(sv); \
} STMT_END

/*
Expand Down

0 comments on commit 7f29878

Please sign in to comment.