Add Exporter in the dual-lived module list
[p5sagit/p5-mst-13.2.git] / ext / Opcode / Opcode.pm
CommitLineData
6badd1a5 1package Opcode;
2
3b825e41 3use 5.006_001;
6badd1a5 4
b75c8c73 5use strict;
6
17f410f9 7our($VERSION, $XS_VERSION, @ISA, @EXPORT_OK);
6badd1a5 8
98225a64 9$VERSION = "1.08";
e858de61 10$XS_VERSION = "1.03";
6badd1a5 11
6badd1a5 12use Carp;
13use Exporter ();
9426adcd 14use XSLoader ();
6badd1a5 15
16BEGIN {
b75c8c73 17 @ISA = qw(Exporter);
6badd1a5 18 @EXPORT_OK = qw(
19 opset ops_to_opset
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
24 );
25}
26
68dc0745 27sub opset (;@);
28sub opset_to_hex ($);
29sub opdump (;$);
6badd1a5 30use subs @EXPORT_OK;
31
9426adcd 32XSLoader::load 'Opcode', $XS_VERSION;
6badd1a5 33
34_init_optags();
35
68dc0745 36sub ops_to_opset { opset @_ } # alias for old name
6badd1a5 37
38sub opset_to_hex ($) {
39 return "(invalid opset)" unless verify_opset($_[0]);
40 unpack("h*",$_[0]);
41}
42
43sub opdump (;$) {
44 my $pat = shift;
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;
49 print $op;
50 }
51}
52
53
54
55sub _init_optags {
56 my(%all, %seen);
57 @all{opset_to_ops(full_opset)} = (); # keys only
58
7a57407b 59 local($_);
6badd1a5 60 local($/) = "\n=cut"; # skip to optags definition section
61 <DATA>;
62 $/ = "\n="; # now read in 'pod section' chunks
63 while(<DATA>) {
64 next unless m/^item\s+(:\w+)/;
65 my $tag = $1;
66
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
71
72 foreach(@ops) {
73 warn "$tag - $_ already tagged in $seen{$_}\n" if $seen{$_};
74 $seen{$_} = $tag;
75 delete $all{$_};
76 }
77 # opset will croak on invalid names
78 define_optag($tag, opset(@ops));
79 }
80 close(DATA);
81 warn "Untagged opnames: ".join(' ',keys %all)."\n" if %all;
82}
83
84
851;
86
87__DATA__
88
89=head1 NAME
90
91Opcode - Disable named opcodes when compiling perl code
92
93=head1 SYNOPSIS
94
95 use Opcode;
96
97
98=head1 DESCRIPTION
99
100Perl code is always compiled into an internal format before execution.
101
102Evaluating perl code (e.g. via "eval" or "do 'file'") causes
103the code to be compiled into an internal format and then,
104provided there was no error in the compilation, executed.
105The internal format is based on many distinct I<opcodes>.
106
107By default no opmask is in effect and any code can be compiled.
108
109The Opcode module allow you to define an I<operator mask> to be in
110effect when perl I<next> compiles any code. Attempting to compile code
111which contains a masked opcode will cause the compilation to fail
112with an error. The code will not be executed.
113
114=head1 NOTE
115
116The Opcode module is not usually used directly. See the ops pragma and
117Safe modules for more typical uses.
118
119=head1 WARNING
120
121The authors make B<no warranty>, implied or otherwise, about the
122suitability of this software for safety or security purposes.
123
124The authors shall not in any case be liable for special, incidental,
125consequential, indirect or other similar damages arising from the use
126of this software.
127
128Your mileage will vary. If in any doubt B<do not use it>.
129
130
131=head1 Operator Names and Operator Lists
132
133The canonical list of operator names is the contents of the array
4369b173 134PL_op_name defined and initialised in file F<opcode.h> of the Perl
6badd1a5 135source distribution (and installed into the perl library).
136
137Each operator has both a terse name (its opname) and a more verbose or
138recognisable descriptive name. The opdesc function can be used to
139return a list of descriptions for a list of operators.
140
141Many of the functions and methods listed below take a list of
142operators as parameters. Most operator lists can be made up of several
143types of element. Each element can be one of
144
145=over 8
146
147=item an operator name (opname)
148
149Operator names are typically small lowercase words like enterloop,
150leaveloop, last, next, redo etc. Sometimes they are rather cryptic
151like gv2cv, i_ncmp and ftsvtx.
152
153=item an operator tag name (optag)
154
155Operator tags can be used to refer to groups (or sets) of operators.
7b8d334a 156Tag names always begin with a colon. The Opcode module defines several
6badd1a5 157optags and the user can define others using the define_optag function.
158
159=item a negated opname or optag
160
161An opname or optag can be prefixed with an exclamation mark, e.g., !mkdir.
162Negating an opname or optag means remove the corresponding ops from the
163accumulated set of ops at that point.
164
165=item an operator set (opset)
166
7c011d3a 167An I<opset> as a binary string of approximately 44 bytes which holds a
6badd1a5 168set or zero or more operators.
169
170The opset and opset_to_ops functions can be used to convert from
171a list of operators to an opset and I<vice versa>.
172
173Wherever a list of operators can be given you can use one or more opsets.
174See also Manipulating Opsets below.
175
176=back
177
178
179=head1 Opcode Functions
180
181The Opcode package contains functions for manipulating operator names
182tags and sets. All are available for export by the package.
183
184=over 8
185
186=item opcodes
187
188In a scalar context opcodes returns the number of opcodes in this
7c011d3a 189version of perl (around 350 for perl-5.7.0).
6badd1a5 190
191In a list context it returns a list of all the operator names.
192(Not yet implemented, use @names = opset_to_ops(full_opset).)
193
194=item opset (OP, ...)
195
196Returns an opset containing the listed operators.
197
198=item opset_to_ops (OPSET)
199
200Returns a list of operator names corresponding to those operators in
201the set.
202
203=item opset_to_hex (OPSET)
204
205Returns a string representation of an opset. Can be handy for debugging.
206
207=item full_opset
208
209Returns an opset which includes all operators.
210
211=item empty_opset
212
213Returns an opset which contains no operators.
214
215=item invert_opset (OPSET)
216
217Returns an opset which is the inverse set of the one supplied.
218
219=item verify_opset (OPSET, ...)
220
221Returns true if the supplied opset looks like a valid opset (is the
222right length etc) otherwise it returns false. If an optional second
223parameter is true then verify_opset will croak on an invalid opset
224instead of returning false.
225
226Most of the other Opcode functions call verify_opset automatically
227and will croak if given an invalid opset.
228
229=item define_optag (OPTAG, OPSET)
230
231Define OPTAG as a symbolic name for OPSET. Optag names always start
232with a colon C<:>.
233
234The optag name used must not be defined already (define_optag will
235croak if it is already defined). Optag names are global to the perl
236process and optag definitions cannot be altered or deleted once
237defined.
238
239It is strongly recommended that applications using Opcode should use a
240leading capital letter on their tag names since lowercase names are
241reserved for use by the Opcode module. If using Opcode within a module
242you should prefix your tags names with the name of your module to
243ensure uniqueness and thus avoid clashes with other modules.
244
245=item opmask_add (OPSET)
246
247Adds the supplied opset to the current opmask. Note that there is
248currently I<no> mechanism for unmasking ops once they have been masked.
249This is intentional.
250
251=item opmask
252
253Returns an opset corresponding to the current opmask.
254
255=item opdesc (OP, ...)
256
257This takes a list of operator names and returns the corresponding list
258of operator descriptions.
259
260=item opdump (PAT)
261
262Dumps to STDOUT a two column list of op names and op descriptions.
263If an optional pattern is given then only lines which match the
264(case insensitive) pattern will be output.
265
266It's designed to be used as a handy command line utility:
267
268 perl -MOpcode=opdump -e opdump
269 perl -MOpcode=opdump -e 'opdump Eval'
270
271=back
272
273=head1 Manipulating Opsets
274
275Opsets may be manipulated using the perl bit vector operators & (and), | (or),
276^ (xor) and ~ (negate/invert).
277
278However you should never rely on the numerical position of any opcode
279within the opset. In other words both sides of a bit vector operator
280should be opsets returned from Opcode functions.
281
282Also, since the number of opcodes in your current version of perl might
283not be an exact multiple of eight, there may be unused bits in the last
284byte of an upset. This should not cause any problems (Opcode functions
285ignore those extra bits) but it does mean that using the ~ operator
286will typically not produce the same 'physical' opset 'string' as the
287invert_opset function.
288
289
290=head1 TO DO (maybe)
291
292 $bool = opset_eq($opset1, $opset2) true if opsets are logically eqiv
293
294 $yes = opset_can($opset, @ops) true if $opset has all @ops set
295
296 @diff = opset_diff($opset1, $opset2) => ('foo', '!bar', ...)
297
298=cut
299
300# the =cut above is used by _init_optags() to get here quickly
301
302=head1 Predefined Opcode Tags
303
304=over 5
305
306=item :base_core
307
308 null stub scalar pushmark wantarray const defined undef
309
310 rv2sv sassign
311
312 rv2av aassign aelem aelemfast aslice av2arylen
313
314 rv2hv helem hslice each values keys exists delete
315
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
319
320 left_shift right_shift bit_and bit_xor bit_or negate i_negate
321 not complement
322
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
325
326 substr vec stringify study pos length index rindex ord chr
327
328 ucfirst lcfirst uc lc quotemeta trans chop schop chomp schomp
329
8782bef2 330 match split qr
6badd1a5 331
332 list lslice splice push pop shift unshift reverse
333
c963b151 334 cond_expr flip flop andassign orassign dorassign and or dor xor
6badd1a5 335
7399586d 336 warn die lineseq nextstate scope enter leave setstate
6badd1a5 337
338 rv2cv anoncode prototype
339
cd06dffe 340 entersub leavesub leavesublv return method method_named -- XXX loops via recursion?
6badd1a5 341
342 leaveeval -- needed for Safe to operate, is safe without entereval
343
344=item :base_mem
345
346These memory related ops are not included in :base_core because they
347can easily be used to implement a resource attack (e.g., consume all
348available memory).
349
350 concat repeat join range
351
352 anonlist anonhash
353
3c4b39be 354Note that despite the existence of this optag a memory resource attack
6badd1a5 355may still be possible using only :base_core ops.
356
357Disabling these ops is a I<very> heavy handed way to attempt to prevent
358a memory resource attack. It's probable that a specific memory limit
359mechanism will be added to perl in the near future.
360
361=item :base_loop
362
363These loop ops are not included in :base_core because they can easily be
364used to implement a resource attack (e.g., consume all available CPU time).
365
366 grepstart grepwhile
367 mapstart mapwhile
368 enteriter iter
e897d888 369 enterloop leaveloop unstack
6badd1a5 370 last next redo
371 goto
372
373=item :base_io
374
375These ops enable I<filehandle> (rather than filename) based input and
376output. These are safe on the assumption that only pre-existing
e866b74b 377filehandles are available for use. Usually, to create new filehandles
378other ops such as open would need to be enabled, if you don't take into
379account the magical open of ARGV.
6badd1a5 380
381 readline rcatline getc read
382
383 formline enterwrite leavewrite
384
0d863452 385 print say sysread syswrite send recv
96e4d5b1 386
8903cb82 387 eof tell seek sysseek
6badd1a5 388
389 readdir telldir seekdir rewinddir
390
391=item :base_orig
392
393These are a hotchpotch of opcodes still waiting to be considered
394
395 gvsv gv gelem
396
397 padsv padav padhv padany
398
399 rv2gv refgen srefgen ref
400
401 bless -- could be used to change ownership of objects (reblessing)
402
2cd61cdb 403 pushre regcmaybe regcreset regcomp subst substcont
6badd1a5 404
405 sprintf prtf -- can core dump
406
407 crypt
408
409 tie untie
410
411 dbmopen dbmclose
412 sselect select
413 pipe_op sockpair
414
415 getppid getpgrp setpgrp getpriority setpriority localtime gmtime
416
417 entertry leavetry -- can be used to 'hide' fatal errors
418
0d863452 419 entergiven leavegiven
420 enterwhen leavewhen
421 break continue
422 smartmatch
423
53e06cf0 424 custom -- where should this go
425
6badd1a5 426=item :base_math
427
428These ops are not included in :base_core because of the risk of them being
429used to generate floating point exceptions (which would have to be caught
430using a $SIG{FPE} handler).
431
432 atan2 sin cos exp log sqrt
433
434These ops are not included in :base_core because they have an effect
435beyond the scope of the compartment.
436
437 rand srand
438
1f5895a1 439=item :base_thread
440
554b3eca 441These ops are related to multi-threading.
1f5895a1 442
2faa37cc 443 lock threadsv
1f5895a1 444
6badd1a5 445=item :default
446
447A handy tag name for a I<reasonable> default set of ops. (The current ops
448allowed are unstable while development continues. It will change.)
449
e866b74b 450 :base_core :base_mem :base_loop :base_orig :base_thread
451
452This list used to contain :base_io prior to Opcode 1.07.
6badd1a5 453
454If safety matters to you (and why else would you be using the Opcode module?)
455then you should not rely on the definition of this, or indeed any other, optag!
456
6badd1a5 457=item :filesys_read
458
459 stat lstat readlink
460
461 ftatime ftblk ftchr ftctime ftdir fteexec fteowned fteread
462 ftewrite ftfile ftis ftlink ftmtime ftpipe ftrexec ftrowned
463 ftrread ftsgid ftsize ftsock ftsuid fttty ftzero ftrwrite ftsvtx
464
465 fttext ftbinary
466
467 fileno
468
469=item :sys_db
470
471 ghbyname ghbyaddr ghostent shostent ehostent -- hosts
472 gnbyname gnbyaddr gnetent snetent enetent -- networks
473 gpbyname gpbynumber gprotoent sprotoent eprotoent -- protocols
474 gsbyname gsbyport gservent sservent eservent -- services
475
476 gpwnam gpwuid gpwent spwent epwent getlogin -- users
477 ggrnam ggrgid ggrent sgrent egrent -- groups
478
479=item :browse
480
481A handy tag name for a I<reasonable> default set of ops beyond the
482:default optag. Like :default (and indeed all the other optags) its
483current definition is unstable while development continues. It will change.
484
485The :browse tag represents the next step beyond :default. It it a
486superset of the :default ops and adds :filesys_read the :sys_db.
487The intent being that scripts can access more (possibly sensitive)
488information about your system but not be able to change it.
489
490 :default :filesys_read :sys_db
491
492=item :filesys_open
493
494 sysopen open close
495 umask binmode
496
497 open_dir closedir -- other dir ops are in :base_io
498
499=item :filesys_write
500
501 link unlink rename symlink truncate
502
503 mkdir rmdir
504
505 utime chmod chown
506
507 fcntl -- not strictly filesys related, but possibly as dangerous?
508
509=item :subprocess
510
511 backtick system
512
513 fork
514
515 wait waitpid
516
f812a825 517 glob -- access to Cshell via <`rm *`>
518
6badd1a5 519=item :ownprocess
520
521 exec exit kill
522
523 time tms -- could be used for timing attacks (paranoid?)
524
525=item :others
526
527This tag holds groups of assorted specialist opcodes that don't warrant
528having optags defined for them.
529
530SystemV Interprocess Communications:
531
532 msgctl msgget msgrcv msgsnd
533
534 semctl semget semop
535
536 shmctl shmget shmread shmwrite
537
538=item :still_to_be_decided
539
540 chdir
541 flock ioctl
542
543 socket getpeername ssockopt
544 bind connect listen accept shutdown gsockopt getsockname
545
546 sleep alarm -- changes global timer state and signal handling
547 sort -- assorted problems including core dumps
548 tied -- can be used to access object implementing a tie
549 pack unpack -- can be used to create/use memory pointers
550
551 entereval -- can be used to hide code from initial compile
552 require dofile
553
554 caller -- get info about calling environment and args
555
556 reset
557
558 dbstate -- perl -d version of nextstate(ment) opcode
559
560=item :dangerous
561
562This tag is simply a bucket for opcodes that are unlikely to be used via
3c4b39be 563a tag name but need to be tagged for completeness and documentation.
6badd1a5 564
565 syscall dump chroot
566
567
568=back
569
570=head1 SEE ALSO
571
572ops(3) -- perl pragma interface to Opcode module.
573
574Safe(3) -- Opcode and namespace limited execution compartments
575
576=head1 AUTHORS
577
578Originally designed and implemented by Malcolm Beattie,
579mbeattie@sable.ox.ac.uk as part of Safe version 1.
580
581Split out from Safe module version 1, named opcode tags and other
7b8d334a 582changes added by Tim Bunce.
6badd1a5 583
584=cut
585