Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Synthetic strings dereference arbitrary pointers

by diotalevi (Canon)
on Aug 02, 2004 at 20:54 UTC ( #379428=snippet: print w/ replies, xml ) Need Help??

Description: Constructs synthetic strings pointing at any address.
use B;
use 5.008001;

=pod

=head1 SYNOPSIS

 no strict 'refs';
 my $pointer = &{"a great evil"}( 0x1800000, 1_000_000 );
 print $pointer->{'text'};

=cut

{
    no strict 'refs';
    # ARG1 address to dereference
    # ARG2  length of string to construct
    *{"a great evil"} = sub 
    {
        my $address = 0 + shift;
        my $length = 0 + shift;

        # Create a host life form. We're about to violate use it as
        # a template
        my $host_string = "an innocuous string";

        # Clone the host life form to form a suitable environment
        # for oursynthetic strings.
        my $sv = unpack 'P12', pack 'L', 0 + \ $host_string;
        my $xpv = unpack 'P12', substr $sv, 0, 4;

        # Now implant our alien spawn into the host string
        substr $sv, 0, 4, pack 'P', $xpv;
        substr $xpv, 0, 4, pack 'L', 0 + $address;
        substr $xpv, 4, 4, pack 'L', 0 + $length;
        substr $xpv, 8, 4, pack 'L', 1 + $length;

        my $new_sv = \ unpack 'L', pack 'P', $sv;

        # Be sure to keep references to $sv and $xpv around so the bac
+king SV
        # isn't deallocated.
        # Let the alien out into the general population.
        +{ text => bless( $new_sv, 'B::PV')->object_2svref,
            _backend => [ \ $sv, \ $xpv ] };
    };
}
Comment on Synthetic strings dereference arbitrary pointers
Download Code
Re: Synthetic strings dereference arbitrary pointers
by ysth (Canon) on Aug 02, 2004 at 21:31 UTC
    object_2svref is 5.8.1, not 5.8.0; and for maximum helpfulness, you should say use 5.008001, not use 5.8.1
Re: Synthetic strings dereference arbitrary pointers
by zentara (Archbishop) on Aug 03, 2004 at 13:46 UTC
    I'm sitting here wondering "what mischief can this be used for?" Would you mind elaborating?

    I'm not really a human, but I play one on earth. flash japh
      This allows you to read and edit any part of your current process including compiled C code, loaded libraries, the compiled perl optree, every variable (allocated or dead), etc. Think of the pointer fiddling possibilities. Or editing the text of error messages. Or um... whatever. Essentially it brings to perl what C has had with plain pointers.
        WOW! :-)

        I'm not really a human, but I play one on earth. flash japh
        I'd love to see a real life example of this

        Sweetblood

•Re: Synthetic strings dereference arbitrary pointers
by merlyn (Sage) on Aug 03, 2004 at 15:16 UTC
      Yeah, plain peek is easy. Its the poking that I thought normally required either XS or that perl 4 typeglob bug. Now I just get "Can't coerce GLOB to string in substr." How would you update Larry's poke for perl 5?
Re: Synthetic strings dereference arbitrary pointers
by ribasushi (Monk) on Aug 31, 2011 at 11:56 UTC
      How does it compare to PeekPoke (since 24 Dec 2001)
        No XS, hence no compiler required, hence FatPack-able. Also much nicer API (you do not need to determine the ptrsizes etc of the system you are using).

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://379428]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-07-12 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls