<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from collections import OrderedDict

import gdb

import pwndbg.events
import pwndbg.typeinfo
from pwndbg.color import message
from pwndbg.constants import ptmalloc
from pwndbg.heap import heap_chain_limit

HEAP_MAX_SIZE     = 1024 * 1024

def heap_for_ptr(ptr):
    "find the heap and corresponding arena for a given ptr"
    return (ptr & ~(HEAP_MAX_SIZE-1))


class Heap(pwndbg.heap.heap.BaseHeap):
    def __init__(self):
        # Global ptmalloc objects
        self._main_arena    = None
        self._mp            = None
        # ptmalloc cache for current thread
        self._thread_cache  = None


    @property
    def main_arena(self):
        main_arena_addr = pwndbg.symbol.address('main_arena')

        if main_arena_addr is not None:
            self._main_arena = pwndbg.memory.poi(self.malloc_state, main_arena_addr)
        else:
            print(message.error('Symbol \'main_arena\' not found. Try installing libc '
                                'debugging symbols and try again.'))

        return self._main_arena

    def has_tcache(self):
        return (self.mp and 'tcache_bins' in self.mp.type.keys() and self.mp['tcache_bins'])


    @property
    def thread_cache(self):
        tcache_addr = pwndbg.symbol.address('tcache')

        if tcache_addr is not None:
            try:
                self._thread_cache = pwndbg.memory.poi(self.tcache_perthread_struct, tcache_addr)
                _ = self._thread_cache['entries'].fetch_lazy()
            except Exception as e:
                print(message.error('Error fetching tcache. GDB cannot access '
                                    'thread-local variables unless you compile with -lpthread.'))
        else:
            if not has_tcache():
                print(message.warning('Your libc does not use thread cache'))
                return None

            print(message.error('Symbol \'tcache\' not found. Try installing libc '
                                'debugging symbols and try again.'))

        return self._thread_cache


    @property
    def mp(self):
        mp_addr = pwndbg.symbol.address('mp_')
<commit_msg>bug fix: tcache bin (#482)<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from collections import OrderedDict

import gdb

import pwndbg.events
import pwndbg.typeinfo
from pwndbg.color import message
from pwndbg.constants import ptmalloc
from pwndbg.heap import heap_chain_limit

HEAP_MAX_SIZE     = 1024 * 1024

def heap_for_ptr(ptr):
    "find the heap and corresponding arena for a given ptr"
    return (ptr & ~(HEAP_MAX_SIZE-1))


class Heap(pwndbg.heap.heap.BaseHeap):
    def __init__(self):
        # Global ptmalloc objects
        self._main_arena    = None
        self._mp            = None
        # ptmalloc cache for current thread
        self._thread_cache  = None


    @property
    def main_arena(self):
        main_arena_addr = pwndbg.symbol.address('main_arena')

        if main_arena_addr is not None:
            self._main_arena = pwndbg.memory.poi(self.malloc_state, main_arena_addr)
        else:
            print(message.error('Symbol \'main_arena\' not found. Try installing libc '
                                'debugging symbols and try again.'))

        return self._main_arena

    def has_tcache(self):
        return (self.mp and 'tcache_bins' in self.mp.type.keys() and self.mp['tcache_bins'])


    @property
    def thread_cache(self):
        tcache_addr = pwndbg.symbol.address('tcache')

        if tcache_addr is not None:
            try:
                self._thread_cache = pwndbg.memory.poi(self.tcache_perthread_struct, tcache_addr)
                _ = self._thread_cache['entries'].fetch_lazy()
            except Exception as e:
                print(message.error('Error fetching tcache. GDB cannot access '
                                    'thread-local variables unless you compile with -lpthread.'))
        else:
            if not self.has_tcache():
                print(message.warn('Your libc does not use thread cache'))
                return None

            print(message.error('Symbol \'tcache\' not found. Try installing libc '
                                'debugging symbols and try again.'))

        return self._thread_cache


    @property
    def mp(self):
        mp_addr = pwndbg.symbol.address('mp_')

{'fraction_non_alphanumeric': 0.08221225710014948, 'fraction_numerical': 0.0049113815929959425, 'mean_word_length': 2.2847124824684433, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6114669', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1337, 'n_words': 366}