implemented subroutine verification (Coglio's method) + several verifier fixes
[cacao.git] / jit / stack.c
index b50342b2875e39dd4058d017b44084dd7980e861..3c3db3367fd73895a16341e311f8b13fe9c1aba0 100644 (file)
@@ -28,7 +28,7 @@
 
    Changes: Edwin Steiner
 
-   $Id: stack.c 787 2003-12-15 18:20:31Z edwin $
+   $Id: stack.c 868 2004-01-10 20:12:10Z edwin $
 
 */
 
@@ -37,6 +37,7 @@
 #include <string.h>
 #include "stack.h"
 #include "global.h"
+#include "main.h"
 #include "jit.h"
 #include "builtin.h"
 #include "disass.h"
@@ -50,6 +51,9 @@
 /* from codegen.inc */
 extern int dseglen;
 
+/**********************************************************************/
+/* Macros used internally by analyse_stack                            */
+/**********************************************************************/
 
 #ifdef STATISTICS
 #define COUNT(cnt) cnt++
@@ -57,28 +61,66 @@ extern int dseglen;
 #define COUNT(cnt)
 #endif
  
-#define STACKRESET {curstack=0;stackdepth=0;}
-
-#define TYPEPANIC  {show_icmd_method();panic("Stack type mismatch");}
-#define UNDERFLOW  {show_icmd_method();panic("Stack underflow");} /* XXX why isn't this caught in parse.c? */
+/* convenient abbreviations */
 #define CURKIND    curstack->varkind
 #define CURTYPE    curstack->type
 
+/*--------------------------------------------------*/
+/* SIGNALING ERRORS                                 */
+/*--------------------------------------------------*/
+
+#define TYPEPANIC  {show_icmd_method();panic("Stack type mismatch");}
+#define UNDERFLOW  {show_icmd_method();panic("Operand stack underflow");}
+#define OVERFLOW   {show_icmd_method();panic("Operand stack overflow");}
+
+/*--------------------------------------------------*/
+/* STACK UNDERFLOW/OVERFLOW CHECKS                  */
+/*--------------------------------------------------*/
+
+/* underflow checks */
 #define REQUIRE(num)  do { if (stackdepth<(num)) {UNDERFLOW;} } while(0)
 #define REQUIRE_1     REQUIRE(1)
 #define REQUIRE_2     REQUIRE(2)
 #define REQUIRE_3     REQUIRE(3)
 #define REQUIRE_4     REQUIRE(4)
 
+/* overflow check */
+/* XXX we allow ACONST instructions inserted as arguments to builtin
+ * functions to exceed the maximum stack depth.  Maybe we should check
+ * against maximum stack depth only at block boundaries?
+ */
+#define CHECKOVERFLOW                                                  \
+       do {                                                                            \
+               if (stackdepth > maxstack) {                    \
+                       if (iptr[0].opc != ICMD_ACONST          \
+                || iptr[0].op1 == 0)            \
+                       {OVERFLOW;}                                                     \
+               }                                                                               \
+       } while(0)
+
+/*--------------------------------------------------*/
+/* ALLOCATING STACK SLOTS                           */
+/*--------------------------------------------------*/
+
 #define NEWSTACK(s,v,n) {new->prev=curstack;new->type=s;new->flags=0;  \
                         new->varkind=v;new->varnum=n;curstack=new;new++;}
 #define NEWSTACKn(s,n)  NEWSTACK(s,UNDEFVAR,n)
 #define NEWSTACK0(s)    NEWSTACK(s,UNDEFVAR,0)
+
+/* allocate the input stack for an exception handler */
 #define NEWXSTACK   {NEWSTACK(TYPE_ADR,STACKVAR,0);curstack=0;}
 
+/*--------------------------------------------------*/
+/* STACK MANIPULATION                               */
+/*--------------------------------------------------*/
+
+/* resetting to an empty operand stack */
+#define STACKRESET {curstack=0;stackdepth=0;}
+
+/* set the output stack of the current instruction */
 #define SETDST      {iptr->dst=curstack;}
 
-/* The following macros do NOT check stackdepth, set stackdepth and iptr->dst */
+/* The following macros do NOT check stackdepth, set stackdepth or iptr->dst */
 #define POP(s)      {if(s!=curstack->type){TYPEPANIC;}                                                                         \
                      if(curstack->varkind==UNDEFVAR)curstack->varkind=TEMPVAR;\
                      curstack=curstack->prev;}
@@ -86,9 +128,20 @@ extern int dseglen;
                      curstack=curstack->prev;}
 #define COPY(s,d)   {(d)->flags=0;(d)->type=(s)->type;\
                      (d)->varkind=(s)->varkind;(d)->varnum=(s)->varnum;}
-/******************************/
 
-/* The following macros check stackdepth, set stackdepth and itpr->dst */
+/*--------------------------------------------------*/
+/* STACK OPERATIONS MODELING                        */
+/*--------------------------------------------------*/
+
+/* The following macros are used to model the stack manipulations of
+ * different kinds of instructions.
+ *
+ * These macros check the input stackdepth and they set the output
+ * stackdepth and the output stack of the instruction (iptr->dst).
+ *
+ * These macros do *not* check for stack overflows!
+ */
+   
 #define PUSHCONST(s){NEWSTACKn(s,stackdepth);SETDST;stackdepth++;}
 #define LOAD(s,v,n) {NEWSTACK(s,v,n);SETDST;stackdepth++;}
 #define STORE(s)    {REQUIRE_1;POP(s);SETDST;stackdepth--;}
@@ -136,8 +189,18 @@ extern int dseglen;
                     new[2].prev=new+1;new[3].prev=new+2;\
                     new[4].prev=new+3;new[5].prev=new+4;\
                     curstack=new+5;new+=6;SETDST;stackdepth+=2;}
-/******************************/
 
+/*--------------------------------------------------*/
+/* MACROS FOR HANDLING BASIC BLOCKS                 */
+/*--------------------------------------------------*/
+
+/* COPYCURSTACK makes a copy of the current operand stack (curstack)
+ * and returns it in the variable copy.
+ *
+ * This macro is used to propagate the operand stack from one basic
+ * block to another. The destination block receives the copy as its
+ * input stack.
+ */
 #define COPYCURSTACK(copy) {\
        int d;\
        stackptr s;\
@@ -162,6 +225,9 @@ extern int dseglen;
                copy=NULL;\
 }
 
+/* BBEND is called at the end of each basic block (after the last
+ * instruction of the block has been processed).
+ */
 
 #define BBEND(s,i){\
        i=stackdepth-1;\
@@ -189,21 +255,54 @@ extern int dseglen;
                }\
 }
 
-       
-#define MARKREACHED(b,c) {\
-       if(b->flags<0)\
-               {COPYCURSTACK(c);b->flags=0;b->instack=c;b->indepth=stackdepth;}\
-       else {stackptr s=curstack;stackptr t=b->instack;\
-               if(b->indepth!=stackdepth)\
-                       {show_icmd_method();panic("Stack depth mismatch");}\
-               while(s){if (s->type!=t->type)\
-                               TYPEPANIC\
-                       s=s->prev;t=t->prev;\
-                       }\
-               }\
+
+/* MARKREACHED marks the destination block <b> as reached. If this
+ * block has been reached before we check if stack depth and types
+ * match. Otherwise the destination block receives a copy of the
+ * current stack as its input stack.
+ *
+ * b...destination block
+ * c...current stack
+ */
+#define MARKREACHED(b,c) {                                                                                             \
+       if(b->flags<0)                                                                                                          \
+               {COPYCURSTACK(c);b->flags=0;b->instack=c;b->indepth=stackdepth;} \
+       else {stackptr s=curstack;stackptr t=b->instack;                                        \
+               if(b->indepth!=stackdepth)                                                                              \
+                       {show_icmd_method();panic("Stack depth mismatch");}                     \
+               while(s){if (s->type!=t->type)                                                                  \
+                               TYPEPANIC                                                                                               \
+                       s=s->prev;t=t->prev;                                                                            \
+                       }                                                                                                                       \
+               }                                                                                                                               \
 }
 
 
+/**********************************************************************/
+/* analyse_stack                                                      */
+/**********************************************************************/
+
+/* analyse_stack uses the intermediate code created by parse.c to
+ * build a model of the JVM operand stack for the current method.
+ *
+ * The following checks are performed:
+ *   - check for operand stack underflow (before each instruction)
+ *   - check for operand stack overflow (after[1] each instruction)
+ *   - check for matching stack depth at merging points
+ *   - check for matching basic types[2] at merging points
+ *   - check basic types for instruction input (except for BUILTIN*
+ *         opcodes, INVOKE* opcodes and MULTIANEWARRAY)
+ *
+ * [1]) XXX Checking this after the instruction should be ok. parse.c
+ * counts the number of required stack slots in such a way that it is
+ * only vital that we don't exceed `maxstack` at basic block
+ * boundaries.
+ *
+ * [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
+ * DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
+ * types are not discerned.
+ */
+
 void analyse_stack()
 {
        int b_count;
@@ -231,8 +330,8 @@ void analyse_stack()
                log_text(logtext);
        }
 
-       argren = DMNEW(int, maxlocals); 
-       //int *argren = (int *)alloca(maxlocals * sizeof(int)); /* table for argument renaming */
+       argren = DMNEW(int, maxlocals);
+       /*int *argren = (int *)alloca(maxlocals * sizeof(int));*/ /* table for argument renaming */
        for (i = 0; i < maxlocals; i++)
                argren[i] = i;
        
@@ -975,6 +1074,13 @@ void analyse_stack()
                                                /* pop 1 push 0 */
 
                                        case ICMD_POP:
+#ifdef TYPECHECK_STACK_COMPCAT
+                                               if (opt_verify) {
+                                                       REQUIRE_1;
+                                                       if (IS_2_WORD_TYPE(curstack->type))
+                                                               panic("Illegal instruction: POP on category 2 type");
+                                               }
+#endif
                                                OP1_0ANY;
                                                break;
 
@@ -1203,6 +1309,14 @@ void analyse_stack()
                                        case ICMD_POP2:
                                                REQUIRE_1;
                                                if (! IS_2_WORD_TYPE(curstack->type)) {
+                                                       /* ..., cat1 */
+#ifdef TYPECHECK_STACK_COMPCAT
+                                                       if (opt_verify) {
+                                                               REQUIRE_2;
+                                                               if (IS_2_WORD_TYPE(curstack->prev->type))
+                                                                       panic("Illegal instruction: POP2 on cat2, cat1 types");
+                                                       }
+#endif
                                                        OP1_0ANY;                /* second pop */
                                                }
                                                else
@@ -1213,6 +1327,13 @@ void analyse_stack()
                                                /* pop 0 push 1 dup */
                                                
                                        case ICMD_DUP:
+#ifdef TYPECHECK_STACK_COMPCAT
+                                               if (opt_verify) {
+                                                       REQUIRE_1;
+                                                       if (IS_2_WORD_TYPE(curstack->type))
+                                                               panic("Illegal instruction: DUP on category 2 type");
+                                               }
+#endif
                                                COUNT(count_dup_instruction);
                                                DUP;
                                                break;
@@ -1220,11 +1341,19 @@ void analyse_stack()
                                        case ICMD_DUP2:
                                                REQUIRE_1;
                                                if (IS_2_WORD_TYPE(curstack->type)) {
+                                                       /* ..., cat2 */
                                                        iptr->opc = ICMD_DUP;
                                                        DUP;
                                                }
                                                else {
                                                        REQUIRE_2;
+                                                       /* ..., ????, cat1 */
+#ifdef TYPECHECK_STACK_COMPCAT
+                                                       if (opt_verify) {
+                                                               if (IS_2_WORD_TYPE(curstack->prev->type))
+                                                                       panic("Illegal instruction: DUP2 on cat2, cat1 types");
+                                                       }
+#endif
                                                        copy = curstack;
                                                        NEWSTACK(copy->prev->type, copy->prev->varkind,
                                                                         copy->prev->varnum);
@@ -1238,16 +1367,40 @@ void analyse_stack()
                                                /* pop 2 push 3 dup */
                                                
                                        case ICMD_DUP_X1:
+#ifdef TYPECHECK_STACK_COMPCAT
+                                               if (opt_verify) {
+                                                       REQUIRE_2;
+                                                       if (IS_2_WORD_TYPE(curstack->type) ||
+                                                               IS_2_WORD_TYPE(curstack->prev->type))
+                                                               panic("Illegal instruction: DUP_X1 on cat 2 type");
+                                               }
+#endif
                                                DUP_X1;
                                                break;
 
                                        case ICMD_DUP2_X1:
                                                REQUIRE_2;
                                                if (IS_2_WORD_TYPE(curstack->type)) {
+                                                       /* ..., ????, cat2 */
+#ifdef TYPECHECK_STACK_COMPCAT
+                                                       if (opt_verify) {
+                                                               if (IS_2_WORD_TYPE(curstack->prev->type))
+                                                                       panic("Illegal instruction: DUP2_X1 on cat2, cat2 types");
+                                                       }
+#endif
                                                        iptr->opc = ICMD_DUP_X1;
                                                        DUP_X1;
                                                }
                                                else {
+                                                       /* ..., ????, cat1 */
+#ifdef TYPECHECK_STACK_COMPCAT
+                                                       if (opt_verify) {
+                                                               REQUIRE_3;
+                                                               if (IS_2_WORD_TYPE(curstack->prev->type)
+                                                                       || IS_2_WORD_TYPE(curstack->prev->prev->type))
+                                                                       panic("Illegal instruction: DUP2_X1 on invalid types");
+                                                       }
+#endif
                                                        DUP2_X1;
                                                }
                                                break;
@@ -1257,10 +1410,26 @@ void analyse_stack()
                                        case ICMD_DUP_X2:
                                                REQUIRE_2;
                                                if (IS_2_WORD_TYPE(curstack->prev->type)) {
+                                                       /* ..., cat2, ???? */
+#ifdef TYPECHECK_STACK_COMPCAT
+                                                       if (opt_verify) {
+                                                               if (IS_2_WORD_TYPE(curstack->type))
+                                                                       panic("Illegal instruction: DUP_X2 on cat2, cat2 types");
+                                                       }
+#endif
                                                        iptr->opc = ICMD_DUP_X1;
                                                        DUP_X1;
                                                }
                                                else {
+                                                       /* ..., cat1, ???? */
+#ifdef TYPECHECK_STACK_COMPCAT
+                                                       if (opt_verify) {
+                                                               REQUIRE_3;
+                                                               if (IS_2_WORD_TYPE(curstack->type)
+                                                                       || IS_2_WORD_TYPE(curstack->prev->prev->type))
+                                                                       panic("Illegal instruction: DUP_X2 on invalid types");
+                                                       }
+#endif
                                                        DUP_X2;
                                                }
                                                break;
@@ -1268,22 +1437,49 @@ void analyse_stack()
                                        case ICMD_DUP2_X2:
                                                REQUIRE_2;
                                                if (IS_2_WORD_TYPE(curstack->type)) {
+                                                       /* ..., ????, cat2 */
                                                        if (IS_2_WORD_TYPE(curstack->prev->type)) {
+                                                               /* ..., cat2, cat2 */
                                                                iptr->opc = ICMD_DUP_X1;
                                                                DUP_X1;
                                                        }
                                                        else {
+                                                               /* ..., cat1, cat2 */
+#ifdef TYPECHECK_STACK_COMPCAT
+                                                               if (opt_verify) {
+                                                                       REQUIRE_3;
+                                                                       if (IS_2_WORD_TYPE(curstack->prev->prev->type))
+                                                                               panic("Illegal instruction: DUP2_X2 on invalid types");
+                                                               }
+#endif
                                                                iptr->opc = ICMD_DUP_X2;
                                                                DUP_X2;
                                                        }
                                                }
                                                else {
                                                        REQUIRE_3;
+                                                       /* ..., ????, ????, cat1 */
                                                        if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
+                                                               /* ..., cat2, ????, cat1 */
+#ifdef TYPECHECK_STACK_COMPCAT
+                                                               if (opt_verify) {
+                                                                       if (IS_2_WORD_TYPE(curstack->prev->type))
+                                                                               panic("Illegal instruction: DUP2_X2 on invalid types");
+                                                               }
+#endif
                                                                iptr->opc = ICMD_DUP2_X1;
                                                                DUP2_X1;
                                                        }
                                                        else {
+                                                               /* ..., cat1, ????, cat1 */
+#ifdef TYPECHECK_STACK_COMPCAT
+                                                               if (opt_verify) {
+                                                                       REQUIRE_4;
+                                                                       if (IS_2_WORD_TYPE(curstack->prev->type)
+                                                                               || IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
+                                                                               panic("Illegal instruction: DUP2_X2 on invalid types");
+                                                               }
+#endif
                                                                DUP2_X2;
                                                        }
                                                }
@@ -1292,6 +1488,14 @@ void analyse_stack()
                                                /* pop 2 push 2 swap */
                                                
                                        case ICMD_SWAP:
+#ifdef TYPECHECK_STACK_COMPCAT
+                                               if (opt_verify) {
+                                                       REQUIRE_2;
+                                                       if (IS_2_WORD_TYPE(curstack->type)
+                                                               || IS_2_WORD_TYPE(curstack->prev->type))
+                                                               panic("Illegal instruction: SWAP on category 2 type");
+                                               }
+#endif
                                                SWAP;
                                                break;
 
@@ -1576,9 +1780,13 @@ void analyse_stack()
                                                 * needs it because the OP1_0ANY below
                                                 * overwrites iptr->dst.
                                                 */
-                                               iptr->val.a = (void*) iptr->dst;
+                                               iptr->val.a = (void *) iptr->dst;
 
-                                               tbptr->type=BBTYPE_SBR;
+                                               tbptr->type = BBTYPE_SBR;
+
+                                               /* We need to check for overflow right here because
+                                                * the pushed value is poped after MARKREACHED. */
+                                               CHECKOVERFLOW;
                                                MARKREACHED(tbptr, copy);
                                                OP1_0ANY;
                                                break;
@@ -1712,6 +1920,7 @@ void analyse_stack()
                                                        arguments_num = i + intreg_argnum;
                                                copy = curstack;
                                                while (--i >= 0) {
+                                                       /* XXX check INT type here? Currently typecheck does this. */
                                                        if (! (copy->flags & SAVEDVAR)) {
                                                                copy->varkind = ARGVAR;
                                                                copy->varnum = i + intreg_argnum;
@@ -1755,6 +1964,9 @@ void analyse_stack()
                                                printf("ICMD %d at %d\n", iptr->opc, (int)(iptr-instr));
                                                panic("Missing ICMD code during stack analysis");
                                        } /* switch */
+
+                                       CHECKOVERFLOW;
+                                       
                                        /* XXX DEBUG */ /*dolog("iptr++");*/
                                        iptr++;
                                } /* while instructions */
@@ -1844,6 +2056,10 @@ void analyse_stack()
 }
 
 
+/**********************************************************************/
+/* DEBUGGING HELPERS                                                  */
+/**********************************************************************/
+
 void icmd_print_stack(stackptr s)
 {
        int i, j;
@@ -1985,11 +2201,8 @@ static char *jit_type[] = {
 void show_icmd_method()
 {
        int i, j;
-       int deadcode;
-       s4  *s4ptr;
-       instruction *iptr;
+       s4  *s4ptr; /* used */
        basicblock *bptr;
-       void **tptr;
        xtable *ex;
 
        printf("\n");
@@ -2091,7 +2304,7 @@ void show_icmd_block(basicblock *bptr)
 {
        int i, j;
        int deadcode;
-       s4  *s4ptr;
+       s4  *s4ptr; /* used */
        instruction *iptr;
 
        if (bptr->flags != BBDELETED) {
@@ -2236,14 +2449,11 @@ void show_icmd(instruction *iptr,bool deadcode)
        case ICMD_PUTSTATIC:
        case ICMD_GETSTATIC:
                printf(" ");
-               utf_fprint(stdout,
-                                  ((fieldinfo *) iptr->val.a)->class->name);
+               utf_fprint(stdout, ((fieldinfo *) iptr->val.a)->class->name);
                printf(".");
-               utf_fprint(stdout,
-                                  ((fieldinfo *) iptr->val.a)->name);
+               utf_fprint(stdout, ((fieldinfo *) iptr->val.a)->name);
                printf(" (type ");
-               utf_fprint(stdout,
-                                  ((fieldinfo *) iptr->val.a)->descriptor);
+               utf_fprint(stdout, ((fieldinfo *) iptr->val.a)->descriptor);
                printf(")");
                break;