<commit_before>#!/usr/bin/env python
'''This module handles constraint generation.'''

import symexec
import s_irop
import s_ccall
import s_value
import s_helpers
import s_exception

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

class UnsupportedIRExprType(Exception):
	pass

# translates several IRExprs, taking into account generated constraints along the way
def translate_irexprs(exprs, state, other_constraints = [ ]):
	constraints = [ ]
	args = [ ]
	s_exprs = [ ]

	for a in exprs:
		e = SimIRExpr(a, state, other_constraints + constraints)
		s_a, s_c = e.expr_and_constraints()

		s_exprs.append(e)
		args.append(s_a)
		constraints += s_c

	return args, constraints, s_exprs


class SimIRExpr:
	def __init__(self, expr, state, other_constraints = None, mode = "symbolic"):
		self.mode = mode
		self.state = state
		self.state_constraints = state.constraints_after()
		self.other_constraints = other_constraints if other_constraints else [ ]
		self.constraints = [ ]
		self.data_reads = [ ]

		func_name = mode + "_" + type(expr).__name__
		l.debug("Looking for handler for IRExpr %s in mode %s" % (type(expr), mode))
		if hasattr(self, func_name):
			getattr(self, func_name)(expr)
		else:
			raise UnsupportedIRExprType("Unsupported expression type %s in mode %s." % (type(expr), mode))

		self.sim_value = s_value.SimValue(self.expr, self.constraints + self.other_constraints + self.state_constraints)

	def expr_and_constraints(self):
		return self.expr, self.constraints

	##################################
	### Static expression handlers ###
	##################################
	def static_Get(self, expr):
		raise s_exception.SimModeError("Get unsupported in static mode.")
	
	def static_op(self, expr):
		raise s_exception.SimModeError("Unop/Binop/Triop/Qop unsupported in static mode.")

	static_Unop = static_op
	static_Binop = static_op
	static_Triop = static_op
	static_Qop = static_op
	
	def static_RdTmp(self, expr):
		self.expr = self.state.temps[expr.tmp]
	
	def static_Const(self, expr):
		self.expr = s_helpers.translate_irconst(expr.con)
	
	def static_Load(self, expr):
		# get the address
		addr = SimIRExpr(expr.addr, self.state, self.other_constraints, mode=self.mode)
		if addr.sim_value.is_symbolic():
			raise s_exception.SimModeError("Can't handle symbolic address in static mode.")

		# TODO: this is a hack
		self.expr = addr

		self.data_reads.append(addr.sim_value)
	
	def static_CCall(self, expr):
		raise s_exception.SimModeError("Unop/Binop/Triop/Qop unsupported in static mode.")

	def static_Mux0X(self, expr):
		cond =  SimIRExpr(expr.cond,  self.state, self.other_constraints, mode=self.mode)
		expr0 = SimIRExpr(expr.expr0, self.state, self.other_constraints, mode=self.mode)
		exprX = SimIRExpr(expr.exprX, self.state, self.other_constraints, mode=self.mode)

		if cond.sim_value.is_symbolic():
			raise s_exception.SimModeError("Can't handle symbolic cond in static mode.")

		if cond.sim_value().any() == 0:
			e = expr0
		else:
			e = exprX

		if e.sim_value.is_symbolic():
			raise s_exception.SimModeError("Can't handle symbolic expression in static mode.")
	
		self.expr = e

	####################################
	### Symbolic expression handlers ###
	####################################
	def symbolic_Get(self, expr):
		# TODO: make sure the way we're handling reads of parts of registers is correct
		size = s_helpers.get_size(expr.type)
<commit_msg>static work<commit_after>#!/usr/bin/env python
'''This module handles constraint generation.'''

import symexec
import s_irop
import s_ccall
import s_value
import s_helpers
import s_exception

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

class UnsupportedIRExprType(Exception):
	pass

# translates several IRExprs, taking into account generated constraints along the way
def translate_irexprs(exprs, state, other_constraints = [ ], mode = "symbolic"):
	constraints = [ ]
	args = [ ]
	s_exprs = [ ]

	for a in exprs:
		e = SimIRExpr(a, state, other_constraints + constraints, mode=mode)
		s_a, s_c = e.expr_and_constraints()

		s_exprs.append(e)
		args.append(s_a)
		constraints += s_c

	return args, constraints, s_exprs


class SimIRExpr:
	def __init__(self, expr, state, other_constraints = None, mode = "symbolic"):
		self.mode = mode
		self.state = state
		self.state_constraints = state.constraints_after()
		self.other_constraints = other_constraints if other_constraints else [ ]
		self.constraints = [ ]
		self.data_reads = [ ]

		self.sim_value = None
		self.expr = None

		func_name = mode + "_" + type(expr).__name__
		l.debug("Looking for handler for IRExpr %s in mode %s" % (type(expr), mode))
		if hasattr(self, func_name):
			getattr(self, func_name)(expr)
		else:
			raise UnsupportedIRExprType("Unsupported expression type %s in mode %s." % (type(expr), mode))

		self.sim_value = s_value.SimValue(self.expr, self.constraints + self.other_constraints + self.state_constraints)

	def expr_and_constraints(self):
		return self.expr, self.constraints

	##################################
	### Static expression handlers ###
	##################################
	def static_Get(self, expr):
		pass
	
	def static_op(self, expr):
		pass

	static_Unop = static_op
	static_Binop = static_op
	static_Triop = static_op
	static_Qop = static_op
	
	def static_RdTmp(self, expr):
		self.expr = self.state.temps[expr.tmp]
	
	def static_Const(self, expr):
		self.expr = s_helpers.translate_irconst(expr.con)
	
	def static_Load(self, expr):
		# get the address
		addr = SimIRExpr(expr.addr, self.state, self.other_constraints, mode=self.mode)
		if addr.sim_value.is_symbolic():
			raise s_exception.SimModeError("Can't handle symbolic address in static mode.")

		# TODO: this is a hack
		self.expr = addr

		self.data_reads.append(addr.sim_value)
	
	def static_CCall(self, expr):
		_,_,exprs = translate_irexprs(expr.args(), self.state, self.other_constraints, mode="static")
		for e in exprs:
			self.data_reads.extend(e.data_reads)

	def static_Mux0X(self, expr):
		expr0 = SimIRExpr(expr.expr0, self.state, self.other_constraints, mode=self.mode)
		exprX = SimIRExpr(expr.exprX, self.state, self.other_constraints, mode=self.mode)

		self.data_reads.extend(expr0.data_reads)
		self.data_reads.extend(exprX.data_reads)

	####################################
	### Symbolic expression handlers ###
	####################################
	def symbolic_Get(self, expr):
		# TODO: make sure the way we're handling reads of parts of registers is correct
		size = s_helpers.get_size(expr.type)

{'fraction_non_alphanumeric': 0.14808549900046133, 'fraction_numerical': 0.0013839766261725358, 'mean_word_length': 4.891304347826087, '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': '4290813', 'n_tokens_mistral': 2370, 'n_tokens_neox': 2167, 'n_words': 517}