<commit_before>#!/usr/bin/env python

from .plugin import SimStatePlugin

import sys
import functools
import logging
l = logging.getLogger("simuvex.s_solver")

def unsat_catcher(f):
    @functools.wraps(f)
    def wrapped_f(self, *args, **kwargs):
        try:
            return f(self, *args, **kwargs)
        except claripy.UnsatError:
            e_type, value, traceback = sys.exc_info()
            raise SimUnsatError, ("Got an unsat result", e_type, value), traceback
    return wrapped_f

def symbolic_guard(f):
    @functools.wraps(f)
    def guarded_f(self, *args, **kwargs):
        e = args[0]
        if o.SYMBOLIC not in self.state.options and self.symbolic(e):
            raise SimSolverModeError('SimSolver.%s() called on a symbolic variable without SYMBOLIC option' % f.__name__)
        return f(self, *args, **kwargs)
    return guarded_f

import claripy
class SimSolver(SimStatePlugin):
    def __init__(self, solver=None, claripy=None): #pylint:disable=redefined-outer-name
        l.debug("Creating SimSolverClaripy.")
        SimStatePlugin.__init__(self)

        self._claripy = claripy
        self._stored_solver = solver

    def _ana_getstate(self):
        return self._stored_solver, self.state

    def _ana_setstate(self, s):
        self._stored_solver, self.state = s
        self._claripy = None

    def set_state(self, state):
        SimStatePlugin.set_state(self, state)

        if self._claripy is None:
            if o.ABSTRACT_SOLVER in self.state.options:
                self._claripy = claripy.Claripies['VSA']
            #elif o.SYMBOLIC not in self.state.options:
            #   self._claripy = claripy.Claripies['Concrete']
            elif o.PARALLEL_SOLVES in self.state.options:
                self._claripy = claripy.Claripies['ParallelZ3']
            else:
                self._claripy = claripy.Claripies['SerialZ3']

    @property
    def _solver(self):
        if self._stored_solver is not None:
            return self._stored_solver

        if o.COMPOSITE_SOLVER in self.state.options:
            self._stored_solver = self._claripy.composite_solver()
        else:
            self._stored_solver = self._claripy.solver()
        return self._stored_solver

    @property
    def constraints(self):
        return self._solver.constraints

    #
    # Get unconstrained stuff
    #
    def Unconstrained(self, name, bits, **kwargs):
        if o.ABSTRACT_MEMORY in self.state.options:
            l.debug("Creating new zero StridedInterval")
            return self._claripy.TSI(bits=bits, name=name, signed=True, **kwargs)
        else:
            l.debug("Creating new unconstrained BV named %s", name)
            return self._claripy.BitVec(name, bits, **kwargs)

    #
    # Various passthroughs
    #

    def downsize(self):
        return self._solver.downsize()

    def __getattribute__(self, a):
        try:
            return SimStatePlugin.__getattribute__(self, a)<commit_msg>fix #49: generating a default value instead of a symbolic variable when accessing non-existant memory addresses in non-symbolic mode<commit_after>#!/usr/bin/env python

from .plugin import SimStatePlugin

import sys
import functools
import logging
l = logging.getLogger("simuvex.s_solver")

def unsat_catcher(f):
    @functools.wraps(f)
    def wrapped_f(self, *args, **kwargs):
        try:
            return f(self, *args, **kwargs)
        except claripy.UnsatError:
            e_type, value, traceback = sys.exc_info()
            raise SimUnsatError, ("Got an unsat result", e_type, value), traceback
    return wrapped_f

def symbolic_guard(f):
    @functools.wraps(f)
    def guarded_f(self, *args, **kwargs):
        e = args[0]
        if o.SYMBOLIC not in self.state.options and self.symbolic(e):
            raise SimSolverModeError('SimSolver.%s() called on a symbolic variable without SYMBOLIC option' % f.__name__)
        return f(self, *args, **kwargs)
    return guarded_f

import claripy
class SimSolver(SimStatePlugin):
    def __init__(self, solver=None, claripy=None): #pylint:disable=redefined-outer-name
        l.debug("Creating SimSolverClaripy.")
        SimStatePlugin.__init__(self)

        self._claripy = claripy
        self._stored_solver = solver

    def _ana_getstate(self):
        return self._stored_solver, self.state

    def _ana_setstate(self, s):
        self._stored_solver, self.state = s
        self._claripy = None

    def set_state(self, state):
        SimStatePlugin.set_state(self, state)

        if self._claripy is None:
            if o.ABSTRACT_SOLVER in self.state.options:
                self._claripy = claripy.Claripies['VSA']
            #elif o.SYMBOLIC not in self.state.options:
            #   self._claripy = claripy.Claripies['Concrete']
            elif o.PARALLEL_SOLVES in self.state.options:
                self._claripy = claripy.Claripies['ParallelZ3']
            else:
                self._claripy = claripy.Claripies['SerialZ3']

    @property
    def _solver(self):
        if self._stored_solver is not None:
            return self._stored_solver

        if o.COMPOSITE_SOLVER in self.state.options:
            self._stored_solver = self._claripy.composite_solver()
        else:
            self._stored_solver = self._claripy.solver()
        return self._stored_solver

    @property
    def constraints(self):
        return self._solver.constraints

    #
    # Get unconstrained stuff
    #
    def Unconstrained(self, name, bits, **kwargs):
        if o.SYMBOLIC in self.state.options:
            # Return a symbolic value
            if o.ABSTRACT_MEMORY in self.state.options:
                l.debug("Creating new zero StridedInterval")
                return self._claripy.TSI(bits=bits, name=name, signed=True, **kwargs)
            else:
                l.debug("Creating new unconstrained BV named %s", name)
                return self._claripy.BitVec(name, bits, **kwargs)
        else:
            # Return a default value, aka. 0
            return self._claripy.BitVecVal(0, bits)

    #
    # Various passthroughs
    #

    def downsize(self):
        return self._solver.downsize()

    def __getattribute__(self, a):
        try:
            return SimStatePlugin.__getattribute__(self, a)
{'fraction_non_alphanumeric': 0.09605872028083613, 'fraction_numerical': 0.0015956598053295037, 'mean_word_length': 2.445849367784497, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4007020', 'n_tokens_mistral': 2059, 'n_tokens_neox': 1982, 'n_words': 451}