FIX #2231: add missing stack check when applying a PAP
authorSimon Marlow <marlowsd@gmail.com>
Mon, 2 Jun 2008 14:37:26 +0000 (14:37 +0000)
committerSimon Marlow <marlowsd@gmail.com>
Mon, 2 Jun 2008 14:37:26 +0000 (14:37 +0000)
This program makes a PAP with 203 arguments :-)

rts/Interpreter.c

index ab59533..d541dfc 100644 (file)
@@ -63,6 +63,7 @@
     SpLim = cap->r.rCurrentTSO->stack + RESERVED_STACK_WORDS;
 
 #define SAVE_STACK_POINTERS                    \
+    ASSERT(Sp > SpLim); \
     cap->r.rCurrentTSO->sp = Sp
 
 #define RETURN_TO_SCHEDULER(todo,retcode)      \
@@ -549,6 +550,16 @@ do_apply:
                goto defer_apply_to_sched;
            }
 
+            // Stack check: we're about to unpack the PAP onto the
+            // stack.  The (+1) is for the (arity < n) case, where we
+            // also need space for an extra info pointer.
+            if (Sp - (pap->n_args + 1) < SpLim) {
+                Sp -= 2;
+                Sp[1] = (W_)tagged_obj;
+                Sp[0] = (W_)&stg_enter_info;
+                RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
+            }
+
            Sp++;
            arity = pap->arity;
            ASSERT(arity > 0);