Class: MemoryIO::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/memory_io/context.rb

Overview

Describes how the memory being accessed lays out its data.

The context belongs to the memory, not to the machine running this library. They only coincide when the memory belongs to a process on the same host.

Constant Summary collapse

ENDIANS =

Byte orders that can be asked for. :native resolves to the byte order of the host, which is the right answer whenever the memory belongs to a process running on it.

%i[little big native].freeze
NATIVE_ENDIAN =

The byte order of the host.

[1].pack('S') == "\x01\x00".b ? :little : :big
DEFAULT_POINTER_SIZE =

Assumed when nothing more specific is known.

8

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endian: :native, pointer_size: DEFAULT_POINTER_SIZE) ⇒ Context

Returns a new instance of Context.

Examples:

Context.new(endian: :big).endian
#=> :big

Parameters:

  • endian (:little, :big, :native) (defaults to: :native)

    Byte order of the memory.

  • pointer_size (Integer) (defaults to: DEFAULT_POINTER_SIZE)

    Size of a pointer, in bytes.

Raises:

  • (ArgumentError)

    endian is not one of ENDIANS.



49
50
51
52
53
54
55
# File 'lib/memory_io/context.rb', line 49

def initialize(endian: :native, pointer_size: DEFAULT_POINTER_SIZE)
  raise ArgumentError, "endian must be one of #{ENDIANS.inspect}, got #{endian.inspect}" \
    unless ENDIANS.include?(endian)

  @endian = endian == :native ? NATIVE_ENDIAN : endian
  @pointer_size = pointer_size
end

Instance Attribute Details

#endian:little, :big (readonly)

Returns Byte order of the memory. :native has already been resolved.

Returns:

  • (:little, :big)

    Byte order of the memory. :native has already been resolved.



26
27
28
# File 'lib/memory_io/context.rb', line 26

def endian
  @endian
end

#pointer_sizeInteger (readonly)

Returns Size of a pointer, in bytes.

Returns:

  • (Integer)

    Size of a pointer, in bytes.



30
31
32
# File 'lib/memory_io/context.rb', line 30

def pointer_size
  @pointer_size
end

Class Method Details

.defaultMemoryIO::Context

Returns Used when a stream carries no context of its own.

Returns:



60
61
62
# File 'lib/memory_io/context.rb', line 60

def default
  @default ||= new
end

.from_elf(path) ⇒ MemoryIO::Context?

Derive a context from an ELF file, which describes the memory it is loaded into.

Examples:

Context.from_elf('/proc/self/exe')
#=> #<MemoryIO::Context @endian=:little, @pointer_size=8>

Parameters:

  • path (String)

    Path of the ELF file.

Returns:



85
86
87
88
89
90
91
92
# File 'lib/memory_io/context.rb', line 85

def from_elf(path)
  ::File.open(path, 'rb') do |file|
    elf = ELFTools::ELFFile.new(file)
    new(endian: elf.endian, pointer_size: elf.elf_class / 8)
  end
rescue SystemCallError, ELFTools::ELFError
  nil
end

.of(stream) ⇒ MemoryIO::Context

Returns The context stream was tagged with, or default when it carries none.

Parameters:

  • stream (Object)

    The stream a type is reading from.

Returns:



69
70
71
# File 'lib/memory_io/context.rb', line 69

def of(stream)
  stream.is_a?(MemoryIO::Stream) ? stream.context : default
end

Instance Method Details

#to_hHash

Returns The attributes, in the form #initialize accepts.

Returns:

  • (Hash)

    The attributes, in the form #initialize accepts.



34
35
36
# File 'lib/memory_io/context.rb', line 34

def to_h
  { endian: endian, pointer_size: pointer_size }
end