<commit_before>import struct
import traceback
import itertools

import envi
import envi.bits as e_bits
import envi.memory as e_mem
import envi.registers as e_reg

import visgraph.pathcore as vg_path

from vivisect.const import *

# Pre-initialize a stack memory bytes
init_stack_map = ''
for i in xrange(8192/4):
    init_stack_map += struct.pack("<I", 0xfefe0000+(i*4))

def imphook(impname):

    def imptemp(f):
        f.__imphook__ = impname
        return f

    return imptemp

class WorkspaceEmulator:

    taintregs = []

    def __init__(self, vw, logwrite=False, logread=False):

        self.vw = vw
        self.funcva = None # Set if using runFunction
        self.emustop = False

        self.hooks = {}
        self.taints = {}
        self.taintva = itertools.count(0x41560000, 8192)

        self.uninit_use = {}
        self.logwrite = logwrite
        self.logread = logread
        self.path = self.newCodePathNode()
        self.curpath = self.path
        self.opcache = {}
        self.emumon = None
        self.psize = self.getPointerSize()

        self.stack_map_mask = e_bits.sign_extend(0xfff00000, 4, vw.psize)
        self.stack_map_base = e_bits.sign_extend(0xbfb00000, 4, vw.psize)
        self.stack_pointer = self.stack_map_base + 4096

        # Possibly need an "options" API?
        self._safe_mem = True   # Should we be forgiving about memory accesses?
        self._func_only = True  # is this emulator meant to stay in one function scope?

        # Map in a memory map for the stack
        self.addMemoryMap(self.stack_map_base, 6, "[stack]", init_stack_map)

        # Map in all the memory associated with the workspace
        for va, size, perms, fname in vw.getMemoryMaps():
            offset, bytes = vw.getByteDef(va)
            self.addMemoryMap(va, perms, fname, bytes)

        for regidx in self.taintregs:
            rname = self.getRegisterName(regidx)
            regval = self.setVivTaint( 'uninitreg', regidx )
            self.setRegister(regidx, regval)

        self.setStackCounter(self.stack_pointer)

        # Create some pre-made taints for positive stack indexes
        # NOTE: This is *ugly* for speed....<commit_msg>added op as a class variable. Its set in runFunction but not initialized by the class. There is a race triggered by some binaries (have the md5s) when emulating certain paths functions relying on op existing specifically getRegister in Amd64WorkspaceEmulator check for the variable before its set by runFunction<commit_after>import struct
import traceback
import itertools

import envi
import envi.bits as e_bits
import envi.memory as e_mem
import envi.registers as e_reg

import visgraph.pathcore as vg_path

from vivisect.const import *

# Pre-initialize a stack memory bytes
init_stack_map = ''
for i in xrange(8192/4):
    init_stack_map += struct.pack("<I", 0xfefe0000+(i*4))

def imphook(impname):

    def imptemp(f):
        f.__imphook__ = impname
        return f

    return imptemp

class WorkspaceEmulator:

    taintregs = []

    def __init__(self, vw, logwrite=False, logread=False):

        self.vw = vw
        self.funcva = None # Set if using runFunction
        self.emustop = False

        self.hooks = {}
        self.taints = {}
        self.taintva = itertools.count(0x41560000, 8192)

        self.uninit_use = {}
        self.logwrite = logwrite
        self.logread = logread
        self.path = self.newCodePathNode()
        self.curpath = self.path
        self.op = None
        self.opcache = {}
        self.emumon = None
        self.psize = self.getPointerSize()

        self.stack_map_mask = e_bits.sign_extend(0xfff00000, 4, vw.psize)
        self.stack_map_base = e_bits.sign_extend(0xbfb00000, 4, vw.psize)
        self.stack_pointer = self.stack_map_base + 4096

        # Possibly need an "options" API?
        self._safe_mem = True   # Should we be forgiving about memory accesses?
        self._func_only = True  # is this emulator meant to stay in one function scope?

        # Map in a memory map for the stack
        self.addMemoryMap(self.stack_map_base, 6, "[stack]", init_stack_map)

        # Map in all the memory associated with the workspace
        for va, size, perms, fname in vw.getMemoryMaps():
            offset, bytes = vw.getByteDef(va)
            self.addMemoryMap(va, perms, fname, bytes)

        for regidx in self.taintregs:
            rname = self.getRegisterName(regidx)
            regval = self.setVivTaint( 'uninitreg', regidx )
            self.setRegister(regidx, regval)

        self.setStackCounter(self.stack_pointer)

        # Create some pre-made taints for positive stack indexes
        # NOTE: This is *ugly* for speed....
{'fraction_non_alphanumeric': 0.08324485028668507, 'fraction_numerical': 0.018899978764068806, 'mean_word_length': 2.587204874333587, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5803195', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1479, 'n_words': 421}