Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by moritz (Cardinal)
on Jul 29, 2012 at 12:15 UTC ( #984281=note: print w/ replies, xml ) Need Help??


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

Perl has references itself (see perlreftut, perlref), but it does not allow you access to arbitrary memory addresses. That's a feature, because it means you can't get segmentation fauls or bus errors if you stick to pure Perl code (no C code involved).

What do you need C-Pointer dereferencing for? Maybe there's a better way to achieve your underlying goal.


Comment on Re: How to access the contents of a specific memory address?
Re^2: How to access the contents of a specific memory address?
by bulk88 (Priest) on Jul 29, 2012 at 17:45 UTC
    That's a feature, because it means you can't get segmentation fauls or bus errors if you stick to pure Perl code (no C code involved).

    I disagree.
    perl -e "print unpack('p', pack('J', 1))"
      Strange: When I try this, I get a segmentation fault. Am I overlooking some important detail?
      $ perl -e 'print unpack("p", pack("J", 1))' Segmentation fault
        Am I overlooking some important detail?

        Simple logic. :P

        Proposition: You can't get segfaults with pure Perl. Response: I disagree: code sample
Re^2: How to access the contents of a specific memory address?
by pat_mc (Pilgrim) on Jul 29, 2012 at 19:37 UTC
    Thanks for this response! It pretty much is a reflection of my understanding of Perl's memory addressing capabilities so far.
    As for my underlying objective, I am trying to understand the concept of buffer overflow and have been experimenting around with it a little. In particular, I have been unable to create a simple buffer overflow (in analogy to how this would be achieved in a simple C programme) and am therefore trying to gain deeper insight into how Perl manages memory allocation. It's exactly at this point that I would like to see what values certain memory addresses have been allocated.
      What do you want know on perl's memory allocation?

      If you want a 1 second glimpse, run
      use Devel::Peek; $num = 5; Dump($num); print $num."\n"; Dump($num);
      Output
      SV = IV(0xbf4958) at 0xbf495c REFCNT = 1 FLAGS = (IOK,pIOK) IV = 5 5 SV = PVIV(0x39df74) at 0xbf495c REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) IV = 5 PV = 0x84cb0c "5"\0 CUR = 1 LEN = 4
      If you want to know more, read illguts. Someone on perlmonks is bound to know whatever you ask about Perl's C side.
        Yes, this is taking me half the way I wanted to go. I can use Devel::Peek to inspect variables and their location in memory. What I really wanted to do, however, was to go the other way: to find out which value is currently stored in a specific memory location.

        Is there a way Devel::Peek will provide this information, too?

      I assume that you are using a perl array as a "buffer". In perl you do not have to predefine the length of an array. The array expands as you push additional elements into it. Use the length of the array to detect your maximum buffer length. Use splice to reset the length.

        Actually, no, I wasn't. I was using this code to look into where Perl stores the contents of the variables:
        my $a = "a"; my $b = "b"; my $offset = \$b - \$a; printf "a: %s\nAddress: %p\n", $a, $a; printf "b: %s\nAddress: %p\n", $b, $b; printf "Buffer Offset: %d\n", $offset;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2014-12-18 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (51 votes), past polls