Class: OneGadget::Emulators::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/one_gadget/emulators/processor.rb

Overview

Base class of a processor.

Direct Known Subclasses

AArch64, X86

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registers, sp) ⇒ Processor

Instantiate a OneGadget::Emulators::Processor object.

Parameters:

  • registers (Array<String>)

    Registers that supported in the architecture.

  • sp (String)

    The stack register.


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/one_gadget/emulators/processor.rb', line 19

def initialize(registers, sp)
  @registers = registers.map { |reg| [reg, to_lambda(reg)] }.to_h
  @sp = sp
  @constraints = []
  @stack = Hash.new do |h, k|
    h[k] = OneGadget::Emulators::Lambda.new(sp).tap do |lmda|
      lmda.immi = k
      lmda.deref!
    end
  end
end

Instance Attribute Details

#pcString (readonly)

Returns Program counter.

Returns:

  • (String)

    Program counter.


12
13
14
# File 'lib/one_gadget/emulators/processor.rb', line 12

def pc
  @pc
end

#registersHash{String => OneGadget::Emulators::Lambda} (readonly)

Returns The current registers' state.

Returns:


9
10
11
# File 'lib/one_gadget/emulators/processor.rb', line 9

def registers
  @registers
end

#spString (readonly)

Returns Stack pointer.

Returns:

  • (String)

    Stack pointer.


11
12
13
# File 'lib/one_gadget/emulators/processor.rb', line 11

def sp
  @sp
end

#stackHash{Integer => OneGadget::Emulators::Lambda} (readonly)

Returns The content on stack.

Returns:


10
11
12
# File 'lib/one_gadget/emulators/processor.rb', line 10

def stack
  @stack
end

Class Method Details

.bitsInteger

32 or 64.

Returns:

  • (Integer)

    32 or 64.

Raises:

  • (NotImplementedError)

129
130
# File 'lib/one_gadget/emulators/processor.rb', line 129

def bits; raise NotImplementedError
end

Instance Method Details

#argument(_idx) ⇒ Lambda, Integer

To be inherited.

Parameters:

  • _idx (Integer)

    The idx-th argument.

Returns:

  • (Lambda, Integer)

    Return value can be a Lambda or an Integer.

Raises:

  • (NotImplementedError)

76
77
# File 'lib/one_gadget/emulators/processor.rb', line 76

def argument(_idx); raise NotImplementedError
end

#constraintsArray<String>

Returns Extra constraints found during execution.

Returns:

  • (Array<String>)

    Extra constraints found during execution.


81
82
83
84
85
86
87
# File 'lib/one_gadget/emulators/processor.rb', line 81

def constraints
  return [] if @constraints.empty?

  # currently only ':writable' type
  cons = @constraints.uniq { |_type, obj| obj.deref_count.zero? ? obj.obj.to_s : obj.to_s }
  cons.map { |_type, obj| "writable: #{obj}" }.sort
end

#instructionsArray<Instruction>

Method need to be implemented in inheritors.

Returns:

Raises:

  • (NotImplementedError)

66
67
# File 'lib/one_gadget/emulators/processor.rb', line 66

def instructions; raise NotImplementedError
end

#parse(cmd) ⇒ (Instruction, Array<String>)

Parse one command into instruction and arguments.

Parameters:

  • cmd (String)

    One line of result of objdump.

Returns:

  • ((Instruction, Array<String>))

    The parsing result.

Raises:


35
36
37
38
39
40
# File 'lib/one_gadget/emulators/processor.rb', line 35

def parse(cmd)
  inst = instructions.find { |i| i.match?(cmd) }
  raise Error::UnsupportedInstructionError, "Not implemented instruction in #{cmd}" if inst.nil?

  [inst, inst.fetch_args(cmd)]
end

#process(cmd) ⇒ Boolean

Process one command, without raising any exceptions.

Parameters:

  • cmd (String)

    See #process! for more information.

Returns:

  • (Boolean)

46
47
48
49
50
51
# File 'lib/one_gadget/emulators/processor.rb', line 46

def process(cmd)
  process!(cmd)# rescue OneGadget::Error::UnsupportedError # for debugging

rescue OneGadget::Error::Error
  false
end

#process!(_cmd) ⇒ Boolean

Method need to be implemented in inheritors.

Process one command. Will raise exceptions when encounter unhandled instruction.

Parameters:

  • _cmd (String)

    One line from result of objdump.

Returns:

  • (Boolean)

    If successfully processed.

Raises:

  • (NotImplementedError)

61
62
# File 'lib/one_gadget/emulators/processor.rb', line 61

def process!(_cmd); raise NotImplementedError
end