<commit_before>#!/usr/bin/env python
'''This module handles exits from IRSBs.'''

import symexec as se
from .s_helpers import ondemand, translate_irconst

import logging
l = logging.getLogger("s_exit")

maximum_exit_split = 255

class SimExit(object):
	'''A SimExit tracks a state, the execution point, and the condition to take a jump.'''

	def __init__(self, sirsb_exit = None, sirsb_postcall = None, sexit = None, addr=None, expr=None, state=None, jumpkind=None, guard=None, simple_postcall=True, simplify=True, state_is_raw=True):
		'''
		Creates a SimExit. Takes the following groups of parameters:

			@param sirsb_exit: the SimIRSB to exit from
			@param sexit: an exit statement to exit from

			@param sirsb_postcall: the SimIRSB to perform a ret-emulation for, to facilitate static analysis after function calls.
			@param simple_postcall: the the ret-emulation simply (ie, the next instruction after the call) instead of actually emulating a ret

			@param addr: an address to exit to
			@param expr: an address (z3 expression) to exit to
			@param state: the state for the addr and expr options
			@param guard: the guard condition for the addr and expr options. Default True
			@param jumpkind: the jumpind

			@param simplify: simplify the state and various expressions
			@param state_is_raw: a True value signifies that the state has not had the guard instruction added to it, and should have it added.
		'''

		# the state of exit, without accounting for the guard condition
		self.raw_state = None

		# the type of jump
		self.jumpkind = None

		# the target of the exit the guard condition
		self.target = None
		self.guard = None

		# set the right type of exit
		if sirsb_exit is not None:
			self.set_irsb_exit(sirsb_exit)
		elif sirsb_postcall is not None:
			self.set_postcall(sirsb_postcall, simple_postcall)
		elif sexit is not None:
			self.set_stmt_exit(sexit)
		elif addr is not None and state is not None:
			self.set_addr_exit(addr, state, guard)
		elif expr is not None and state is not None:
			self.set_expr_exit(expr, state, guard)
		else:
			raise Exception("Invalid SimExit creation.")

		if jumpkind is not None:
			self.jumpkind = jumpkind

		if state_is_raw:
			if o.COW_STATES in self.raw_state.options:
				self.state = self.raw_state.copy()
			elif o.SINGLE_EXIT not in self.raw_state.options:
				raise Exception("COW_STATES *must* be used with SINGLE_EXIT for now.")
			else:
				self.state = self.raw_state

			if se.is_symbolic(self.guard):
				self.state.add_constraints(self.guard)
		else:
			self.state = self.raw_state

		# simplify constraints to speed this up
		if simplify:
			self.state.simplify()
			self.target = se.simplify_expression(self.target)
			self.guard = se.simplify_expression(self.guard)

		# the sim_values for the target and guard
		self.target_value = self.state.expr_value(self.target)
		self.guard_value = self.state.expr_value(self.guard)

		if self.target_value.is_symbolic():
			l.debug("Made exit to symbolic expression.")
		else:
			l.debug("Made exit to address 0x%x.", self.target_value.any())

		if o.DOWNSIZE_Z3 in self.state.options:
			self.downsize()

	def downsize(self):
		# precache, so we don't have to upsize
		_ = self.is_unique()
		_ = self.reachable()
		try:
			_ = self.concretize()
		except ConcretizingException:
			pass

		self.state.downsize()

	@property<commit_msg>OMG FREAKING RAW_STATE ATE ALL THE FREKING MEMORY!!!!!!!!<commit_after>#!/usr/bin/env python
'''This module handles exits from IRSBs.'''

import symexec as se
from .s_helpers import ondemand, translate_irconst

import logging
l = logging.getLogger("s_exit")

maximum_exit_split = 255

class SimExit(object):
	'''A SimExit tracks a state, the execution point, and the condition to take a jump.'''

	def __init__(self, sirsb_exit = None, sirsb_postcall = None, sexit = None, addr=None, expr=None, state=None, jumpkind=None, guard=None, simple_postcall=True, simplify=True, state_is_raw=True):
		'''
		Creates a SimExit. Takes the following groups of parameters:

			@param sirsb_exit: the SimIRSB to exit from
			@param sexit: an exit statement to exit from

			@param sirsb_postcall: the SimIRSB to perform a ret-emulation for, to facilitate static analysis after function calls.
			@param simple_postcall: the the ret-emulation simply (ie, the next instruction after the call) instead of actually emulating a ret

			@param addr: an address to exit to
			@param expr: an address (z3 expression) to exit to
			@param state: the state for the addr and expr options
			@param guard: the guard condition for the addr and expr options. Default True
			@param jumpkind: the jumpind

			@param simplify: simplify the state and various expressions
			@param state_is_raw: a True value signifies that the state has not had the guard instruction added to it, and should have it added.
		'''

		# the state of exit, without accounting for the guard condition
		self.raw_state = None

		# the type of jump
		self.jumpkind = None

		# the target of the exit the guard condition
		self.target = None
		self.guard = None

		# set the right type of exit
		if sirsb_exit is not None:
			self.set_irsb_exit(sirsb_exit)
		elif sirsb_postcall is not None:
			self.set_postcall(sirsb_postcall, simple_postcall)
		elif sexit is not None:
			self.set_stmt_exit(sexit)
		elif addr is not None and state is not None:
			self.set_addr_exit(addr, state, guard)
		elif expr is not None and state is not None:
			self.set_expr_exit(expr, state, guard)
		else:
			raise Exception("Invalid SimExit creation.")

		if jumpkind is not None:
			self.jumpkind = jumpkind

		if state_is_raw:
			if o.COW_STATES in self.raw_state.options:
				self.state = self.raw_state.copy()
			elif o.SINGLE_EXIT not in self.raw_state.options:
				raise Exception("COW_STATES *must* be used with SINGLE_EXIT for now.")
			else:
				self.state = self.raw_state

			if se.is_symbolic(self.guard):
				self.state.add_constraints(self.guard)
		else:
			self.state = self.raw_state

		# we no longer need the raw state
		del self.raw_state

		# simplify constraints to speed this up
		if simplify:
			self.state.simplify()
			self.target = se.simplify_expression(self.target)
			self.guard = se.simplify_expression(self.guard)

		# the sim_values for the target and guard
		self.target_value = self.state.expr_value(self.target)
		self.guard_value = self.state.expr_value(self.guard)

		if self.target_value.is_symbolic():
			l.debug("Made exit to symbolic expression.")
		else:
			l.debug("Made exit to address 0x%x.", self.target_value.any())

		if o.DOWNSIZE_Z3 in self.state.options:
			self.downsize()

	def downsize(self):
		# precache, so we don't have to upsize
		_ = self.is_unique()
		_ = self.reachable()
		try:
			_ = self.concretize()
		except ConcretizingException:
			pass

		self.state.downsize()

	@property
{'fraction_non_alphanumeric': 0.08311384255194615, 'fraction_numerical': 0.001755926251097454, 'mean_word_length': 3.9031563845050217, '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': '3171416', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2162, 'n_words': 763}