[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Is it possible to extract data out of memory using Ruby?

Erik Terpstra

8/14/2008 7:31:00 AM

Is it possible to extract data out of memory using Ruby?

Something like this: Memory.get_byte(pos)

Or something like this Perl module:

http://search.cpan.org/~bobmath/Disassemble-X86-0.13/X86/Me...

This Perl module is *not* an extension in C, I am not sure how the
author pulled that off. I looked at the source code, but I am not very
good at Perl.

- Erik
--
Posted via http://www.ruby-....

3 Answers

Pit Capitain

8/14/2008 7:42:00 AM

0

2008/8/14 Erik Terpstra <erik@ruby-lang.nl>:
> Is it possible to extract data out of memory using Ruby?

Erik, to read and write longs (4 bytes) I use the DL standard library
with the following code:

require "dl"

ptr = DL::PtrData.new 0
ptr.struct! "L", "val"
size = ptr.size

# read
ptr[ address, size ].unpack( "L" )[ 0 ]

# write
ptr[ address, size ] = [ val ].pack( "L" )

For reading and writing bytes you have to change the "L" to something
else. See the docs for the DL standard library.

Regards,
Pit

Erik Terpstra

8/14/2008 3:16:00 PM

0

So if you would like to look at the 'in memory' representation of a Ruby
String how would that work?

require "dl"

ptr = DL::PtrData.new 0
ptr.struct! "L", "val"
size = ptr.size

str = 'Hello world!'
address = str.object_id * 2
value = ptr[ address, size ]

puts [address, value].inspect

TIA,

Erik.

Pit Capitain wrote:
> 2008/8/14 Erik Terpstra <erik@ruby-lang.nl>:
>> Is it possible to extract data out of memory using Ruby?
>
> Erik, to read and write longs (4 bytes) I use the DL standard library
> with the following code:
>
> require "dl"
>
> ptr = DL::PtrData.new 0
> ptr.struct! "L", "val"
> size = ptr.size
>
> # read
> ptr[ address, size ].unpack( "L" )[ 0 ]
>
> # write
> ptr[ address, size ] = [ val ].pack( "L" )
>
> For reading and writing bytes you have to change the "L" to something
> else. See the docs for the DL standard library.
>
> Regards,
> Pit

--
Posted via http://www.ruby-....

Pit Capitain

8/15/2008 7:07:00 AM

0

2008/8/14 Erik Terpstra <erik@ruby-lang.nl>:
> So if you would like to look at the 'in memory' representation of a Ruby
> String how would that work?

Erik, you have to look at the C source code of Ruby to find the
structure of String instances. In MRI 1.8, you can get at the address
of a Ruby object via

object.object_id * 2

Regards,
Pit