Class: Crabstone::Disassembler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arch, mode) ⇒ Disassembler

Returns a new instance of Disassembler.



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/crabstone.rb', line 433

def initialize arch, mode

  maj, min = version
  if maj != BINDING_MAJ || min != BINDING_MIN
    raise "FATAL: Binding for #{BINDING_MAJ}.#{BINDING_MIN}, found #{maj}.#{min}"
  end

  @arch    = arch
  @mode    = mode
  p_size_t = FFI::MemoryPointer.new :ulong_long
  @p_csh    = FFI::MemoryPointer.new p_size_t
  if ( res = Binding.cs_open( arch, mode, @p_csh )).nonzero?
    Crabstone.raise_errno res
  end

  @csh = @p_csh.read_ulong_long

end

Instance Attribute Details

#archObject (readonly)

Returns the value of attribute arch.



431
432
433
# File 'lib/crabstone.rb', line 431

def arch
  @arch
end

#cshObject (readonly)

Returns the value of attribute csh.



431
432
433
# File 'lib/crabstone.rb', line 431

def csh
  @csh
end

#decomposerObject

Returns the value of attribute decomposer.



431
432
433
# File 'lib/crabstone.rb', line 431

def decomposer
  @decomposer
end

#modeObject (readonly)

Returns the value of attribute mode.



431
432
433
# File 'lib/crabstone.rb', line 431

def mode
  @mode
end

#syntaxObject

Returns the value of attribute syntax.



431
432
433
# File 'lib/crabstone.rb', line 431

def syntax
  @syntax
end

Instance Method Details

#closeObject

After you close the engine, don’t use it anymore. Can’t believe I even have to write this.



454
455
456
457
458
# File 'lib/crabstone.rb', line 454

def close
  if ( res = Binding.cs_close(@p_csh) ).nonzero?
    Crabstone.raise_errno res
  end
end

#diet?Boolean

Returns:

  • (Boolean)


480
481
482
# File 'lib/crabstone.rb', line 480

def diet?
  DIET_MODE
end

#disasm(code, offset, count = 0) ⇒ Object



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/crabstone.rb', line 532

def disasm code, offset, count = 0
  return [] if code.empty?

  insn_ptr   = FFI::MemoryPointer.new :pointer
  insn_count = Binding.cs_disasm(
    @csh,
    code,
    code.bytesize,
    offset,
    count,
    insn_ptr
  )
  Crabstone.raise_errno(errno) if insn_count.zero?

  insns = (0...insn_count * Binding::Instruction.size).step(Binding::Instruction.size).map do |off|
    cs_insn_ptr = Binding.malloc Binding::Instruction.size
    cs_insn = Binding::Instruction.new cs_insn_ptr
    Binding.memcpy(cs_insn_ptr, insn_ptr.read_pointer + off, Binding::Instruction.size)
    Instruction.new @csh, cs_insn, @arch
  end

  Binding.free(insn_ptr.read_pointer)

  insns
end

#errnoObject



484
485
486
# File 'lib/crabstone.rb', line 484

def errno
  Binding.cs_errno(csh)
end

#reg_name(regid) ⇒ Object



525
526
527
528
529
530
# File 'lib/crabstone.rb', line 525

def reg_name regid
  Crabstone.raise_errno( Crabstone::ERRNO_KLASS[ErrDiet] ) if DIET_MODE
  name = Binding.cs_reg_name(csh, regid)
  Crabstone.raise_errno( Crabstone::ERRNO_KLASS[ErrCsh] ) unless name
  name
end

#set_raw_option(opt, val) ⇒ Object



558
559
560
561
# File 'lib/crabstone.rb', line 558

def set_raw_option opt, val
  res = Binding.cs_option csh, opt, val
  Crabstone.raise_errno res if res.nonzero?
end

#skipdata(mnemonic = '.byte') ⇒ Object



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/crabstone.rb', line 488

def skipdata mnemonic='.byte'

  cfg = Binding::SkipdataConfig.new
  cfg[:mnemonic] = FFI::MemoryPointer.from_string String(mnemonic)

  if block_given?

    real_cb = FFI::Function.new(
      :size_t,
      [:pointer, :size_t, :size_t, :pointer]
    ) {|code, sz, offset, _|

      code = code.read_array_of_uchar(sz).pack('c*')
      begin
        res = yield code, offset
        Integer(res)
      rescue
        warn "Error in skipdata callback: #{$!}"
        # It will go on to crash, but now at least there's more info :)
      end
    }

    cfg[:callback] = real_cb

  end

  res = Binding.cs_option(csh, OPT_SKIPDATA_SETUP, cfg.pointer.address)
  Crabstone.raise_errno res if res.nonzero?
  res = Binding.cs_option(csh, OPT_SKIPDATA, SKIPDATA[true])
  Crabstone.raise_errno res if res.nonzero?
end

#skipdata_offObject



520
521
522
523
# File 'lib/crabstone.rb', line 520

def skipdata_off
  res = Binding.cs_option(csh, OPT_SKIPDATA, SKIPDATA[false])
  Crabstone.raise_errno res if res.nonzero?
end

#versionObject



473
474
475
476
477
478
# File 'lib/crabstone.rb', line 473

def version
  maj = FFI::MemoryPointer.new(:int)
  min = FFI::MemoryPointer.new(:int)
  Binding.cs_version maj, min
  [ maj.read_int, min.read_int ]
end