Opcode(3perl)

๐Ÿ’– NAME ๐Ÿ’–

Opcode โ€” disable named opcodes when compiling perl code ๐Ÿšซโœจ

(this is the Opcode page from the Perl Programmers Reference Guide, bestie, buckle up)


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

use Opcode;

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

okay so first things first: perl code is always compiled into an internal format before it ever runs. like, always always.

When you evaluate perl code โ€” say, via eval or do 'file' โ€” that code gets compiled into an internal format, and then, as long as nothing exploded during compilation, it gets executed. And that internal format? It's built out of many distinct opcodes. tiny little Lego bricks. ๐Ÿงฑ

By default, no opmask is in effect, so literally any code can be compiled. free-for-all energy. ๐ŸŽ‰

But the Opcode module lets you define an operator mask that'll be in effect the next time perl compiles any code. And if you try to compile code containing a masked opcode? Compilation fails with an error. The code does not get executed. nope. blocked. ๐Ÿ’€


๐Ÿ“Œ NOTE ๐Ÿ“Œ

ngl, you usually don't use Opcode directly. for the typical use cases, go look at the ops pragma and the Safe modules instead, bestie. ๐Ÿซถ


๐Ÿšจ WARNING ๐Ÿšจ

listen up, this part is serious. The Opcode module does not implement an effective sandbox for evaluating untrusted code with the perl interpreter.

Bugs in the perl interpreter that could be abused to bypass Opcode restrictions are not treated as vulnerabilities. See perlsecpolicy for more info on that.

The authors make no warranty, implied or otherwise, about the suitability of this software for safety or security purposes. none. zip. ๐Ÿ™ˆ

The authors shall not in any case be liable for special, incidental, consequential, indirect or other similar damages arising from the use of this software.

Your mileage will vary. If in any doubt, do not use it. full stop. periodt.


๐Ÿท๏ธ Operator Names and Operator Lists ๐Ÿท๏ธ

The canonical list of operator names? That's the contents of the array PL_op_name, defined and initialised in the file opcode.h of the Perl source distribution (and installed into the perl library). that's the source of truth, iykyk. ๐Ÿ“œ

Every operator has two names:

  • a terse name (its opname) ๐Ÿ‘•
  • a more verbose / recognisable descriptive name ๐Ÿ“–

The opdesc function can hand you back a list of descriptions for a list of operators, if that's the vibe you need.

Lots of the functions and methods below take a list of operators as parameters. And most operator lists can be made up of several types of element. Each element can be one of:

  • an operator name (opname) โ€” Operator names are typically small lowercase words like enterloop, leaveloop, last, next, redo etc. Sometimes they're rather cryptic like gv2cv, i_ncmp and ftsvtx. cryptic but iconic, we love that for them. ๐ŸŒš
  • an operator tag name (optag) โ€” Operator tags can be used to refer to groups (or sets) of operators. Tag names always begin with a colon. The Opcode module defines several optags, and you can define your own using the define_optag function. ๐Ÿท๏ธ
  • a negated opname or optag โ€” An opname or optag can be prefixed with an exclamation mark, e.g., !mkdir. Negating an opname or optag means remove the corresponding ops from the accumulated set of ops at that point. so !mkdir is giving "not that one bestie" ๐Ÿ’…
  • an operator set (opset) โ€” An opset is a binary string of approximately 44 bytes which holds a set of zero or more operators. (yes, approximately, don't come for me.) The opset and opset_to_ops functions can be used to convert from a list of operators to an opset and vice versa. And wherever a list of operators can be given, you can use one or more opsets. See also Manipulating Opsets below. ๐Ÿงบ

๐Ÿ› ๏ธ Opcode Functions ๐Ÿ› ๏ธ

The Opcode package contains functions for manipulating operator names, tags and sets. All of them are available for export by the package. ๐Ÿซถ

  • opcodes โ€” In a scalar context, opcodes returns the number of opcodes in this version of perl (around 350 for perl-5.7.0). In a list context, it returns a list of all the operator names. (โš ๏ธ Not yet implemented โ€” use @names = opset_to_ops(full_opset).) ๐Ÿ˜ญ
  • opset (OP, ...) โ€” Returns an opset containing the listed operators.
  • opset_to_ops (OPSET) โ€” Returns a list of operator names corresponding to those operators in the set.
  • opset_to_hex (OPSET) โ€” Returns a string representation of an opset. Can be handy for debugging. ๐Ÿ›๐Ÿ”
  • full_opset โ€” Returns an opset which includes all operators. the whole squad. ๐Ÿ‘‘
  • empty_opset โ€” Returns an opset which contains no operators. ghost town. ๐Ÿซฅ
  • invert_opset (OPSET) โ€” Returns an opset which is the inverse set of the one supplied. ๐Ÿ”„
  • verify_opset (OPSET, ...) โ€” Returns true if the supplied opset looks like a valid opset (is the right length etc), otherwise it returns false. If an optional second parameter is true, then verify_opset will croak on an invalid opset instead of returning false. ๐Ÿ˜ค

Most of the other Opcode functions call verify_opset automatically and will croak if given an invalid opset.

  • define_optag (OPTAG, OPSET) โ€” Define OPTAG as a symbolic name for OPSET. Optag names always start with a colon :. ๐Ÿท๏ธ

The optag name used must not be defined already (define_optag will croak if it is already defined). Optag names are global to the perl process, and optag definitions cannot be altered or deleted once defined. locked in fr fr. ๐Ÿ”’

It is strongly recommended that applications using Opcode should use a leading capital letter on their tag names, since lowercase names are reserved for use by the Opcode module. If using Opcode within a module, you should prefix your tag names with the name of your module to ensure uniqueness and thus avoid clashes with other modules. ๐Ÿ’…

  • opmask_add (OPSET) โ€” Adds the supplied opset to the current opmask. Note that there is currently no mechanism for unmasking ops once they have been masked. This is intentional. no takebacksies. ๐Ÿšซโ†ฉ๏ธ
  • opmask โ€” Returns an opset corresponding to the current opmask.
  • opdesc (OP, ...) โ€” This takes a list of operator names and returns the corresponding list of operator descriptions.
  • opdump (PAT) โ€” Dumps to STDOUT a two column list of op names and op descriptions. If an optional pattern is given, then only lines which match the (case insensitive) pattern will be output. ๐Ÿ‘€

It's designed to be used as a handy command line utility:

perl -MOpcode=opdump -e opdump
perl -MOpcode=opdump -e 'opdump Eval'

๐ŸŒˆ Manipulating Opsets ๐ŸŒˆ

Opsets may be manipulated using the perl bit vector operators & (and), | (or), ^ (xor) and ~ (negate/invert). ๐Ÿงฎโœจ

However, you should never rely on the numerical position of any opcode within the opset. In other words, both sides of a bit vector operator should be opsets returned from Opcode functions. like, don't get cute with it. ๐Ÿ™…

Also, since the number of opcodes in your current version of perl might not be an exact multiple of eight, there may be unused bits in the last byte of an opset. This shouldn't cause any problems (Opcode functions ignore those extra bits), but it does mean that using the ~ operator will typically not produce the same 'physical' opset 'string' as the invert_opset function. sneaky little discrepancy, but that's just how it is. ๐Ÿ˜Œ


๐Ÿงš TO DO (maybe) ๐Ÿงš

(manifesting these into existence one day โœจ)

$bool = opset_eq($opset1, $opset2)  true if opsets are logically
                                    equivalent
$yes = opset_can($opset, @ops)      true if $opset has all @ops set

@diff = opset_diff($opset1, $opset2) => ('foo', '!bar', ...)

โœจ Predefined Opcode Tags โœจ

okay bestie, get ready for the tag list! these are like preset playlists of ops ๐Ÿ’…โœจ

:base_core

so this is the base core set โ€” the essential ops that make perl go brrr. they're the foundation, but even these can be used for resource attacks if you're not careful.

                null stub scalar pushmark wantarray const defined undef

                rv2sv sassign padsv_store

                rv2av aassign aelem aelemfast aelemfast_lex aslice kvaslice
                av2arylen aelemfastlex_store

                rv2hv helem hslice kvhslice each values keys exists delete
                aeach akeys avalues multideref argelem argdefelem argcheck
                multiparam paramtest paramstore

                preinc i_preinc predec i_predec postinc i_postinc
                postdec i_postdec int hex oct abs pow multiply i_multiply
                divide i_divide modulo i_modulo add i_add subtract i_subtract

                left_shift right_shift bit_and bit_xor bit_or nbit_and
                nbit_xor nbit_or sbit_and sbit_xor sbit_or negate i_negate not
                complement ncomplement scomplement

                lt i_lt gt i_gt le i_le ge i_ge eq i_eq ne i_ne ncmp i_ncmp
                slt sgt sle sge seq sne scmp
                isa

                substr substr_left vec stringify study pos length index
                rindex ord chr

                ucfirst lcfirst uc lc fc quotemeta trans transr chop schop
                chomp schomp

                match split qr

                list lslice splice push pop shift unshift reverse

                cond_expr flip flop andassign orassign dorassign and or dor xor
                helemexistsor

                warn die lineseq nextstate scope enter leave

                rv2cv anoncode prototype coreargs avhvswitch anonconst
                emptyavhv

                entersub leavesub leavesublv return method method_named
                method_super method_redir method_redir_super
                 -- XXX loops via recursion?

                cmpchain_and cmpchain_dup

                is_bool
                is_weak weaken unweaken

                leaveeval -- needed for Safe to operate, is safe
                             without entereval

                methstart initfield

:base_mem

these memory-related ops are not in :base_core because they can easily be used for a resource attack (like, eat all your memory ๐Ÿ˜ญ).

                concat multiconcat repeat join range

                anonlist anonhash

note that even with this optag, a memory resource attack might still be possible using only :base_core ops.

disabling these ops is a very heavy-handed way to try to prevent a memory resource attack. it's probable that a specific memory limit mechanism will be added to perl in the near future.

:base_loop

these loop ops are not in :base_core because they can easily be used for a resource attack (e.g., consume all available CPU time ๐Ÿ’€).

                grepstart grepwhile
                mapstart mapwhile
                anystart allstart anywhile
                enteriter iter
                enterloop leaveloop unstack
                last next redo
                goto

:base_io

these ops enable filehandle (rather than filename) based input and output. they're safe on the assumption that only pre-existing filehandles are available for use. usually, to create new filehandles, other ops such as open would need to be enabled, if you don't take into account the magical open of ARGV.

                readline rcatline getc read

                formline enterwrite leavewrite

                print say sysread syswrite send recv

                eof tell seek sysseek

                readdir telldir seekdir rewinddir

:base_orig

these are a hotchpotch of opcodes still waiting to be considered. basically the miscellaneous pile ๐Ÿ—‘๏ธ.

                gvsv gv gelem

                padsv padav padhv padcv padany padrange introcv clonecv

                once

                rv2gv refgen srefgen ref refassign lvref lvrefslice lvavref
                blessed refaddr reftype

                bless -- could be used to change ownership of objects
                         (reblessing)

                 regcmaybe regcreset regcomp subst substcont

                sprintf prtf -- can core dump

                crypt

                tie untie

                dbmopen dbmclose
                sselect select
                pipe_op sockpair

                getppid getpgrp setpgrp getpriority setpriority
                localtime gmtime

                entertry leavetry -- can be used to 'hide' fatal errors
                entertrycatch poptry catch leavetrycatch -- similar

                entergiven leavegiven
                enterwhen leavewhen
                break continue
                smartmatch

                pushdefer

                custom -- where should this go

                ceil floor

                is_tainted

:base_math

these ops are not in :base_core because of the risk of them being used to generate floating point exceptions (which would have to be caught using a $SIG{FPE} handler).

                atan2 sin cos exp log sqrt

and these ops are not in :base_core because they have an effect beyond the scope of the compartment.

                rand srand

:base_thread

these ops are related to multi-threading.

                lock

:default

a handy tag name for a reasonable default set of ops. (the current ops allowed are unstable while development continues. it will change.)

                :base_core :base_mem :base_loop :base_orig :base_thread

this list used to contain :base_io prior to Opcode 1.07.

if safety matters to you (and why else would you be using the Opcode module?) then you should not rely on the definition of this, or indeed any other, optag!

:filesys_read

reading filesystem stuff, go off ๐Ÿ’…

                stat lstat readlink

                ftatime ftblk ftchr ftctime ftdir fteexec fteowned
                fteread ftewrite ftfile ftis ftlink ftmtime ftpipe
                ftrexec ftrowned ftrread ftsgid ftsize ftsock ftsuid
                fttty ftzero ftrwrite ftsvtx

                fttext ftbinary

                fileno

:sys_db

system database ops, getting all up in your system's business ๐Ÿ˜

                ghbyname ghbyaddr ghostent shostent ehostent      -- hosts
                gnbyname gnbyaddr gnetent snetent enetent         -- networks
                gpbyname gpbynumber gprotoent sprotoent eprotoent -- protocols
                gsbyname gsbyport gservent sservent eservent      -- services

                gpwnam gpwuid gpwent spwent epwent getlogin       -- users
                ggrnam ggrgid ggrent sgrent egrent                -- groups

:browse

a handy tag name for a reasonable default set of ops beyond the :default optag. like :default (and indeed all the other optags) its current definition is unstable while development continues. it will change.

the :browse tag represents the next step beyond :default. it is a superset of the :default ops and adds :filesys_read and :sys_db. the intent being that scripts can access more (possibly sensitive) information about your system but not be able to change it.

                :default :filesys_read :sys_db

:filesys_open

opening files and dirs, slay ๐Ÿšชโœจ

                sysopen open close
                umask binmode

                open_dir closedir -- other dir ops are in :base_io

:filesys_write

writing files and dirs, careful bestie ๐Ÿ’…

                link unlink rename symlink truncate

                mkdir rmdir

                utime chmod chown

                fcntl -- not strictly filesys related, but possibly as
                         dangerous?

:subprocess

subprocess vibes, running other stuff ๐Ÿ’€

                backtick system

                fork

                wait waitpid

                glob -- access to Cshell via <`rm *`>

:ownprocess

own process ops, doing your own thing ๐Ÿ’…

                exec exit kill

                time tms -- could be used for timing attacks (paranoid?)

:others

this tag holds groups of assorted specialist opcodes that don't warrant having optags defined for them.

SystemV Interprocess Communications:

                msgctl msgget msgrcv msgsnd

                semctl semget semop

                shmctl shmget shmread shmwrite

:load

opcodes related to loading modules and getting info about calling environment and args.

                require dofile
                caller runcv classname

:still_to_be_decided

these are still up in the air, the committee is deciding ๐Ÿง

                chdir
                flock ioctl

                socket getpeername ssockopt
                bind connect listen accept shutdown gsockopt getsockname

                sleep alarm -- changes global timer state and signal handling
                sort -- assorted problems including core dumps
                tied -- can be used to access object implementing a tie
                pack unpack -- can be used to create/use memory pointers

                hintseval -- constant op holding eval hints

                entereval -- can be used to hide code from initial compile

                reset

                dbstate -- perl -d version of nextstate(ment) opcode

:dangerous

this tag is simply a bucket for opcodes that are unlikely to be used via a tag name but need to be tagged for completeness and documentation. dangerous, but make it fashion ๐Ÿ’…

                syscall dump chroot

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

  • ops โ€” perl pragma interface to Opcode module.
  • Safe โ€” Opcode and namespace limited execution compartments

๐Ÿ’– AUTHORS ๐Ÿ’–

Originally designed and implemented by Malcolm Beattie, mbeattie@sable.ox.ac.uk as part of Safe version 1.

Split out from Safe module version 1, named opcode tags and other changes added by Tim Bunce.


this page is from perl v5.44.0, dated 2026-10-17, and it's Opcode(3perl) โ€” slay, bestie ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: perl 5.44.0-1 ยท Artistic-1.0-Perl AND GPL-1.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms