Author: Armin Rigo <[email protected]>
Branch: sandbox-2
Changeset: r97261:0328e9de2ba0
Date: 2019-08-26 11:31 +0200
http://bitbucket.org/pypy/pypy/changeset/0328e9de2ba0/
Log: Direct tests
diff --git a/rpython/translator/sandbox/graphchecker.py
b/rpython/translator/sandbox/graphchecker.py
--- a/rpython/translator/sandbox/graphchecker.py
+++ b/rpython/translator/sandbox/graphchecker.py
@@ -78,6 +78,7 @@
# this will be transformed into a stdin/stdout stub
pass
else:
+ # not 'external', but no 'graph' either?
return "direct_call to %r" % (obj,)
elif opname in ('cast_ptr_to_adr', 'force_cast'):
diff --git a/rpython/translator/sandbox/test/test_graphchecker.py
b/rpython/translator/sandbox/test/test_graphchecker.py
new file mode 100644
--- /dev/null
+++ b/rpython/translator/sandbox/test/test_graphchecker.py
@@ -0,0 +1,102 @@
+from rpython.translator.translator import TranslationContext, graphof
+from rpython.rtyper.lltypesystem import lltype, rffi
+from rpython.rtyper.lltypesystem.lloperation import llop
+from rpython.rlib.objectmodel import sandbox_review
+
+from rpython.translator.sandbox.graphchecker import GraphChecker
+from rpython.translator.sandbox.graphchecker import make_abort_graph
+
+
+class TestGraphIsUnsafe(object):
+
+ def graph_is_unsafe(self, fn, signature=[]):
+ t = TranslationContext()
+ self.t = t
+ t.buildannotator().build_types(fn, signature)
+ t.buildrtyper().specialize()
+ graph = graphof(t, fn)
+
+ checker = GraphChecker(t)
+ return checker.graph_is_unsafe(graph)
+
+ def check_safe(self, fn, signature=[]):
+ result = self.graph_is_unsafe(fn, signature)
+ assert result is None, repr(fn)
+
+ def check_unsafe(self, error_substring, fn, signature=[]):
+ result = self.graph_is_unsafe(fn, signature)
+ assert result is not None, repr(fn)
+ assert error_substring in result
+
+ def test_simple(self):
+ def f():
+ pass
+ self.check_safe(f)
+
+ def test_unsafe_setfield(self):
+ S = lltype.Struct('S', ('x', lltype.Signed))
+ s = lltype.malloc(S, flavor='raw', immortal=True)
+ def f():
+ s.x = 42
+ self.check_unsafe("non-GC memory write", f)
+
+ def test_unsafe_operation(self):
+ def f():
+ llop.debug_forked(lltype.Void)
+ self.check_unsafe("unsupported llop", f)
+
+ def test_force_cast(self):
+ SRAW = lltype.Struct('SRAW', ('x', lltype.Signed))
+ SGC = lltype.GcStruct('SGC', ('x', lltype.Signed))
+ def f(x):
+ return llop.force_cast(lltype.Signed, x)
+ self.check_safe(f, [float])
+ self.check_safe(f, [lltype.Ptr(SRAW)])
+ self.check_unsafe("argument is a GC ptr", f, [lltype.Ptr(SGC)])
+
+ def test_direct_call_to_check_caller(self):
+ @sandbox_review(check_caller=True)
+ def g():
+ pass
+ def f():
+ g()
+ self.check_unsafe("caller has not been checked", f)
+
+ def test_direct_call_to_reviewed(self):
+ @sandbox_review(reviewed=True)
+ def g():
+ pass
+ def f():
+ g()
+ self.check_safe(f)
+
+ def test_direct_call_to_abort(self):
+ @sandbox_review(abort=True)
+ def g():
+ pass
+ def f():
+ g()
+ self.check_safe(f)
+
+ def test_direct_call_external(self):
+ llfn1 = rffi.llexternal("foobar", [], lltype.Void, sandboxsafe=True,
+ _nowrapper=True)
+ self.check_safe(lambda: llfn1)
+ #
+ llfn2 = rffi.llexternal("foobar", [], lltype.Void, sandboxsafe=False,
+ _nowrapper=True)
+ self.check_safe(lambda: llfn2) # will be turned into an I/O stub
+ #
+ llfn3 = rffi.llexternal("foobar", [], lltype.Void, sandboxsafe=True)
+ self.check_safe(lambda: llfn3)
+ #
+ llfn4 = rffi.llexternal("foobar", [], lltype.Void, sandboxsafe=False)
+ self.check_safe(lambda: llfn4)
+
+ def test_make_abort_graph(self):
+ def dummy():
+ pass
+ self.check_safe(dummy)
+ graph = graphof(self.t, dummy)
+ make_abort_graph(graph)
+ assert graph.startblock.operations[0].opname == 'debug_fatalerror'
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit