Author: Armin Rigo <ar...@tunes.org> Branch: sandbox-lib Changeset: r83185:70ce574912b2 Date: 2016-03-20 10:13 +0100 http://bitbucket.org/pypy/pypy/changeset/70ce574912b2/
Log: tweaks diff --git a/rpython/translator/rsandbox/default.h b/rpython/translator/rsandbox/default.h new file mode 100644 --- /dev/null +++ b/rpython/translator/rsandbox/default.h @@ -0,0 +1,24 @@ +/*** translator/rsandbox/default.h ***/ + + +/* This is called by most default implementations of 'rsandbox_*' */ +__attribute__((noinline, noreturn)) +static void rsand_fatal(const char *fnname) +{ + fprintf(stderr, "The sandboxed program called the C function %s(), " + "but no implementation of this function was provided.\n", + fnname); + abort(); +} + + +/* Default implementation for some functions that don't abort */ + +static char *rsand_def_getenv(char *v) +{ + /* default implementation: "no such environment variable" */ + return NULL; +} + + +/*** generated code follows ***/ diff --git a/rpython/translator/rsandbox/rsandbox.py b/rpython/translator/rsandbox/rsandbox.py --- a/rpython/translator/rsandbox/rsandbox.py +++ b/rpython/translator/rsandbox/rsandbox.py @@ -1,4 +1,4 @@ -import py +import py, re from rpython.rtyper.lltypesystem import lltype, rffi from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.translator.c.support import cdecl @@ -33,20 +33,37 @@ #define _RSANDBOX_H_ #ifndef RPY_SANDBOX_EXPORTED -/* common definitions when including this file from an external C project */ +/* Common definitions when including this file from an external C project */ + +#include <stdlib.h> +#include <sys/utsname.h> + #define RPY_SANDBOX_EXPORTED extern + typedef long Signed; typedef unsigned long Unsigned; + #endif +/* The list of 'rsandbox_*' function pointers is automatically + generated. Most of these function pointers are initialized to + point to a function that aborts the sandboxed execution. The + sandboxed program cannot, by default, use any of them. A few + exceptions are provided, where the default implementation returns a + safe default; for example rsandbox_getenv(). +*/ '''] c_source = [''' +#include "common_header.h" +#include "rsandbox.h" #include <stdlib.h> -#include <sys/utsname.h> -#include "rsandbox.h" '''] + default_h = py.path.local(__file__).join('..', 'default.h').read() + c_source.append(default_h) + present = set(re.findall(r'\brsand_def_([a-zA-Z0-9_]+)[(]', default_h)) + fnnames = database._sandboxlib_fnnames for fnname in sorted(fnnames): FUNC = fnnames[fnname] @@ -57,13 +74,15 @@ emptyfuncname = 'rsand_def_' + fnname argnames = ['a%d' % i for i in range(len(FUNC.ARGS))] - c_source.append(""" + if fnname not in present: + c_source.append(""" static %s { - abort(); + rsand_fatal("%s"); }; -%s = %s; -""" % (cdecl(database.gettype(FUNC, argnames=argnames), emptyfuncname), - vardecl, emptyfuncname)) +""" % (cdecl(database.gettype(FUNC, argnames=argnames), emptyfuncname), fnname)) + else: + c_source.append('\n') + c_source.append("%s = %s;\n" % (vardecl, emptyfuncname)) c_header.append(''' #endif /* _RSANDBOX_H_ */ _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit