def summarize_execution( fn: Callable, args: Sequence[object] = (), kwargs: Mapping[str, object] = None, detach_path: bool = True, ) -> ExecutionResult: if not kwargs: kwargs = {} ret = None exc = None try: symbolic_ret = fn(*args, **kwargs) if detach_path: context_statespace().detach_path() _ret = realize(symbolic_ret) # TODO, this covers up potential issues with return types. Handle differently? # summarize iterators as the values they produce: if hasattr(_ret, "__next__"): ret = list(_ret) else: ret = _ret except BaseException as e: if isinstance(e, (UnexploredPath, IgnoreAttempt)): raise if in_debug(): debug("hit exception:", type(e), e, test_stack(e.__traceback__)) exc = e if detach_path: context_statespace().detach_path() args = tuple(realize(a) for a in args) kwargs = {k: realize(v) for (k, v) in kwargs.items()} return ExecutionResult(ret, exc, args, kwargs)
def _fullmatch(self, string, pos=0, endpos=None): with NoTracing(): if type(string) is SymbolicStr: try: return _match_pattern(self, self.pattern + r"\Z", string, pos, endpos) except ReUnhandled as e: debug( "Unable to symbolically analyze regular expression:", self.pattern, e, ) if endpos is None: return re.Pattern.fullmatch(self, realize(string), pos) else: return re.Pattern.fullmatch(self, realize(string), pos, endpos)
def _match(self, string, pos=0, endpos=None) -> Union[None, re.Match, _Match]: with NoTracing(): if type(string) is SymbolicStr: try: return _match_pattern(self, self.pattern, string, pos, endpos) except ReUnhandled as e: debug( "Unable to symbolically analyze regular expression:", self.pattern, e, ) if endpos is None: return _orig_match(self, realize(string), pos) else: return _orig_match(self, realize(string), pos, endpos)
def symbolic_run( self, fn: Callable[[TrackingStateSpace], bool] ) -> Tuple[object, Optional[BaseException]]: search_root = SinglePathNode(True) patched_builtins = PatchedBuiltins(contracted_builtins.__dict__, enabled=lambda: True) with patched_builtins: for itr in range(1, 200): debug('iteration', itr) space = TrackingStateSpace(time.time() + 10.0, 1.0, search_root=search_root) try: return (realize(fn(space)), None) except IgnoreAttempt as e: debug('ignore iteration attempt: ', str(e)) pass except BaseException as e: #traceback.print_exc() return (None, e) top_analysis, space_exhausted = space.bubble_status( CallAnalysis()) if space_exhausted: return ( None, CrosshairInternal(f'exhausted after {itr} iterations')) return (None, CrosshairInternal( 'Unable to find a successful symbolic execution'))
def symbolic_run( self, fn: Callable[[TrackingStateSpace], object] ) -> Tuple[object, Optional[BaseException], TrackingStateSpace]: search_root = SinglePathNode(True) with Patched(enabled=lambda: True): for itr in range(1, 200): debug('iteration', itr) space = TrackingStateSpace(time.monotonic() + 10.0, 1.0, search_root=search_root) try: with StateSpaceContext(space): ret = (realize(fn(space)), None, space) space.check_deferred_assumptions() return ret except IgnoreAttempt as e: debug('ignore iteration attempt: ', str(e)) pass except BaseException as e: debug(traceback.format_exc()) return (None, e, space) top_analysis, space_exhausted = space.bubble_status( CallAnalysis()) if space_exhausted: return ( None, CrosshairInternal(f'exhausted after {itr} iterations'), space) return (None, CrosshairInternal( 'Unable to find a successful symbolic execution'), space)
def symbolic_run( self, fn: Callable[[TrackingStateSpace], bool] ) -> Tuple[object, Optional[BaseException]]: search_root = SinglePathNode(True) itr = 0 for _ in range(200): itr += 1 space = TrackingStateSpace(time.time() + 10.0, 1.0, search_root=search_root) try: return (realize(fn(space)), None) except IgnoreAttempt: pass except BaseException as e: #import traceback #traceback.print_exc() return (None, e) top_analysis, space_exhausted = space.bubble_status(CallAnalysis()) if space_exhausted: return (None, CrosshairInternal(f'exhausted after {itr} iterations')) return (None, CrosshairInternal( 'Unable to find a successful symbolic execution'))
def summarize_execution( fn: Callable, args: Sequence[object] = (), kwargs: Mapping[str, object] = None) -> ExecutionResult: if not kwargs: kwargs = {} ret = None exc = None try: _ret = realize(fn(*args, **kwargs)) # summarize iterators as the values they produce: if hasattr(_ret, "__next__"): ret = list(_ret) else: ret = _ret except BaseException as e: if isinstance(e, (UnexploredPath, IgnoreAttempt)): raise if in_debug(): debug(type(e), e, test_stack(e.__traceback__)) exc = e args = tuple(realize(a) for a in args) kwargs = {k: realize(v) for (k, v) in kwargs.items()} return ExecutionResult(ret, exc, args, kwargs)
def make(creator, *type_args): ret = smt_type(creator.space, creator.pytype, creator.varname) if creator.space.fork_parallel(false_probability=0.98): ret = realize(ret) debug('Prematurely realized', creator.pytype, 'value') return ret
def f(a: str, b: str) -> bool: """ post: implies(_, a == b) """ return realize(a) == b