Integrate mainline.
[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
d6a466d7 9$VERSION = "1.05";
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
ed423f7a 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
354Note that despite the existance of this optag a memory resource attack
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
377filehandles are available for use. To create new filehandles other ops
378such as open would need to be enabled.
379
380 readline rcatline getc read
381
382 formline enterwrite leavewrite
383
96e4d5b1 384 print sysread syswrite send recv
385
8903cb82 386 eof tell seek sysseek
6badd1a5 387
388 readdir telldir seekdir rewinddir
389
390=item :base_orig
391
392These are a hotchpotch of opcodes still waiting to be considered
393
394 gvsv gv gelem
395
396 padsv padav padhv padany
397
398 rv2gv refgen srefgen ref
399
400 bless -- could be used to change ownership of objects (reblessing)
401
2cd61cdb 402 pushre regcmaybe regcreset regcomp subst substcont
6badd1a5 403
404 sprintf prtf -- can core dump
405
406 crypt
407
408 tie untie
409
410 dbmopen dbmclose
411 sselect select
412 pipe_op sockpair
413
414 getppid getpgrp setpgrp getpriority setpriority localtime gmtime
415
416 entertry leavetry -- can be used to 'hide' fatal errors
417
53e06cf0 418 custom -- where should this go
419
6badd1a5 420=item :base_math
421
422These ops are not included in :base_core because of the risk of them being
423used to generate floating point exceptions (which would have to be caught
424using a $SIG{FPE} handler).
425
426 atan2 sin cos exp log sqrt
427
428These ops are not included in :base_core because they have an effect
429beyond the scope of the compartment.
430
431 rand srand
432
1f5895a1 433=item :base_thread
434
554b3eca 435These ops are related to multi-threading.
1f5895a1 436
2faa37cc 437 lock threadsv
1f5895a1 438
6badd1a5 439=item :default
440
441A handy tag name for a I<reasonable> default set of ops. (The current ops
442allowed are unstable while development continues. It will change.)
443
5ddb6eab 444 :base_core :base_mem :base_loop :base_io :base_orig :base_thread
6badd1a5 445
446If safety matters to you (and why else would you be using the Opcode module?)
447then you should not rely on the definition of this, or indeed any other, optag!
448
449
450=item :filesys_read
451
452 stat lstat readlink
453
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
457
458 fttext ftbinary
459
460 fileno
461
462=item :sys_db
463
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
468
469 gpwnam gpwuid gpwent spwent epwent getlogin -- users
470 ggrnam ggrgid ggrent sgrent egrent -- groups
471
472=item :browse
473
474A 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
476current definition is unstable while development continues. It will change.
477
478The :browse tag represents the next step beyond :default. It it a
479superset of the :default ops and adds :filesys_read the :sys_db.
480The intent being that scripts can access more (possibly sensitive)
481information about your system but not be able to change it.
482
483 :default :filesys_read :sys_db
484
485=item :filesys_open
486
487 sysopen open close
488 umask binmode
489
490 open_dir closedir -- other dir ops are in :base_io
491
492=item :filesys_write
493
494 link unlink rename symlink truncate
495
496 mkdir rmdir
497
498 utime chmod chown
499
500 fcntl -- not strictly filesys related, but possibly as dangerous?
501
502=item :subprocess
503
504 backtick system
505
506 fork
507
508 wait waitpid
509
f812a825 510 glob -- access to Cshell via <`rm *`>
511
6badd1a5 512=item :ownprocess
513
514 exec exit kill
515
516 time tms -- could be used for timing attacks (paranoid?)
517
518=item :others
519
520This tag holds groups of assorted specialist opcodes that don't warrant
521having optags defined for them.
522
523SystemV Interprocess Communications:
524
525 msgctl msgget msgrcv msgsnd
526
527 semctl semget semop
528
529 shmctl shmget shmread shmwrite
530
531=item :still_to_be_decided
532
533 chdir
534 flock ioctl
535
536 socket getpeername ssockopt
537 bind connect listen accept shutdown gsockopt getsockname
538
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
543
544 entereval -- can be used to hide code from initial compile
545 require dofile
546
547 caller -- get info about calling environment and args
548
549 reset
550
551 dbstate -- perl -d version of nextstate(ment) opcode
552
553=item :dangerous
554
555This tag is simply a bucket for opcodes that are unlikely to be used via
556a tag name but need to be tagged for completness and documentation.
557
558 syscall dump chroot
559
560
561=back
562
563=head1 SEE ALSO
564
565ops(3) -- perl pragma interface to Opcode module.
566
567Safe(3) -- Opcode and namespace limited execution compartments
568
569=head1 AUTHORS
570
571Originally designed and implemented by Malcolm Beattie,
572mbeattie@sable.ox.ac.uk as part of Safe version 1.
573
574Split out from Safe module version 1, named opcode tags and other
7b8d334a 575changes added by Tim Bunce.
6badd1a5 576
577=cut
578