1from angr.engines.vex.light import VEXMixin
2from angr import errors
3
4raiseme = object()
5class VEXResilienceMixin(VEXMixin):
6    def _check_unsupported_ccall(self, func_name, retty, args, **kwargs):
7        return raiseme
8
9    def _check_errored_ccall(self, func_name, ty, args, **kwargs):
10        return raiseme
11
12    def _check_unsupported_op(self, op, args):
13        return raiseme
14
15    def _check_zero_division(self, op, args):
16        return raiseme
17
18    def _check_errored_op(self, op, args):
19        return raiseme
20
21    def _check_unsupported_dirty(self, func_name, ty, args, **kwargs):
22        return raiseme
23
24    def _check_errored_dirty(self, func_name, ty, args, **kwargs):
25        return raiseme
26
27    def _check_errored_stmt(self, stmt):
28        return raiseme
29
30def _make_wrapper(func, *args):
31    excs = args[::2]
32    handlers = args[1::2]
33
34    def inner(self, *iargs, **ikwargs):
35        try:
36            return getattr(super(VEXResilienceMixin, self), func)(*iargs, **ikwargs)
37        except excs as e:
38            for exc, handler in zip(excs, handlers):
39                if isinstance(e, exc):
40                    v = getattr(self, handler)(*iargs, **ikwargs)
41                    if v is raiseme:
42                        raise
43                    return v
44            assert False, "this should be unreachable if Python is working correctly"
45    setattr(VEXResilienceMixin, func, inner)
46
47_make_wrapper('_perform_vex_stmt_Dirty_call', errors.UnsupportedDirtyError, '_check_unsupported_dirty', errors.SimOperationError, '_check_errored_dirty')
48_make_wrapper('_perform_vex_expr_CCall', errors.UnsupportedCCallError, '_check_unsupported_ccall', errors.SimOperationError, '_check_errored_ccall')
49_make_wrapper('_perform_vex_expr_Op', errors.SimZeroDivisionException, '_check_zero_division', errors.UnsupportedIROpError, '_check_unsupported_op')
50_make_wrapper('_handle_vex_stmt', errors.SimError, '_check_errored_stmt')
51