Class: Evoasm::X64::Parameters

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

Overview

Represents x86-64 instruction parameters.

Direct Known Subclasses

BasicParameters

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Parameters

Returns a new instance of Parameters

Parameters:

  • hash (Hash) (defaults to: {})

    an optional hash containing parameters



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/evoasm/x64/parameters.rb', line 44

def initialize(hash = {})
  if basic?
    ptr = Libevoasm.x64_basic_params_alloc
    Libevoasm.x64_basic_params_init ptr
  else
    ptr = Libevoasm.x64_params_alloc
    Libevoasm.x64_params_init ptr
  end

  @param_id_enum_type =
    if basic?
      Libevoasm.enum_type(:x64_basic_param_id)
    else
      Libevoasm.enum_type(:x64_param_id)
    end

  @disp_size_enum_type = Libevoasm.enum_type :x64_disp_size
  @addr_size_enum_type = Libevoasm.enum_type :x64_addr_size
  @scale_enum_type = Libevoasm.enum_type :x64_scale
  @reg_id_enum_type = Libevoasm.enum_type :x64_reg_id

  @type_map_enum_types = {
    scale: @scale_enum_type,
    addr_size: @addr_size_enum_type,
    reg: @reg_id_enum_type
  }

  @type_map = {
    scale: {
      1 => :scale1,
      2 => :scale2,
      4 => :scale4,
      8 => :scale8
    },

    addr_size: {
      32 => :addr_size32,
      64 => :addr_size64,
    },

    bool: {
      true => 1,
      false => 0
    },

    uint1: proc {|v| check_uint_range v, 1},
    int3: proc {|v| check_int_range v, 3},
    int4: proc {|v| check_int_range v, 4},
    int8: proc {|v| check_int_range v, 8},
    int32: proc {|v| check_int_range v, 32},
    int64: proc {|v| check_int_range v, 64},
    reg: proc {|v| @reg_id_enum_type[v]}
  }

  @inv_type_map = @type_map.map do |k, v|
    if v.is_a? Hash
      [k, v.invert]
    else
      [k, v]
    end
  end.to_h

  @inv_type_map[:reg] = proc {|v| v}

  super(ptr)

  hash.each do |k, v|
    self[k] = v
  end
end

Class Method Details

.random(instruction, other_instruction = nil) ⇒ X64::Parameters

Creates a random set of parameters for the given instruction and parameters

Parameters:

Returns:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/evoasm/x64/parameters.rb', line 125

def self.random(instruction, other_instruction = nil)
  parameters = Evoasm::X64::Parameters.new
  success =
    if other_instruction
      Libevoasm.x64_params_rand2 parameters, instruction, other_instruction, Evoasm::PRNG.default
    else
      Libevoasm.x64_params_rand parameters, instruction, Evoasm::PRNG.default
    end

  if success
    parameters
  else
    nil
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



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

def ==(other)
  return true if other.equal?(self)
  return false unless other.instance_of?(self.class)
  @param_id_enum_type.symbols[0..-2].all? do |s|
    self[s] == other[s]
  end
end

#[](parameter_name) ⇒ Symbol, Integer

Returns the parameter value

Parameters:

  • parameter_name (Symbol)

    the parameter's name

Returns:

  • (Symbol, Integer)

    the parameter value



143
144
145
146
147
148
149
150
151
152
# File 'lib/evoasm/x64/parameters.rb', line 143

def [](parameter_name)
  ffi_value =
    if basic?
      Libevoasm.x64_basic_params_get self, parameter_name_to_id(parameter_name)
    else
      Libevoasm.x64_params_get self, parameter_name_to_id(parameter_name)
    end

  ffi_value_to_value parameter_name, ffi_value
end

#[]=(parameter_name, value) ⇒ void

This method returns an undefined value.

Set a parameter

Parameters:

  • parameter_name (Symbol)

    the parameter's name

  • value (Symbol, Integer)

    the parameter's value



165
166
167
168
169
170
171
172
173
174
# File 'lib/evoasm/x64/parameters.rb', line 165

def []=(parameter_name, value)
  ffi_value = value_to_ffi_value parameter_name, value
  parameter_id = parameter_name_to_id(parameter_name)

  if basic?
    Libevoasm.x64_basic_params_set self, parameter_id, ffi_value
  else
    Libevoasm.x64_params_set self, parameter_id, ffi_value
  end
end

#basic?Bool

Returns whether this parameters are for basic encoding

Returns:

  • (Bool)


117
118
119
# File 'lib/evoasm/x64/parameters.rb', line 117

def basic?
  false
end

#inspectObject



28
29
30
31
# File 'lib/evoasm/x64/parameters.rb', line 28

def inspect
  fields = @param_id_enum_type.symbols[0..-2].map {|s| "#{s}:#{self[s]}"}.join(' ')
  "#<#{self.class.inspect} #{fields}>"
end

#parameter?(parameter_name) ⇒ Bool

Checks the existence of a parameter

Parameters:

  • parameter_name (Symbol)

    the parameter name

Returns:

  • (Bool)

    whether the parameter exists or nor



157
158
159
# File 'lib/evoasm/x64/parameters.rb', line 157

def parameter?(parameter_name)
  !@param_id_enum_type[parameter_name].nil?
end