Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: How to access the contents of a specific memory address?

by bulk88 (Priest)
on Jul 29, 2012 at 15:46 UTC ( [id://984289]=note: print w/replies, xml ) Need Help??


in reply to How to access the contents of a specific memory address?

my $astring = "some string"; my $svptr = (\$astring)+0; #works on 32 bit ONLY my $packesvptr = pack('L', $svptr); my $svbinary = unpack('P[L4]', $packesvptr); my $packedPV = substr($svbinary, 4*3, 4); print "\"".unpack('p', $packedPV)."\"";#null terminated, use 'P[12345] +' for binary data print "\n\nExample 2\n\n"; $packedPV = pack('p', $astring ); print "\"".unpack('p', $packedPV)."\"";
This is pretty much all of Perl's Perl Language pointer API. You can get a pointer to a scalar string (not a pointer to a scalar integers!!!, egh, actually you can, but if you need to do it, you know how to do it) and you can get a SV *and you can read an arbitrary memory location into a scalar string, null terminated or fixed length. pack wants pointers in packed binary "\x01\x02\x03\x04" format, not english/readable/perl scalar integers "1234567" format. Only major limitation is Perl language can't copy scalar strings to arbitrary memory address (but see this hack Pure Perl module(246 lines, Linux/Win32) that calls external libraries - no XS file., but its not pure Perl language (Dynaloader Module), so that doesn't meet the standard of pure Perl language, also there is syscall, I dont use unix, so I dont know if there is a way to call a C memcpy with syscall). My code is 32 bit only. Changes would have to be made for it to work on 64 bits.

update: after reading Re^4: How to access the contents of a specific memory address?, I'll add this

pointer dereferencing with basic Perl commands (rather than any specially designed modules)


You mentioned with the basic Perl language. malloc and memcpy and free are all available somewhere someway on CPAN as XS modules, and they will work correctly without any crashes except for cases where they will crash in C and in Perl together, always (access vio, corrupting malloc headers, etc). Some monks here question why on earth you need pointers in Perl? They are correct, you can't save a pointer to a disk, it will never be valid in another process. You can't send a pointer through IPC to another process, its not valid in the other process. You can't send a pointer through the network to another PC, it is not valid on that other PC. No perl basic language commands take integer pointers (except for syscall maybe). To use your integer pointers you would need a FFI XS module, or a XS module that wants SVIVs with pointers in them as parameters (a possible design choice for a XS library to keep the XS code as small as possible, and put the parameter conversion logic into Perl, even though its quite slower then) for passing to a C function.

Replies are listed 'Best First'.
Re^2: How to access the contents of a specific memory address?
by pat_mc (Pilgrim) on Jul 29, 2012 at 19:48 UTC
    Thanks for this post!

    I had actually been experimenting with pack() and unpack() before as well but somehow did not get them to work the way I wanted it, probably because of the incorrect input address format. I'll try to understand your sample code and may get back with further questions in case I cannot fathom all depths of it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 18:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found