Class: Fisk::Helpers::JITBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/fisk/helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(memory, size) ⇒ JITBuffer

Returns a new instance of JITBuffer.



54
55
56
57
58
# File 'lib/fisk/helpers.rb', line 54

def initialize memory, size
  @memory = memory
  @pos = 0
  @size = size
end

Instance Attribute Details

#memoryObject (readonly)

Returns the value of attribute memory.



52
53
54
# File 'lib/fisk/helpers.rb', line 52

def memory
  @memory
end

#posObject (readonly)

Returns the value of attribute pos.



52
53
54
# File 'lib/fisk/helpers.rb', line 52

def pos
  @pos
end

Instance Method Details

#addressObject



102
103
104
# File 'lib/fisk/helpers.rb', line 102

def address
  memory.to_i + pos
end

#patch_jump(at:, to:, type: :jmp) ⇒ Object

Write a jump instruction at location at that jumps to the location specified by to. type specifies the type of jump. This method maintains the current position of the cursor inside the memory chunk



81
82
83
84
85
# File 'lib/fisk/helpers.rb', line 81

def patch_jump at:, to:, type: :jmp
  pos = self.pos
  write_jump(to: to, at: at, type: type)
  seek pos, IO::SEEK_SET
end

#putc(byte) ⇒ Object



60
61
62
63
64
# File 'lib/fisk/helpers.rb', line 60

def putc byte
  raise "Buffer full! #{pos} - #{@size}" if pos >= @size
  @memory[@pos] = byte
  @pos += 1
end

#seek(pos, whence = IO::SEEK_SET) ⇒ Object

Raises:

  • (NotImplementedError)


66
67
68
69
70
71
72
# File 'lib/fisk/helpers.rb', line 66

def seek pos, whence = IO::SEEK_SET
  raise NotImplementedError if whence != IO::SEEK_SET
  raise if pos >= @size

  @pos = pos
  self
end

#to_function(params, ret) ⇒ Object



74
75
76
# File 'lib/fisk/helpers.rb', line 74

def to_function params, ret
  Fiddle::Function.new memory.to_i, params, ret
end

#write_jump(to:, at: self.pos, type: :jmp) ⇒ Object

Write a jump instruction at location at that jumps to the location specified by to. type specifies the type of jump. Returns the position in the buffer where the jump instruction was written.

This method does not maintain the current position of the cursor



92
93
94
95
96
97
98
99
100
# File 'lib/fisk/helpers.rb', line 92

def write_jump to:, at: self.pos, type: :jmp
  rel_jump = 0xCAFE
  2.times do
    seek at, IO::SEEK_SET
    Fisk.new { |__| __.public_send(type, __.rel32(rel_jump)) }.write_to(self)
    rel_jump = to - address
  end
  self.pos
end