Dynamic code generation in Perl
-
Upload
marian-marinov -
Category
Education
-
view
221 -
download
3
description
Transcript of Dynamic code generation in Perl
![Page 1: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/1.jpg)
Dynamic Code Generation
Marian HackMan Marinov
22.Nov.2014
Marian HackMan Marinov Dynamic Code Generation
![Page 2: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/2.jpg)
Why would you need that?
Marian HackMan Marinov Dynamic Code Generation
![Page 3: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/3.jpg)
Why would you need that?
Faster Development time
Marian HackMan Marinov Dynamic Code Generation
![Page 4: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/4.jpg)
Why would you need that?
Faster Development time
Easier interfaces
Marian HackMan Marinov Dynamic Code Generation
![Page 5: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/5.jpg)
Why would you need that?
Faster Development time
Easier interfaces
Abstraction
Marian HackMan Marinov Dynamic Code Generation
![Page 6: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/6.jpg)
Why would you need that?
Faster Development time
Easier interfaces
Abstraction
Obfuscation
Marian HackMan Marinov Dynamic Code Generation
![Page 7: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/7.jpg)
Why would you need that?
Faster Development time
Easier interfaces
Abstraction
Obfuscation/crypto
Performance
Marian HackMan Marinov Dynamic Code Generation
![Page 8: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/8.jpg)
I will focus mainly on PERFORMANCE
Marian HackMan Marinov Dynamic Code Generation
![Page 9: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/9.jpg)
Let’s start with a basic script
#!/usr/bin/perl
use warnings;
use strict;
sub DEBUG { 0; };
use constant ADEBUG => 0;
Marian HackMan Marinov Dynamic Code Generation
![Page 10: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/10.jpg)
The following would be optimised out
if (DEBUG) {
print "Debuging\n";
}
if (ADEBUG) {
print "Debuging A\n";
}
Marian HackMan Marinov Dynamic Code Generation
![Page 11: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/11.jpg)
What if we need to have something like this
if (defined($ARGV[0]) && $ARGV[0] eq ’ok’) {
sub DEBUG { 1; };
}
Marian HackMan Marinov Dynamic Code Generation
![Page 12: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/12.jpg)
What if we need to have something like this
if (defined($ARGV[0]) && $ARGV[0] eq ’ok’) {
sub DEBUG { 1; };
}
$ perl f.pl ok
Subroutine DEBUG redefined at f.pl line 9.
$
Marian HackMan Marinov Dynamic Code Generation
![Page 13: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/13.jpg)
Or something like this
if (defined($ARGV[0]) && $ARGV[0] eq ’ok’) {
ADEBUG => 1;
}
Marian HackMan Marinov Dynamic Code Generation
![Page 14: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/14.jpg)
Or something like this
if (defined($ARGV[0]) && $ARGV[0] eq ’ok’) {
ADEBUG => 1;
}
$ perl f.pl ok
Useless use of a constant ("ADEBUG") in void context at f.pl line 9.
Marian HackMan Marinov Dynamic Code Generation
![Page 15: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/15.jpg)
What if we start playing dirty :)
if (defined($ARGV[0]) && $ARGV[0] eq ’ok’) {
undef &ADEBUG;
use constant ADEBUG => 1;
}
$ perl f.pl ok
Constant subroutine main::ADEBUG redefined at /usr/share/perl5/constant.pm line 140.
Constant subroutine (anonymous) undefined at f.pl line 9.
Debuging
$
Marian HackMan Marinov Dynamic Code Generation
![Page 16: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/16.jpg)
Let’s declare this as futile
Figure : Resistance is futile
Marian HackMan Marinov Dynamic Code Generation
![Page 17: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/17.jpg)
So why did I tried to redifine a constant ?
What if we have a script which does:
while (my $line = <$fd>) {
if ($line =~ /RE/) { };
if ($line =~ /RE/) { };
....
....
if ($line =~ /RE/) { };
}
Marian HackMan Marinov Dynamic Code Generation
![Page 18: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/18.jpg)
So why did I tried to redifine a constant ?
We can update it to:
while (my $line = <$fd>) {
if ($config{’type1’} && $line =~ /RE/) { };
if ($config{’type2’} && $line =~ /RE/) { };
....
....
if ($config{’typeN’} && $line =~ /RE/) { };
}
Marian HackMan Marinov Dynamic Code Generation
![Page 19: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/19.jpg)
Hook::Filter
First looked at this
use Hook::Filter::RulePool qw(get_rule_pool);
get_rule_pool->add_rule("subname eq ’mydebug’ &&
from =~ /^My::Filthy::Attempt/");
->add_rule("from =~ /^My::Filthy::Attempt::func$/");
->add_rule("args(1) =~ /bob/");
Marian HackMan Marinov Dynamic Code Generation
![Page 20: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/20.jpg)
Welcome to perlfilter
Preprocessor like directives for Perl
Marian HackMan Marinov Dynamic Code Generation
![Page 21: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/21.jpg)
perlfilter
Filter::Util::Call - the main filter module
Marian HackMan Marinov Dynamic Code Generation
![Page 22: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/22.jpg)
perlfilter
Filter::Util::Call - the main filter module
Filter::Simple - simplified version of the Util::Call
Marian HackMan Marinov Dynamic Code Generation
![Page 23: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/23.jpg)
perlfilter
Filter::Util::Call - the main filter module
Filter::Simple - simplified version of the Util::Call
Filter::sh - filter the source trough cmd with shell
Marian HackMan Marinov Dynamic Code Generation
![Page 24: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/24.jpg)
perlfilter
Filter::Util::Call - the main filter module
Filter::Simple - simplified version of the Util::Call
Filter::sh - filter the source trough cmd with shell
Filter::exec - filter the source trough cmd
Marian HackMan Marinov Dynamic Code Generation
![Page 25: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/25.jpg)
perlfilter
Filter::Util::Call - the main filter module
Filter::Simple - simplified version of the Util::Call
Filter::sh - filter the source trough cmd with shell
Filter::exec - filter the source trough cmd
Filter::tee - copies all text from the line after the use
Marian HackMan Marinov Dynamic Code Generation
![Page 26: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/26.jpg)
perlfilter
Filter::Util::Call - the main filter module
Filter::Simple - simplified version of the Util::Call
Filter::sh - filter the source trough cmd with shell
Filter::exec - filter the source trough cmd
Filter::tee - copies all text from the line after the use
Filter::cpp - pipes all souce trough the cpp pre-processor
Marian HackMan Marinov Dynamic Code Generation
![Page 27: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/27.jpg)
Crypto filters
Filter::decrypt - example documentation for enc/decrypting
Marian HackMan Marinov Dynamic Code Generation
![Page 28: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/28.jpg)
Crypto filters
Filter::decrypt - example documentation for enc/decrypting
Filter::Rijndael - decrypting source filter based on Rijndaelencryption
Marian HackMan Marinov Dynamic Code Generation
![Page 29: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/29.jpg)
Crypto filters
Filter::decrypt - example documentation for enc/decrypting
Filter::Rijndael - decrypting source filter based on Rijndaelencryption
Filter::CBC - Source filter for Cipher Block Chaining
Marian HackMan Marinov Dynamic Code Generation
![Page 30: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/30.jpg)
Crypto filters
Filter::decrypt - example documentation for enc/decrypting
Filter::Rijndael - decrypting source filter based on Rijndaelencryption
Filter::CBC - Source filter for Cipher Block Chaining
Filter::Crypto::CryptFile & Filter::Crypto::Decrypt
Marian HackMan Marinov Dynamic Code Generation
![Page 31: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/31.jpg)
Filter::Util::Call part I
package Debug;
use strict;
use warnings;
use Filter::Util::Call;
use constant TRUE => 1;
use constant FALSE => 0;
sub import {
my ($type) = @_;
my (%context) = (
Enabled => defined $ENV{DEBUG},
InTraceBlock => FALSE,
Filename => (caller)[1],
LineNo => 0,
LastBegin => 0,
);
filter_add(bless \%context);
}Marian HackMan Marinov Dynamic Code Generation
![Page 32: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/32.jpg)
Filter::Util::Call part II
sub filter {
my ($self) = @_;
my ($status);
$status = filter_read();
++ $self->{LineNo};
if ($self->{InTraceBlock}) {
if (/^\s*##\s*DEBUG_END/) {
$self->{InTraceBlock} = FALSE;
}
s/^/#/ if ! $self->{Enabled};
} elsif ( /^\s*##\s*DEBUG_BEGIN/ ) {
$self->{InTraceBlock} = TRUE;
$self->{LastBegin} = $self->{LineNo};
}
return $status;
}
1;Marian HackMan Marinov Dynamic Code Generation
![Page 33: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/33.jpg)
Filter::Simple
package BANG;
use Filter::Simple;
FILTER {
s/BANG\s+BANG/die ’BANG’ if \$BANG/g;
};
1;
Marian HackMan Marinov Dynamic Code Generation
![Page 34: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/34.jpg)
Filter::cpp
use Filter::cpp;
#define FRED 1
$a = 2 + FRED;
print "a = $a\n";
#ifdef FRED
print "Hello FRED\n";
#else
print "Where is FRED\n";
#endif
Marian HackMan Marinov Dynamic Code Generation
![Page 35: Dynamic code generation in Perl](https://reader033.fdocuments.us/reader033/viewer/2022042700/55943ebc1a28abf15b8b46d3/html5/thumbnails/35.jpg)
Thank you
Questions ?
Additional readinghttp://www.bytelabs.org/pub/papers/hburg07.pdf
Contact me:
Marian HackMan Marinov
mm [maybe at] 1h.com
icq: 7556201
irc: irc.freenode.net HackMan
jabber: [email protected]
Marian HackMan Marinov Dynamic Code Generation