Class: HeapInfo::Arena

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

Overview

Records status of an arena, including bin(s) and top chunk.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, size_t, dumper) ⇒ Arena

Instantiate a HeapInfo::Arena object.

Parameters:

  • base (Integer)

    Base address of arena.

  • size_t (Integer)

    Either 8 or 4

  • dumper (Proc)

    For dump more data


23
24
25
26
27
28
# File 'lib/heapinfo/arena.rb', line 23

def initialize(base, size_t, dumper)
  @base = base
  @size_t = size_t
  @dumper = dumper
  reload!
end

Instance Attribute Details

#fastbinArray<HeapInfo::Fastbin> (readonly)

Returns Fastbins in an array.

Returns:


5
6
7
# File 'lib/heapinfo/arena.rb', line 5

def fastbin
  @fastbin
end

#last_remainderHeapInfo::Chunk (readonly)

Returns Current last remainder.

Returns:


9
10
11
# File 'lib/heapinfo/arena.rb', line 9

def last_remainder
  @last_remainder
end

#smallbinArray<HeapInfo::Smallbin> (readonly)

Returns Smallbins in an array.

Returns:


13
14
15
# File 'lib/heapinfo/arena.rb', line 13

def smallbin
  @smallbin
end

#system_memInteger (readonly)

Returns The system_mem in arena.

Returns:

  • (Integer)

    The system_mem in arena.


15
16
17
# File 'lib/heapinfo/arena.rb', line 15

def system_mem
  @system_mem
end

#top_chunkHeapInfo::Chunk (readonly)

Returns Current top chunk.

Returns:


7
8
9
# File 'lib/heapinfo/arena.rb', line 7

def top_chunk
  @top_chunk
end

#unsorted_binArray<HeapInfo::UnsortedBin> (readonly)

Returns The unsorted bin (array size will always be one).

Returns:


11
12
13
# File 'lib/heapinfo/arena.rb', line 11

def unsorted_bin
  @unsorted_bin
end

Instance Method Details

#layouts(*args) ⇒ String

Pretty dump of bins layouts.

Examples:

puts h.libc.main_arena.layouts(:fast, :unsorted, :small)
puts h.libc.main_arena.layouts(:all)

Parameters:

  • args (Symbol)

    Bin type(s) you want to see.

Returns:

  • (String)

    Bin layouts that wrapper with color codes.


65
66
67
68
69
70
71
72
73
# File 'lib/heapinfo/arena.rb', line 65

def layouts(*args)
  args.concat(%i[fast unsort small large]) if args.map(&:to_s).include?('all')
  args = args.map(&:to_s).join('|')
  res = ''
  res += fastbin.map(&:inspect).join if args.include?('fast')
  res += unsorted_bin.inspect if args.include?('unsort')
  res += smallbin.map(&:inspect).join if args.include?('small')
  res
end

#reload!HeapInfo::Arena

Refresh all attributes. Retrive data using @dumper, load bins, top chunk etc.

Returns:


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/heapinfo/arena.rb', line 33

def reload!
  top_ptr_offset = @base + 8 + size_t * 10
  top_ptr = Helper.unpack(size_t, @dumper.call(top_ptr_offset, size_t))
  @fastbin = []
  return self if top_ptr.zero? # arena not init yet
  @top_chunk = Chunk.new size_t, top_ptr, @dumper
  @last_remainder = Chunk.new size_t, top_ptr_offset + 8, @dumper
  # this offset diff after 2.23
  @system_mem = Array.new(2) do |off|
    Helper.unpack(size_t, @dumper.call(top_ptr_offset + 258 * size_t + 16 + off * size_t, size_t))
  end.find { |val| val >= 0x21000 && (val & 0xfff).zero? }
  @fastbin = Array.new(7) do |idx|
    f = Fastbin.new(size_t, @base + 8 - size_t * 2 + size_t * idx, @dumper, head: true)
    f.index = idx
    f
  end
  @unsorted_bin = UnsortedBin.new(size_t, top_ptr_offset, @dumper, head: true)
  @smallbin = Array.new(62) do |idx|
    s = Smallbin.new(size_t, @base + 8 + size_t * (12 + 2 * idx), @dumper, head: true)
    s.index = idx
    s
  end
  self
end