Class: Evoasm::X64::CPUState

Inherits:
FFI::AutoPointer
  • Object
show all
Defined in:
lib/evoasm/x64/cpu_state.rb

Overview

Represents the CPU state (i.e. a snapshot of all registers) at a specific moment in time.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags = [:rflags]) ⇒ CPUState

Returns a new instance of CPUState

Parameters:

  • flags (Array<Symbol>) (defaults to: [:rflags])


16
17
18
19
20
# File 'lib/evoasm/x64/cpu_state.rb', line 16

def initialize(flags = [:rflags])
  ptr = Libevoasm.x64_cpu_state_alloc
  Libevoasm.x64_cpu_state_init ptr, Libevoasm.enum_type(:x64_cpu_state_flags).flags(flags, shift: false)
  super(ptr)
end

Class Method Details

.randomObject



22
23
24
25
26
27
# File 'lib/evoasm/x64/cpu_state.rb', line 22

def self.random
  cpu_state = new
  Libevoasm.x64_cpu_state_rand cpu_state, PRNG.default

  cpu_state
end

Instance Method Details

#[](register, word = :none) ⇒ Array<Integer>

Obtain the value of a register

Parameters:

  • register (Symbol)

    the register

  • word (Symbol) (defaults to: :none)

    an optional word to mask the value (e.g. to obtain a subregister value)

Returns:

  • (Array<Integer>)

    the register's value as an array of 64-bit integers



45
46
47
48
49
# File 'lib/evoasm/x64/cpu_state.rb', line 45

def [](register, word = :none)
  data_ptr = FFI::MemoryPointer.new :uint64, 16
  data_len = Libevoasm.x64_cpu_state_get self, register, word, data_ptr, 16
  data_ptr.read_array_of_uint64 data_len
end

#[]=(register, data) ⇒ void

This method returns an undefined value.

Sets the value of a register

Parameters:

  • register (Symbol)

    register to set

  • data (Array<Integer>, Integer)

    value as a single 64-bit integer or an array of multiple 64-bit integers (e.g. for vector registers)



34
35
36
37
38
39
# File 'lib/evoasm/x64/cpu_state.rb', line 34

def []=(register, data)
  data = Array(data)
  ptr = FFI::MemoryPointer.new :uint64, data.size
  ptr.write_array_of_uint64 data
  Libevoasm.x64_cpu_state_set self, register, ptr, data.size
end

#cloneCPUState

Clone this CPU state object

Returns:



53
54
55
56
57
58
# File 'lib/evoasm/x64/cpu_state.rb', line 53

def clone
  cloned_cpu_state = self.class.new
  Libevoasm.x64_cpu_state_clone self, cloned_cpu_state

  cloned_cpu_state
end

#emit_load(buffer) ⇒ void

This method returns an undefined value.

Emits machine code to load (set) the current CPU state to the state of this object

Parameters:

  • buffer (Buffer)

    the buffer to emit to

Raises:

  • (Error)

    if an error occurres



100
101
102
103
104
# File 'lib/evoasm/x64/cpu_state.rb', line 100

def emit_load(buffer)
  unless Libevoasm.x64_cpu_state_emit_load self, buffer
    raise Error.last
  end
end

#emit_store(buffer) ⇒ void

This method returns an undefined value.

Emits machine code to store (save) the current CPU state into this object

Parameters:

  • buffer (Buffer)

    the buffer to emit to

Raises:

  • (Error)

    if an error occurres



89
90
91
92
93
# File 'lib/evoasm/x64/cpu_state.rb', line 89

def emit_store(buffer)
  unless Libevoasm.x64_cpu_state_emit_store self, buffer
    raise Error.last
  end
end

#rflags_flag(flag) ⇒ Object



80
81
82
# File 'lib/evoasm/x64/cpu_state.rb', line 80

def rflags_flag(flag)
  Libevoasm.x64_cpu_state_get_rflags_flag self, flag
end

#to_hHash

Converts this object into a hash

Returns:

  • (Hash)

    the hash



74
75
76
77
78
# File 'lib/evoasm/x64/cpu_state.rb', line 74

def to_h
  X64.registers.each_with_object({}) do |register, hash|
    hash[register] = self[register]
  end
end