Commits

Amaury Forgeot d'Arc committed ad51c32

Catch some common RPython errors in jit hooks

Comments (0)

Files changed (2)

pypy/module/pypyjit/interp_resop.py

         self.llbox = llbox
 
     def descr_getint(self, space):
-        return space.wrap(jit_hooks.box_getint(self.llbox))
+        try:
+            value = jit_hooks.box_getint(self.llbox)
+        except NotImplementedError:
+            raise OperationError(space.w_NotImplementedError,
+                                 space.wrap("Box has no int value"))
+        return space.wrap(value)
 
 @unwrap_spec(no=int)
 def descr_new_box(space, w_tp, no):
 
     @unwrap_spec(no=int)
     def descr_getarg(self, space, no):
-        return WrappedBox(jit_hooks.resop_getarg(self.op, no))
+        try:
+            box = jit_hooks.resop_getarg(self.op, no)
+        except IndexError:
+            raise OperationError(space.w_IndexError,
+                                 space.wrap("Index out of range"))
+        return WrappedBox(box)
 
     @unwrap_spec(no=int, w_box=WrappedBox)
     def descr_setarg(self, space, no, w_box):

pypy/module/pypyjit/test/test_jit_hook.py

         assert 'jit hook' in s.getvalue()
         assert 'ZeroDivisionError' in s.getvalue()
 
+    def test_on_compile_crashes(self):
+        import pypyjit
+        loops = []
+        def hook(loop):
+            loops.append(loop)
+        pypyjit.set_compile_hook(hook)
+        self.on_compile()
+        loop = loops[0]
+        op = loop.operations[2]
+        # Should not crash the interpreter
+        raises(IndexError, op.getarg, 2)
+        assert op.name == 'guard_nonnull'
+        raises(NotImplementedError, op.getarg(0).getint)
+
     def test_non_reentrant(self):
         import pypyjit
         l = []