def get_external_function_sandbox_graph(fnobj, db, force_stub=False): """Build the graph of a helper trampoline function to be used in place of real calls to the external function 'fnobj'. The trampoline marshals its input arguments, dumps them to STDOUT, and waits for an answer on STDIN. """ fnname = fnobj._name if hasattr(fnobj, 'graph'): # get the annotation of the input arguments and the result graph = fnobj.graph annotator = db.translator.annotator args_s = [annotator.binding(v) for v in graph.getargs()] s_result = annotator.binding(graph.getreturnvar()) else: # pure external function - fall back to the annotations # corresponding to the ll types FUNCTYPE = lltype.typeOf(fnobj) args_s = [lltype_to_annotation(ARG) for ARG in FUNCTYPE.ARGS] s_result = lltype_to_annotation(FUNCTYPE.RESULT) try: if force_stub: # old case - don't try to support suggested_primitive raise NotImplementedError("sandboxing for external function '%s'" % (fnname,)) dump_arguments = rmarshal.get_marshaller(tuple(args_s)) load_result = rmarshal.get_loader(s_result) except (NotImplementedError, rmarshal.CannotMarshal, rmarshal.CannotUnmarshall), e: msg = 'Not Implemented: %s' % (e,) log.WARNING(msg) def execute(*args): not_implemented_stub(msg)
def make_sandbox_trampoline(fnname, args_s, s_result): """Create a trampoline function with the specified signature. The trampoline is meant to be used in place of real calls to the external function named 'fnname'. It marshals its input arguments, dumps them to STDOUT, and waits for an answer on STDIN. """ try: dump_arguments = rmarshal.get_marshaller(tuple(args_s)) load_result = rmarshal.get_loader(s_result) except (rmarshal.CannotMarshal, rmarshal.CannotUnmarshall) as e: msg = "Cannot sandbox function '%s': %s" % (fnname, e) execute = make_stub(fnname, msg) else: def execute(*args): # marshal the function name and input arguments buf = [] dump_string(buf, fnname) dump_arguments(buf, args) # send the buffer and wait for the answer loader = sandboxed_io(buf) # decode the answer result = load_result(loader) loader.check_finished() return result execute.__name__ = 'sandboxed_%s' % (fnname, ) return execute
def get_external_function_sandbox_graph(fnobj, db, force_stub=False): """Build the graph of a helper trampoline function to be used in place of real calls to the external function 'fnobj'. The trampoline marshals its input arguments, dumps them to STDOUT, and waits for an answer on STDIN. """ fnname = fnobj._name if hasattr(fnobj, 'graph'): # get the annotation of the input arguments and the result graph = fnobj.graph annotator = db.translator.annotator args_s = [annotator.binding(v) for v in graph.getargs()] s_result = annotator.binding(graph.getreturnvar()) else: # pure external function - fall back to the annotations # corresponding to the ll types FUNCTYPE = lltype.typeOf(fnobj) args_s = [lltype_to_annotation(ARG) for ARG in FUNCTYPE.ARGS] s_result = lltype_to_annotation(FUNCTYPE.RESULT) try: if force_stub: # old case - don't try to support suggested_primitive raise NotImplementedError("sandboxing for external function '%s'" % (fnname, )) dump_arguments = rmarshal.get_marshaller(tuple(args_s)) load_result = rmarshal.get_loader(s_result) except (NotImplementedError, rmarshal.CannotMarshal, rmarshal.CannotUnmarshall), e: msg = 'Not Implemented: %s' % (e, ) log.WARNING(msg) def execute(*args): not_implemented_stub(msg)
def make_sandbox_trampoline(fnname, args_s, s_result): """Create a trampoline function with the specified signature. The trampoline is meant to be used in place of real calls to the external function named 'fnname'. It marshals its input arguments, dumps them to STDOUT, and waits for an answer on STDIN. """ try: dump_arguments = rmarshal.get_marshaller(tuple(args_s)) load_result = rmarshal.get_loader(s_result) except (rmarshal.CannotMarshal, rmarshal.CannotUnmarshall) as e: msg = "Cannot sandbox function '%s': %s" % (fnname, e) execute = make_stub(fnname, msg) else: def execute(*args): # marshal the function name and input arguments buf = [] dump_string(buf, fnname) dump_arguments(buf, args) # send the buffer and wait for the answer loader = sandboxed_io(buf) # decode the answer result = load_result(loader) loader.check_finished() return result execute.__name__ = 'sandboxed_%s' % (fnname,) return execute
def make_sandbox_trampoline(fnname, args_s, s_result): """Create a trampoline function with the specified signature. The trampoline is meant to be used in place of real calls to the external function named 'fnname'. It marshals its input arguments, dumps them to STDOUT, and waits for an answer on STDIN. """ try: dump_arguments = rmarshal.get_marshaller(tuple(args_s)) load_result = rmarshal.get_loader(s_result) print("Created sandbox for {}({})->{}".format(fnname, args_s, s_result)) except (rmarshal.CannotMarshal, rmarshal.CannotUnmarshall) as e: print("Trying to make sandbox function for {}({})->{}. Failed because of {} (type: {})".format(fnname, args_s, s_result, e, e.__class__)) import traceback, sys exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_tb(exc_traceback) # ignore special cases if fnname.startswith("ll_os.ll_os_exec") or fnname in ["ll_os.ll_os_fstatvfs", "ll_os.ll_os_statvfs"]: print("We'll let this function slide.") else: raise Exception("Stopping execution until we figure this out") msg = "Cannot sandbox function '%s': %s" % (fnname, e) execute = make_stub(fnname, msg) else: def execute(*args): # marshal the function name and input arguments buf = [] dump_string(buf, fnname) dump_arguments(buf, args) # send the buffer and wait for the answer loader = sandboxed_io(buf) # decode the answer result = load_result(loader) loader.check_finished() return result execute.__name__ = 'sandboxed_%s' % (fnname,) return execute
def execute(*args): not_implemented_stub(msg) execute.__name__ = 'sandboxed_%s' % (fnname, ) return execute def sig_ll(fnobj): FUNCTYPE = lltype.typeOf(fnobj) args_s = [lltype_to_annotation(ARG) for ARG in FUNCTYPE.ARGS] s_result = lltype_to_annotation(FUNCTYPE.RESULT) return args_s, s_result dump_string = rmarshal.get_marshaller(str) load_int = rmarshal.get_loader(int) def get_sandbox_stub(fnobj, rtyper): fnname = fnobj._name args_s, s_result = sig_ll(fnobj) msg = "Not implemented: sandboxing for external function '%s'" % (fnname, ) execute = make_stub(fnname, msg) return _annotate(rtyper, execute, args_s, s_result) def make_sandbox_trampoline(fnname, args_s, s_result): """Create a trampoline function with the specified signature. The trampoline is meant to be used in place of real calls to the external function named 'fnname'. It marshals its input arguments, dumps them to
elif error == 5: raise ZeroDivisionError elif error == 6: raise MemoryError elif error == 7: raise KeyError elif error == 8: raise IndexError else: raise RuntimeError @signature(types.str(), returns=types.impossible()) def not_implemented_stub(msg): STDERR = 2 with rffi.scoped_str2charp(msg + '\n') as buf: writeall_not_sandboxed(STDERR, buf, len(msg) + 1) raise RuntimeError(msg) # XXX in RPython, the msg is ignored at the moment dump_string = rmarshal.get_marshaller(str) load_int = rmarshal.get_loader(int) def get_external_function_sandbox_graph(fnobj, db, force_stub=False): """Build the graph of a helper trampoline function to be used in place of real calls to the external function 'fnobj'. The trampoline marshals its input arguments, dumps them to STDOUT, and waits for an answer on STDIN. """ fnname = fnobj._name if hasattr(fnobj, 'graph'): # get the annotation of the input arguments and the result graph = fnobj.graph annotator = db.translator.annotator args_s = [annotator.binding(v) for v in graph.getargs()] s_result = annotator.binding(graph.getreturnvar()) else: