<commit_before>import simuvex
from itertools import count

fastpath_data_counter = count()

class receive(simuvex.SimProcedure):
    #pylint:disable=arguments-differ

    IS_SYSCALL = True

    def run(self, fd, buf, count, rx_bytes):

        if self.state.mode == 'fastpath':
            # Special case for CFG generation
            if not self.state.se.symbolic(count):
                actual_size = count
                data = self.state.se.Unconstrained(
                    'receive_data_%d' % fastpath_data_counter.next(),
                    self.state.se.exactly_int(actual_size) * 8
                )
                self.state.memory.store(buf, data)
            else:
                actual_size = self.state.se.Unconstrained('receive_length', self.state.arch.bits)
            self.state.memory.store(rx_bytes, actual_size, endness='Iend_LE')

            return self.state.se.BVV(0, self.state.arch.bits)

        if CGC_NO_SYMBOLIC_RECEIVE_LENGTH in self.state.options:
            read_length = self.state.posix.read(fd, buf, count)

            self.state.memory.store(rx_bytes, read_length, condition=rx_bytes != 0, endness='Iend_LE')
            self.size = read_length

            return self.state.se.BVV(0, self.state.arch.bits)
        else:
            if ABSTRACT_MEMORY in self.state.options:
                actual_size = count
            else:
                actual_size = self.state.se.Unconstrained('receive_length', self.state.arch.bits)
                self.state.add_constraints(self.state.se.ULE(actual_size, count), action=True)

            if self.state.satisfiable(extra_constraints=[count != 0]):
                read_length = self.state.posix.read(fd, buf, actual_size)
                list(self.state.log.actions)[-1].size.ast = actual_size
                list(self.state.log.actions)[-2].data.ast = list(self.state.log.actions)[-1].actual_value.ast
                self.data = self.state.memory.load(buf, read_length)
            else:
                self.data = None

            self.size = actual_size
            self.state.memory.store(rx_bytes, actual_size, condition=rx_bytes != 0, endness='Iend_LE')

            # return values
            return self.state.se.If(actual_size == 0,
                                    self.state.se.BVV(0xffffffff, self.state.arch.bits),
                                    self.state.se.BVV(0, self.state.arch.bits)
                                    )

from simuvex.s_options import ABSTRACT_MEMORY, CGC_NO_SYMBOLIC_RECEIVE_LENGTH
<commit_msg>be resilient to writes failing<commit_after>import simuvex
from itertools import count

fastpath_data_counter = count()

class receive(simuvex.SimProcedure):
    #pylint:disable=arguments-differ,attribute-defined-outside-init,redefined-outer-name

    IS_SYSCALL = True

    def run(self, fd, buf, count, rx_bytes):

        if self.state.mode == 'fastpath':
            # Special case for CFG generation
            if not self.state.se.symbolic(count):
                actual_size = count
                data = self.state.se.Unconstrained(
                    'receive_data_%d' % fastpath_data_counter.next(),
                    self.state.se.exactly_int(actual_size) * 8
                )
                self.state.memory.store(buf, data)
            else:
                actual_size = self.state.se.Unconstrained('receive_length', self.state.arch.bits)
            self.state.memory.store(rx_bytes, actual_size, endness='Iend_LE')

            return self.state.se.BVV(0, self.state.arch.bits)

        if CGC_NO_SYMBOLIC_RECEIVE_LENGTH in self.state.options:
            read_length = self.state.posix.read(fd, buf, count)

            self.state.memory.store(rx_bytes, read_length, condition=rx_bytes != 0, endness='Iend_LE')
            self.size = read_length

            return self.state.se.BVV(0, self.state.arch.bits)
        else:
            if ABSTRACT_MEMORY in self.state.options:
                actual_size = count
            else:
                actual_size = self.state.se.Unconstrained('receive_length', self.state.arch.bits)
                self.state.add_constraints(self.state.se.ULE(actual_size, count), action=True)

            if self.state.satisfiable(extra_constraints=[count != 0]):
                read_length = self.state.posix.read(fd, buf, actual_size)
                action_list = list(self.state.log.actions)

                try:
                    # get and fix up the memory write
                    action = next(
                        a for a in reversed(action_list) if
                        isinstance(a, SimActionData) and a.action == 'write' and a.type == 'mem'
                    )
                    action.size.ast = actual_size
                    action.data.ast = action.actual_value.ast
                    self.data = self.state.memory.load(buf, read_length)
                except StopIteration:
                    # the write didn't occur (i.e., size of 0)
                    self.data = None
            else:
                self.data = None

            self.size = actual_size
            self.state.memory.store(rx_bytes, actual_size, condition=rx_bytes != 0, endness='Iend_LE')

            # return values
            return self.state.se.If(
                actual_size == 0,
                self.state.se.BVV(0xffffffff, self.state.arch.bits),
                self.state.se.BVV(0, self.state.arch.bits)
            )

from ...s_options import ABSTRACT_MEMORY, CGC_NO_SYMBOLIC_RECEIVE_LENGTH
from ...s_action import SimActionData

{'fraction_non_alphanumeric': 0.1009009009009009, 'fraction_numerical': 0.003963963963963964, 'mean_word_length': 1.8481272447408927, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5608672', 'n_tokens_mistral': 1711, 'n_tokens_neox': 1604, 'n_words': 287}