7 our($VERSION, $XS_VERSION, @ISA, @EXPORT_OK);
20 opset_to_ops opset_to_hex invert_opset
21 empty_opset full_opset
22 opdesc opcodes opmask define_optag
23 opmask_add verify_opset opdump
32 XSLoader::load 'Opcode', $XS_VERSION;
36 sub ops_to_opset { opset @_ } # alias for old name
38 sub opset_to_hex ($) {
39 return "(invalid opset)" unless verify_opset($_[0]);
45 # handy utility: perl -MOpcode=opdump -e 'opdump File'
46 foreach(opset_to_ops(full_opset)) {
47 my $op = sprintf " %12s %s\n", $_, opdesc($_);
48 next if defined $pat and $op !~ m/$pat/i;
57 @all{opset_to_ops(full_opset)} = (); # keys only
60 local($/) = "\n=cut"; # skip to optags definition section
62 $/ = "\n="; # now read in 'pod section' chunks
64 next unless m/^item\s+(:\w+)/;
67 # Split into lines, keep only indented lines
68 my @lines = grep { m/^\s/ } split(/\n/);
69 foreach (@lines) { s/--.*// } # delete comments
70 my @ops = map { split ' ' } @lines; # get op words
73 warn "$tag - $_ already tagged in $seen{$_}\n" if $seen{$_};
77 # opset will croak on invalid names
78 define_optag($tag, opset(@ops));
81 warn "Untagged opnames: ".join(' ',keys %all)."\n" if %all;
91 Opcode - Disable named opcodes when compiling perl code
100 Perl code is always compiled into an internal format before execution.
102 Evaluating perl code (e.g. via "eval" or "do 'file'") causes
103 the code to be compiled into an internal format and then,
104 provided there was no error in the compilation, executed.
105 The internal format is based on many distinct I<opcodes>.
107 By default no opmask is in effect and any code can be compiled.
109 The Opcode module allow you to define an I<operator mask> to be in
110 effect when perl I<next> compiles any code. Attempting to compile code
111 which contains a masked opcode will cause the compilation to fail
112 with an error. The code will not be executed.
116 The Opcode module is not usually used directly. See the ops pragma and
117 Safe modules for more typical uses.
121 The authors make B<no warranty>, implied or otherwise, about the
122 suitability of this software for safety or security purposes.
124 The authors shall not in any case be liable for special, incidental,
125 consequential, indirect or other similar damages arising from the use
128 Your mileage will vary. If in any doubt B<do not use it>.
131 =head1 Operator Names and Operator Lists
133 The canonical list of operator names is the contents of the array
134 PL_op_name defined and initialised in file F<opcode.h> of the Perl
135 source distribution (and installed into the perl library).
137 Each operator has both a terse name (its opname) and a more verbose or
138 recognisable descriptive name. The opdesc function can be used to
139 return a list of descriptions for a list of operators.
141 Many of the functions and methods listed below take a list of
142 operators as parameters. Most operator lists can be made up of several
143 types of element. Each element can be one of
147 =item an operator name (opname)
149 Operator names are typically small lowercase words like enterloop,
150 leaveloop, last, next, redo etc. Sometimes they are rather cryptic
151 like gv2cv, i_ncmp and ftsvtx.
153 =item an operator tag name (optag)
155 Operator tags can be used to refer to groups (or sets) of operators.
156 Tag names always begin with a colon. The Opcode module defines several
157 optags and the user can define others using the define_optag function.
159 =item a negated opname or optag
161 An opname or optag can be prefixed with an exclamation mark, e.g., !mkdir.
162 Negating an opname or optag means remove the corresponding ops from the
163 accumulated set of ops at that point.
165 =item an operator set (opset)
167 An I<opset> as a binary string of approximately 44 bytes which holds a
168 set or zero or more operators.
170 The opset and opset_to_ops functions can be used to convert from
171 a list of operators to an opset and I<vice versa>.
173 Wherever a list of operators can be given you can use one or more opsets.
174 See also Manipulating Opsets below.
179 =head1 Opcode Functions
181 The Opcode package contains functions for manipulating operator names
182 tags and sets. All are available for export by the package.
188 In a scalar context opcodes returns the number of opcodes in this
189 version of perl (around 350 for perl-5.7.0).
191 In a list context it returns a list of all the operator names.
192 (Not yet implemented, use @names = opset_to_ops(full_opset).)
194 =item opset (OP, ...)
196 Returns an opset containing the listed operators.
198 =item opset_to_ops (OPSET)
200 Returns a list of operator names corresponding to those operators in
203 =item opset_to_hex (OPSET)
205 Returns a string representation of an opset. Can be handy for debugging.
209 Returns an opset which includes all operators.
213 Returns an opset which contains no operators.
215 =item invert_opset (OPSET)
217 Returns an opset which is the inverse set of the one supplied.
219 =item verify_opset (OPSET, ...)
221 Returns true if the supplied opset looks like a valid opset (is the
222 right length etc) otherwise it returns false. If an optional second
223 parameter is true then verify_opset will croak on an invalid opset
224 instead of returning false.
226 Most of the other Opcode functions call verify_opset automatically
227 and will croak if given an invalid opset.
229 =item define_optag (OPTAG, OPSET)
231 Define OPTAG as a symbolic name for OPSET. Optag names always start
234 The optag name used must not be defined already (define_optag will
235 croak if it is already defined). Optag names are global to the perl
236 process and optag definitions cannot be altered or deleted once
239 It is strongly recommended that applications using Opcode should use a
240 leading capital letter on their tag names since lowercase names are
241 reserved for use by the Opcode module. If using Opcode within a module
242 you should prefix your tags names with the name of your module to
243 ensure uniqueness and thus avoid clashes with other modules.
245 =item opmask_add (OPSET)
247 Adds the supplied opset to the current opmask. Note that there is
248 currently I<no> mechanism for unmasking ops once they have been masked.
253 Returns an opset corresponding to the current opmask.
255 =item opdesc (OP, ...)
257 This takes a list of operator names and returns the corresponding list
258 of operator descriptions.
262 Dumps to STDOUT a two column list of op names and op descriptions.
263 If an optional pattern is given then only lines which match the
264 (case insensitive) pattern will be output.
266 It's designed to be used as a handy command line utility:
268 perl -MOpcode=opdump -e opdump
269 perl -MOpcode=opdump -e 'opdump Eval'
273 =head1 Manipulating Opsets
275 Opsets may be manipulated using the perl bit vector operators & (and), | (or),
276 ^ (xor) and ~ (negate/invert).
278 However you should never rely on the numerical position of any opcode
279 within the opset. In other words both sides of a bit vector operator
280 should be opsets returned from Opcode functions.
282 Also, since the number of opcodes in your current version of perl might
283 not be an exact multiple of eight, there may be unused bits in the last
284 byte of an upset. This should not cause any problems (Opcode functions
285 ignore those extra bits) but it does mean that using the ~ operator
286 will typically not produce the same 'physical' opset 'string' as the
287 invert_opset function.
292 $bool = opset_eq($opset1, $opset2) true if opsets are logically eqiv
294 $yes = opset_can($opset, @ops) true if $opset has all @ops set
296 @diff = opset_diff($opset1, $opset2) => ('foo', '!bar', ...)
300 # the =cut above is used by _init_optags() to get here quickly
302 =head1 Predefined Opcode Tags
308 null stub scalar pushmark wantarray const defined undef
312 rv2av aassign aelem aelemfast aslice av2arylen
314 rv2hv helem hslice each values keys exists delete
316 preinc i_preinc predec i_predec postinc i_postinc postdec i_postdec
317 int hex oct abs pow multiply i_multiply divide i_divide
318 modulo i_modulo add i_add subtract i_subtract
320 left_shift right_shift bit_and bit_xor bit_or negate i_negate
323 lt i_lt gt i_gt le i_le ge i_ge eq i_eq ne i_ne ncmp i_ncmp
324 slt sgt sle sge seq sne scmp
326 substr vec stringify study pos length index rindex ord chr
328 ucfirst lcfirst uc lc quotemeta trans chop schop chomp schomp
332 list lslice splice push pop shift unshift reverse
334 cond_expr flip flop andassign orassign and or xor
336 warn die lineseq nextstate scope enter leave setstate
338 rv2cv anoncode prototype
340 entersub leavesub leavesublv return method method_named -- XXX loops via recursion?
342 leaveeval -- needed for Safe to operate, is safe without entereval
346 These memory related ops are not included in :base_core because they
347 can easily be used to implement a resource attack (e.g., consume all
350 concat repeat join range
354 Note that despite the existance of this optag a memory resource attack
355 may still be possible using only :base_core ops.
357 Disabling these ops is a I<very> heavy handed way to attempt to prevent
358 a memory resource attack. It's probable that a specific memory limit
359 mechanism will be added to perl in the near future.
363 These loop ops are not included in :base_core because they can easily be
364 used to implement a resource attack (e.g., consume all available CPU time).
369 enterloop leaveloop unstack
375 These ops enable I<filehandle> (rather than filename) based input and
376 output. These are safe on the assumption that only pre-existing
377 filehandles are available for use. To create new filehandles other ops
378 such as open would need to be enabled.
380 readline rcatline getc read
382 formline enterwrite leavewrite
384 print sysread syswrite send recv
386 eof tell seek sysseek
388 readdir telldir seekdir rewinddir
392 These are a hotchpotch of opcodes still waiting to be considered
396 padsv padav padhv padany
398 rv2gv refgen srefgen ref
400 bless -- could be used to change ownership of objects (reblessing)
402 pushre regcmaybe regcreset regcomp subst substcont
404 sprintf prtf -- can core dump
414 getppid getpgrp setpgrp getpriority setpriority localtime gmtime
416 entertry leavetry -- can be used to 'hide' fatal errors
418 custom -- where should this go
422 These ops are not included in :base_core because of the risk of them being
423 used to generate floating point exceptions (which would have to be caught
424 using a $SIG{FPE} handler).
426 atan2 sin cos exp log sqrt
428 These ops are not included in :base_core because they have an effect
429 beyond the scope of the compartment.
435 These ops are related to multi-threading.
441 A handy tag name for a I<reasonable> default set of ops. (The current ops
442 allowed are unstable while development continues. It will change.)
444 :base_core :base_mem :base_loop :base_io :base_orig :base_thread
446 If safety matters to you (and why else would you be using the Opcode module?)
447 then you should not rely on the definition of this, or indeed any other, optag!
454 ftatime ftblk ftchr ftctime ftdir fteexec fteowned fteread
455 ftewrite ftfile ftis ftlink ftmtime ftpipe ftrexec ftrowned
456 ftrread ftsgid ftsize ftsock ftsuid fttty ftzero ftrwrite ftsvtx
464 ghbyname ghbyaddr ghostent shostent ehostent -- hosts
465 gnbyname gnbyaddr gnetent snetent enetent -- networks
466 gpbyname gpbynumber gprotoent sprotoent eprotoent -- protocols
467 gsbyname gsbyport gservent sservent eservent -- services
469 gpwnam gpwuid gpwent spwent epwent getlogin -- users
470 ggrnam ggrgid ggrent sgrent egrent -- groups
474 A handy tag name for a I<reasonable> default set of ops beyond the
475 :default optag. Like :default (and indeed all the other optags) its
476 current definition is unstable while development continues. It will change.
478 The :browse tag represents the next step beyond :default. It it a
479 superset of the :default ops and adds :filesys_read the :sys_db.
480 The intent being that scripts can access more (possibly sensitive)
481 information about your system but not be able to change it.
483 :default :filesys_read :sys_db
490 open_dir closedir -- other dir ops are in :base_io
494 link unlink rename symlink truncate
500 fcntl -- not strictly filesys related, but possibly as dangerous?
510 glob -- access to Cshell via <`rm *`>
516 time tms -- could be used for timing attacks (paranoid?)
520 This tag holds groups of assorted specialist opcodes that don't warrant
521 having optags defined for them.
523 SystemV Interprocess Communications:
525 msgctl msgget msgrcv msgsnd
529 shmctl shmget shmread shmwrite
531 =item :still_to_be_decided
536 socket getpeername ssockopt
537 bind connect listen accept shutdown gsockopt getsockname
539 sleep alarm -- changes global timer state and signal handling
540 sort -- assorted problems including core dumps
541 tied -- can be used to access object implementing a tie
542 pack unpack -- can be used to create/use memory pointers
544 entereval -- can be used to hide code from initial compile
547 caller -- get info about calling environment and args
551 dbstate -- perl -d version of nextstate(ment) opcode
555 This tag is simply a bucket for opcodes that are unlikely to be used via
556 a tag name but need to be tagged for completness and documentation.
565 ops(3) -- perl pragma interface to Opcode module.
567 Safe(3) -- Opcode and namespace limited execution compartments
571 Originally designed and implemented by Malcolm Beattie,
572 mbeattie@sable.ox.ac.uk as part of Safe version 1.
574 Split out from Safe module version 1, named opcode tags and other
575 changes added by Tim Bunce.