<commit_before>import fcntl

import cgcrandom
import weakref
import errno
import os, struct
from ..utils.helpers import issymbolic
from ..core.cpu.abstractcpu import Interruption, Syscall, ConcretizeRegister
from ..core.cpu.cpufactory import CpuFactory
from ..core.memory import SMemory32, SMemory64, Memory32, Memory64
from ..core.smtlib import Operators, ConstraintSet
from elftools.elf.elffile import ELFFile
import logging
import random
from ..core.cpu.arm import *
from ..core.executor import SyscallNotImplemented, ProcessExit
logger = logging.getLogger("MODEL")


class RestartSyscall(Exception):
    pass

class Deadlock(Exception):
    pass

def perms_from_elf(elf_flags):
    return ['   ', '  x', ' w ', ' wx', 'r  ', 'r x', 'rw ', 'rwx'][elf_flags&7]

def perms_from_protflags(prot_flags):
    return ['   ', 'r  ', ' w ', 'rw ', '  x', 'r x', ' wx', 'rwx'][prot_flags&7]

class SymbolicSyscallArgument(Exception):
    def __init__(self, reg_num, message='Concretizing syscall argument', policy='SAMPLED'):
        self.reg_num = reg_num
        self.message = message
        self.policy = policy
        super(SymbolicSyscallArgument, self).__init__(message)


class File(object):
    def __init__(self, *args, **kwargs):
        #Todo: assert file is seekable otherwise we should save what was
        #read/write to the state
        self.file = file(*args,**kwargs)
    def stat(self):
        return os.fstat(self.fileno())
    def ioctl(self, request, argp):
        #argp ignored..
        return fcntl.fcntl(self, request)
    @property
    def name(self):
        return self.file.name
    @property
    def mode(self):
        return self.file.mode
    def tell(self, *args):
        return self.file.tell(*args)
    def seek(self, *args):
        return self.file.seek(*args)
    def write(self, *args):
        return self.file.write(*args)
    def read(self, *args):
        return self.file.read(*args)
    def close(self, *args):
        return self.file.close(*args)
    def fileno(self, *args):
        return self.file.fileno(*args)

    def __getstate__(self):
        state = {}
        state['name'] = self.name
        state['mode'] = self.mode
        state['pos'] = self.tell()
        return state

    def __setstate__(self, state):
        name = state['name']
        mode = state['mode']
        pos = state['pos']
        self.file = file(name, mode)
        self.seek(pos)

class SymbolicFile(object):
    '''
    Represents a symbolic file.
    '''
    def __init__(self, constraints, path="sfile", mode='rw', max_size=100, wildcard='+'):
        '''
        Builds a symbolic file

        :param constraints: the constraints
        :param path: the pathname of the symbolic file
        :type path: str or File
        :param str mode: the access permissions of the symbolic file
        :param max_size: Maximun amount of bytes of the symbolic file
        '''
        # XXX(yan): wildcard isn't used; check callers and remove.
        assert 'r' in mode
        if isinstance(path, str):
            path = File(path, mode)
        assert isinstance(path, File)

        #self._constraints = weakref.ref(constraints)
        WILDCARD = '+'

        symbols_cnt = 0
        data = path.read()
        size = len(data)
        self.array = constraints.new_array(name=path.name, max_size=size)
        for i in range(size):
            if data[i] != WILDCARD:
                self.array[i] = data[i]
            else:
                symbols_cnt+=1

        self._constraints = constraints
        self.pos = 0
        self.max_size=min(len(data), max_size)
        if symbols_cnt > max_size:
            logger.warning("Found more wilcards in the file than free symbolic values allowed (%d > %d)",symbols_cnt, max_size)
        else:
            logger.debug("Found %d free symbolic values on file %s",symbols_cnt, path.name)<commit_msg>Fix wrong variable name in SymbolicFile (#220)<commit_after>import fcntl

import cgcrandom
import weakref
import errno
import os, struct
from ..utils.helpers import issymbolic
from ..core.cpu.abstractcpu import Interruption, Syscall, ConcretizeRegister
from ..core.cpu.cpufactory import CpuFactory
from ..core.memory import SMemory32, SMemory64, Memory32, Memory64
from ..core.smtlib import Operators, ConstraintSet
from elftools.elf.elffile import ELFFile
import logging
import random
from ..core.cpu.arm import *
from ..core.executor import SyscallNotImplemented, ProcessExit
logger = logging.getLogger("MODEL")


class RestartSyscall(Exception):
    pass

class Deadlock(Exception):
    pass

def perms_from_elf(elf_flags):
    return ['   ', '  x', ' w ', ' wx', 'r  ', 'r x', 'rw ', 'rwx'][elf_flags&7]

def perms_from_protflags(prot_flags):
    return ['   ', 'r  ', ' w ', 'rw ', '  x', 'r x', ' wx', 'rwx'][prot_flags&7]

class SymbolicSyscallArgument(Exception):
    def __init__(self, reg_num, message='Concretizing syscall argument', policy='SAMPLED'):
        self.reg_num = reg_num
        self.message = message
        self.policy = policy
        super(SymbolicSyscallArgument, self).__init__(message)


class File(object):
    def __init__(self, *args, **kwargs):
        #Todo: assert file is seekable otherwise we should save what was
        #read/write to the state
        self.file = file(*args,**kwargs)
    def stat(self):
        return os.fstat(self.fileno())
    def ioctl(self, request, argp):
        #argp ignored..
        return fcntl.fcntl(self, request)
    @property
    def name(self):
        return self.file.name
    @property
    def mode(self):
        return self.file.mode
    def tell(self, *args):
        return self.file.tell(*args)
    def seek(self, *args):
        return self.file.seek(*args)
    def write(self, *args):
        return self.file.write(*args)
    def read(self, *args):
        return self.file.read(*args)
    def close(self, *args):
        return self.file.close(*args)
    def fileno(self, *args):
        return self.file.fileno(*args)

    def __getstate__(self):
        state = {}
        state['name'] = self.name
        state['mode'] = self.mode
        state['pos'] = self.tell()
        return state

    def __setstate__(self, state):
        name = state['name']
        mode = state['mode']
        pos = state['pos']
        self.file = file(name, mode)
        self.seek(pos)

class SymbolicFile(object):
    '''
    Represents a symbolic file.
    '''
    def __init__(self, constraints, path="sfile", mode='rw', max_size=100, wildcard='+'):
        '''
        Builds a symbolic file

        :param constraints: the constraints
        :param path: the pathname of the symbolic file
        :type path: str or File
        :param str mode: the access permissions of the symbolic file
        :param max_size: Maximun amount of bytes of the symbolic file
        '''
        # XXX(yan): wildcard isn't used; check callers and remove.
        assert 'r' in mode
        if isinstance(path, str):
            path = File(path, mode)
        assert isinstance(path, File)

        #self._constraints = weakref.ref(constraints)
        WILDCARD = '+'

        symbols_cnt = 0
        data = path.read()
        size = len(data)
        self.array = constraints.new_array(name=path.name, index_max=size)
        for i in range(size):
            if data[i] != WILDCARD:
                self.array[i] = data[i]
            else:
                symbols_cnt+=1

        self._constraints = constraints
        self.pos = 0
        self.max_size=min(len(data), max_size)
        if symbols_cnt > max_size:
            logger.warning("Found more wilcards in the file than free symbolic values allowed (%d > %d)",symbols_cnt, max_size)
        else:
            logger.debug("Found %d free symbolic values on file %s",symbols_cnt, path.name)
{'fraction_non_alphanumeric': 0.11647872885699641, 'fraction_numerical': 0.0044848795489492565, 'mean_word_length': 2.5951174573929063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7287925', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2349, 'n_words': 667}