Merge "More exception cleanup." into dalvik-dev
diff --git a/vm/alloc/MarkSweep.c b/vm/alloc/MarkSweep.c
index 1fb3f24..ced52be 100644
--- a/vm/alloc/MarkSweep.c
+++ b/vm/alloc/MarkSweep.c
@@ -26,23 +26,13 @@
 #include <sys/mman.h>   // for madvise(), mmap()
 #include <errno.h>
 
-#define GC_LOG_TAG      LOG_TAG "-gc"
-
-#if LOG_NDEBUG
-#define LOGD_GC(...)    ((void)0)
-#else
-#define LOGD_GC(...)    LOG(LOG_DEBUG, GC_LOG_TAG, __VA_ARGS__)
-#endif
-
-#define LOGE_GC(...)    LOG(LOG_ERROR, GC_LOG_TAG, __VA_ARGS__)
-
 typedef unsigned long Word;
 const size_t kWordSize = sizeof(Word);
 
-/* Do not cast the result of this to a boolean; the only set bit
- * may be > 1<<8.
+/*
+ * Returns true if the given object is marked.
  */
-static long isMarked(const void *obj, const GcMarkContext *ctx)
+static bool isMarked(const Object *obj, const GcMarkContext *ctx)
 {
     return dvmHeapBitmapIsObjectBitSet(ctx->bitmap, obj);
 }
@@ -478,6 +468,7 @@
 {
     assert(obj != NULL);
     assert(ctx != NULL);
+    assert(isMarked(obj, ctx));
     assert(obj->clazz != NULL);
     if (obj->clazz == gDvm.classJavaLangClass) {
         scanClassObject(obj, ctx);
@@ -796,8 +787,7 @@
         //      we can schedule them next time.  Watch out,
         //      because we may be expecting to free up space
         //      by calling finalizers.
-        LOGE_GC("scheduleFinalizations(): no room for "
-                "pending finalizations");
+        LOGE("scheduleFinalizations(): no room for pending finalizations");
         dvmAbort();
     }
 
@@ -816,9 +806,9 @@
                 if (!dvmAddToReferenceTable(&newPendingRefs, *ref)) {
                     //TODO: add the current table and allocate
                     //      a new, smaller one.
-                    LOGE_GC("scheduleFinalizations(): "
-                            "no room for any more pending finalizations: %zd",
-                            dvmReferenceTableEntries(&newPendingRefs));
+                    LOGE("scheduleFinalizations(): "
+                         "no room for any more pending finalizations: %zd",
+                         dvmReferenceTableEntries(&newPendingRefs));
                     dvmAbort();
                 }
                 newPendCount++;
@@ -842,8 +832,7 @@
         totalPendCount += newPendCount;
         finRefs = finRefs->next;
     }
-    LOGD_GC("scheduleFinalizations(): %zd finalizers triggered.",
-            totalPendCount);
+    LOGD("scheduleFinalizations(): %zd finalizers triggered.", totalPendCount);
     if (totalPendCount == 0) {
         /* No objects required finalization.
          * Free the empty temporary table.
@@ -857,8 +846,7 @@
     if (!dvmHeapAddTableToLargeTable(&gDvm.gcHeap->pendingFinalizationRefs,
                 &newPendingRefs))
     {
-        LOGE_GC("scheduleFinalizations(): can't insert new "
-                "pending finalizations");
+        LOGE("scheduleFinalizations(): can't insert new pending finalizations");
         dvmAbort();
     }
 
@@ -985,10 +973,9 @@
  * Returns true if the given object is unmarked.  This assumes that
  * the bitmaps have not yet been swapped.
  */
-static int isUnmarkedObject(void *object)
+static int isUnmarkedObject(void *obj)
 {
-    return !isMarked((void *)((uintptr_t)object & ~(HB_OBJECT_ALIGNMENT-1)),
-            &gDvm.gcHeap->markContext);
+    return !isMarked((Object *)obj, &gDvm.gcHeap->markContext);
 }
 
 /*
diff --git a/vm/mterp/armv5te/OP_NEW_ARRAY.S b/vm/mterp/armv5te/OP_NEW_ARRAY.S
index a9c4e93..eca1ac6 100644
--- a/vm/mterp/armv5te/OP_NEW_ARRAY.S
+++ b/vm/mterp/armv5te/OP_NEW_ARRAY.S
@@ -16,7 +16,7 @@
     ldr     r3, [r3, #offDvmDex_pResClasses]    @ r3<- pDvmDex->pResClasses
     cmp     r1, #0                      @ check length
     ldr     r0, [r3, r2, lsl #2]        @ r0<- resolved class
-    bmi     common_errNegativeArraySize @ negative length, bail
+    bmi     common_errNegativeArraySize @ negative length, bail - len in r1
     cmp     r0, #0                      @ already resolved?
     EXPORT_PC()                         @ req'd for resolve, alloc
     bne     .L${opcode}_finish          @ resolved, continue
diff --git a/vm/mterp/armv5te/OP_NEW_ARRAY_JUMBO.S b/vm/mterp/armv5te/OP_NEW_ARRAY_JUMBO.S
index e0e10ae..568afb4 100644
--- a/vm/mterp/armv5te/OP_NEW_ARRAY_JUMBO.S
+++ b/vm/mterp/armv5te/OP_NEW_ARRAY_JUMBO.S
@@ -18,7 +18,7 @@
     ldr     r3, [r3, #offDvmDex_pResClasses]    @ r3<- pDvmDex->pResClasses
     cmp     r1, #0                      @ check length
     ldr     r0, [r3, r2, lsl #2]        @ r0<- resolved class
-    bmi     common_errNegativeArraySize @ negative length, bail
+    bmi     common_errNegativeArraySize @ negative length, bail - len in r1
     cmp     r0, #0                      @ already resolved?
     EXPORT_PC()                         @ req'd for resolve, alloc
     bne     .L${opcode}_finish          @ resolved, continue
diff --git a/vm/mterp/armv5te/footer.S b/vm/mterp/armv5te/footer.S
index 300c45c..00026c2 100644
--- a/vm/mterp/armv5te/footer.S
+++ b/vm/mterp/armv5te/footer.S
@@ -1013,12 +1013,12 @@
 
 /*
  * Attempt to allocate an array with a negative size.
+ * On entry: length in r1
  */
 common_errNegativeArraySize:
     EXPORT_PC()
-    ldr     r0, strNegativeArraySizeException
-    mov     r1, #0
-    bl      dvmThrowException
+    mov     r0, r1                                @ arg0 <- len
+    bl      dvmThrowNegativeArraySizeException    @ (len)
     b       common_exceptionThrown
 
 /*
@@ -1180,8 +1180,6 @@
     .word   .LstrArithmeticException
 strDivideByZero:
     .word   .LstrDivideByZero
-strNegativeArraySizeException:
-    .word   .LstrNegativeArraySizeException
 strNoSuchMethodError:
     .word   .LstrNoSuchMethodError
 strNullPointerException:
@@ -1224,8 +1222,6 @@
     .asciz  "Ljava/lang/InternalError;"
 .LstrInstantiationError:
     .asciz  "Ljava/lang/InstantiationError;"
-.LstrNegativeArraySizeException:
-    .asciz  "Ljava/lang/NegativeArraySizeException;"
 .LstrNoSuchMethodError:
     .asciz  "Ljava/lang/NoSuchMethodError;"
 .LstrNullPointerException:
diff --git a/vm/mterp/out/InterpAsm-armv5te-vfp.S b/vm/mterp/out/InterpAsm-armv5te-vfp.S
index 9f144cf..a0f54e8 100644
--- a/vm/mterp/out/InterpAsm-armv5te-vfp.S
+++ b/vm/mterp/out/InterpAsm-armv5te-vfp.S
@@ -1001,7 +1001,7 @@
     ldr     r3, [r3, #offDvmDex_pResClasses]    @ r3<- pDvmDex->pResClasses
     cmp     r1, #0                      @ check length
     ldr     r0, [r3, r2, lsl #2]        @ r0<- resolved class
-    bmi     common_errNegativeArraySize @ negative length, bail
+    bmi     common_errNegativeArraySize @ negative length, bail - len in r1
     cmp     r0, #0                      @ already resolved?
     EXPORT_PC()                         @ req'd for resolve, alloc
     bne     .LOP_NEW_ARRAY_finish          @ resolved, continue
@@ -7874,7 +7874,7 @@
     ldr     r3, [r3, #offDvmDex_pResClasses]    @ r3<- pDvmDex->pResClasses
     cmp     r1, #0                      @ check length
     ldr     r0, [r3, r2, lsl #2]        @ r0<- resolved class
-    bmi     common_errNegativeArraySize @ negative length, bail
+    bmi     common_errNegativeArraySize @ negative length, bail - len in r1
     cmp     r0, #0                      @ already resolved?
     EXPORT_PC()                         @ req'd for resolve, alloc
     bne     .LOP_NEW_ARRAY_JUMBO_finish          @ resolved, continue
@@ -14173,12 +14173,12 @@
 
 /*
  * Attempt to allocate an array with a negative size.
+ * On entry: length in r1
  */
 common_errNegativeArraySize:
     EXPORT_PC()
-    ldr     r0, strNegativeArraySizeException
-    mov     r1, #0
-    bl      dvmThrowException
+    mov     r0, r1                                @ arg0 <- len
+    bl      dvmThrowNegativeArraySizeException    @ (len)
     b       common_exceptionThrown
 
 /*
@@ -14340,8 +14340,6 @@
     .word   .LstrArithmeticException
 strDivideByZero:
     .word   .LstrDivideByZero
-strNegativeArraySizeException:
-    .word   .LstrNegativeArraySizeException
 strNoSuchMethodError:
     .word   .LstrNoSuchMethodError
 strNullPointerException:
@@ -14384,8 +14382,6 @@
     .asciz  "Ljava/lang/InternalError;"
 .LstrInstantiationError:
     .asciz  "Ljava/lang/InstantiationError;"
-.LstrNegativeArraySizeException:
-    .asciz  "Ljava/lang/NegativeArraySizeException;"
 .LstrNoSuchMethodError:
     .asciz  "Ljava/lang/NoSuchMethodError;"
 .LstrNullPointerException:
diff --git a/vm/mterp/out/InterpAsm-armv5te.S b/vm/mterp/out/InterpAsm-armv5te.S
index 783a1de..18f19be 100644
--- a/vm/mterp/out/InterpAsm-armv5te.S
+++ b/vm/mterp/out/InterpAsm-armv5te.S
@@ -1001,7 +1001,7 @@
     ldr     r3, [r3, #offDvmDex_pResClasses]    @ r3<- pDvmDex->pResClasses
     cmp     r1, #0                      @ check length
     ldr     r0, [r3, r2, lsl #2]        @ r0<- resolved class
-    bmi     common_errNegativeArraySize @ negative length, bail
+    bmi     common_errNegativeArraySize @ negative length, bail - len in r1
     cmp     r0, #0                      @ already resolved?
     EXPORT_PC()                         @ req'd for resolve, alloc
     bne     .LOP_NEW_ARRAY_finish          @ resolved, continue
@@ -8196,7 +8196,7 @@
     ldr     r3, [r3, #offDvmDex_pResClasses]    @ r3<- pDvmDex->pResClasses
     cmp     r1, #0                      @ check length
     ldr     r0, [r3, r2, lsl #2]        @ r0<- resolved class
-    bmi     common_errNegativeArraySize @ negative length, bail
+    bmi     common_errNegativeArraySize @ negative length, bail - len in r1
     cmp     r0, #0                      @ already resolved?
     EXPORT_PC()                         @ req'd for resolve, alloc
     bne     .LOP_NEW_ARRAY_JUMBO_finish          @ resolved, continue
@@ -14631,12 +14631,12 @@
 
 /*
  * Attempt to allocate an array with a negative size.
+ * On entry: length in r1
  */
 common_errNegativeArraySize:
     EXPORT_PC()
-    ldr     r0, strNegativeArraySizeException
-    mov     r1, #0
-    bl      dvmThrowException
+    mov     r0, r1                                @ arg0 <- len
+    bl      dvmThrowNegativeArraySizeException    @ (len)
     b       common_exceptionThrown
 
 /*
@@ -14798,8 +14798,6 @@
     .word   .LstrArithmeticException
 strDivideByZero:
     .word   .LstrDivideByZero
-strNegativeArraySizeException:
-    .word   .LstrNegativeArraySizeException
 strNoSuchMethodError:
     .word   .LstrNoSuchMethodError
 strNullPointerException:
@@ -14842,8 +14840,6 @@
     .asciz  "Ljava/lang/InternalError;"
 .LstrInstantiationError:
     .asciz  "Ljava/lang/InstantiationError;"
-.LstrNegativeArraySizeException:
-    .asciz  "Ljava/lang/NegativeArraySizeException;"
 .LstrNoSuchMethodError:
     .asciz  "Ljava/lang/NoSuchMethodError;"
 .LstrNullPointerException:
diff --git a/vm/mterp/out/InterpAsm-armv7-a-neon.S b/vm/mterp/out/InterpAsm-armv7-a-neon.S
index 6fe0282..0a9a41a 100644
--- a/vm/mterp/out/InterpAsm-armv7-a-neon.S
+++ b/vm/mterp/out/InterpAsm-armv7-a-neon.S
@@ -1011,7 +1011,7 @@
     ldr     r3, [r3, #offDvmDex_pResClasses]    @ r3<- pDvmDex->pResClasses
     cmp     r1, #0                      @ check length
     ldr     r0, [r3, r2, lsl #2]        @ r0<- resolved class
-    bmi     common_errNegativeArraySize @ negative length, bail
+    bmi     common_errNegativeArraySize @ negative length, bail - len in r1
     cmp     r0, #0                      @ already resolved?
     EXPORT_PC()                         @ req'd for resolve, alloc
     bne     .LOP_NEW_ARRAY_finish          @ resolved, continue
@@ -7828,7 +7828,7 @@
     ldr     r3, [r3, #offDvmDex_pResClasses]    @ r3<- pDvmDex->pResClasses
     cmp     r1, #0                      @ check length
     ldr     r0, [r3, r2, lsl #2]        @ r0<- resolved class
-    bmi     common_errNegativeArraySize @ negative length, bail
+    bmi     common_errNegativeArraySize @ negative length, bail - len in r1
     cmp     r0, #0                      @ already resolved?
     EXPORT_PC()                         @ req'd for resolve, alloc
     bne     .LOP_NEW_ARRAY_JUMBO_finish          @ resolved, continue
@@ -14111,12 +14111,12 @@
 
 /*
  * Attempt to allocate an array with a negative size.
+ * On entry: length in r1
  */
 common_errNegativeArraySize:
     EXPORT_PC()
-    ldr     r0, strNegativeArraySizeException
-    mov     r1, #0
-    bl      dvmThrowException
+    mov     r0, r1                                @ arg0 <- len
+    bl      dvmThrowNegativeArraySizeException    @ (len)
     b       common_exceptionThrown
 
 /*
@@ -14278,8 +14278,6 @@
     .word   .LstrArithmeticException
 strDivideByZero:
     .word   .LstrDivideByZero
-strNegativeArraySizeException:
-    .word   .LstrNegativeArraySizeException
 strNoSuchMethodError:
     .word   .LstrNoSuchMethodError
 strNullPointerException:
@@ -14322,8 +14320,6 @@
     .asciz  "Ljava/lang/InternalError;"
 .LstrInstantiationError:
     .asciz  "Ljava/lang/InstantiationError;"
-.LstrNegativeArraySizeException:
-    .asciz  "Ljava/lang/NegativeArraySizeException;"
 .LstrNoSuchMethodError:
     .asciz  "Ljava/lang/NoSuchMethodError;"
 .LstrNullPointerException:
diff --git a/vm/mterp/out/InterpAsm-armv7-a.S b/vm/mterp/out/InterpAsm-armv7-a.S
index dd7171e..c2e501e 100644
--- a/vm/mterp/out/InterpAsm-armv7-a.S
+++ b/vm/mterp/out/InterpAsm-armv7-a.S
@@ -1011,7 +1011,7 @@
     ldr     r3, [r3, #offDvmDex_pResClasses]    @ r3<- pDvmDex->pResClasses
     cmp     r1, #0                      @ check length
     ldr     r0, [r3, r2, lsl #2]        @ r0<- resolved class
-    bmi     common_errNegativeArraySize @ negative length, bail
+    bmi     common_errNegativeArraySize @ negative length, bail - len in r1
     cmp     r0, #0                      @ already resolved?
     EXPORT_PC()                         @ req'd for resolve, alloc
     bne     .LOP_NEW_ARRAY_finish          @ resolved, continue
@@ -7828,7 +7828,7 @@
     ldr     r3, [r3, #offDvmDex_pResClasses]    @ r3<- pDvmDex->pResClasses
     cmp     r1, #0                      @ check length
     ldr     r0, [r3, r2, lsl #2]        @ r0<- resolved class
-    bmi     common_errNegativeArraySize @ negative length, bail
+    bmi     common_errNegativeArraySize @ negative length, bail - len in r1
     cmp     r0, #0                      @ already resolved?
     EXPORT_PC()                         @ req'd for resolve, alloc
     bne     .LOP_NEW_ARRAY_JUMBO_finish          @ resolved, continue
@@ -14111,12 +14111,12 @@
 
 /*
  * Attempt to allocate an array with a negative size.
+ * On entry: length in r1
  */
 common_errNegativeArraySize:
     EXPORT_PC()
-    ldr     r0, strNegativeArraySizeException
-    mov     r1, #0
-    bl      dvmThrowException
+    mov     r0, r1                                @ arg0 <- len
+    bl      dvmThrowNegativeArraySizeException    @ (len)
     b       common_exceptionThrown
 
 /*
@@ -14278,8 +14278,6 @@
     .word   .LstrArithmeticException
 strDivideByZero:
     .word   .LstrDivideByZero
-strNegativeArraySizeException:
-    .word   .LstrNegativeArraySizeException
 strNoSuchMethodError:
     .word   .LstrNoSuchMethodError
 strNullPointerException:
@@ -14322,8 +14320,6 @@
     .asciz  "Ljava/lang/InternalError;"
 .LstrInstantiationError:
     .asciz  "Ljava/lang/InstantiationError;"
-.LstrNegativeArraySizeException:
-    .asciz  "Ljava/lang/NegativeArraySizeException;"
 .LstrNoSuchMethodError:
     .asciz  "Ljava/lang/NoSuchMethodError;"
 .LstrNullPointerException:
diff --git a/vm/mterp/out/InterpAsm-x86.S b/vm/mterp/out/InterpAsm-x86.S
index 9a8f588..f4e8733 100644
--- a/vm/mterp/out/InterpAsm-x86.S
+++ b/vm/mterp/out/InterpAsm-x86.S
@@ -1201,7 +1201,7 @@
     GET_VREG_R %eax %eax                      # eax<- vB (array length)
     andb    $0xf,rINSTbl                     # rINST<- A
     testl   %eax,%eax
-    js      common_errNegativeArraySize       # bail
+    js      common_errNegativeArraySize       # bail, passing len in eax
     testl   %ecx,%ecx                         # already resolved?
     jne     .LOP_NEW_ARRAY_finish                # yes, fast path
     jmp     .LOP_NEW_ARRAY_resolve               # resolve now
@@ -6567,7 +6567,7 @@
     movzwl  8(rPC),%eax                       # eax<- CCCC
     GET_VREG_R %eax %eax                      # eax<- vCCCC (array length)
     testl   %eax,%eax
-    js      common_errNegativeArraySize       # bail
+    js      common_errNegativeArraySize       # bail, passing len in eax
     testl   %ecx,%ecx                         # already resolved?
     jne     .LOP_NEW_ARRAY_JUMBO_finish                # yes, fast path
     jmp     .LOP_NEW_ARRAY_JUMBO_resolve               # resolve now
@@ -13804,14 +13804,12 @@
 
 /*
  * Attempt to allocate an array with a negative size.
+ * On entry, len in eax
  */
 common_errNegativeArraySize:
     EXPORT_PC
-    movl    $.LstrNegativeArraySizeException,%eax
-    movl    %eax,OUT_ARG0(%esp)
-    xorl    %eax,%eax
-    movl    %eax,OUT_ARG1(%esp)
-    call    dvmThrowException
+    movl    %eax,OUT_ARG0(%esp)                  # arg0<- len
+    call    dvmThrowNegativeArraySizeException   # (len)
     jmp     common_exceptionThrown
 
 /*
@@ -13887,8 +13885,6 @@
     .asciz  "Ljava/lang/ArithmeticException;"
 .LstrDivideByZero:
     .asciz  "divide by zero"
-.LstrNegativeArraySizeException:
-    .asciz  "Ljava/lang/NegativeArraySizeException;"
 .LstrInstantiationError:
     .asciz  "Ljava/lang/InstantiationError;"
 .LstrNoSuchMethodError:
diff --git a/vm/mterp/x86-atom/TODO.txt b/vm/mterp/x86-atom/TODO.txt
index 533894c..5ff0083 100644
--- a/vm/mterp/x86-atom/TODO.txt
+++ b/vm/mterp/x86-atom/TODO.txt
@@ -21,6 +21,7 @@
      array bounds errors.
 (md) Use dvmThrowClassCastException(actual, desired) for class cast errors.
 (md) Use dvmThrowArrayStoreException(actual, desired) for array store errors.
+(md) Use dvmThrowNegativeArraySizeException(len) forarray alloc errors
 
 (lo) Implement OP_BREAKPOINT
 (lo) Implement OP_EXECUTE_INLINE_RANGE
diff --git a/vm/mterp/x86/OP_NEW_ARRAY.S b/vm/mterp/x86/OP_NEW_ARRAY.S
index 35a14f7..ec3b3a9 100644
--- a/vm/mterp/x86/OP_NEW_ARRAY.S
+++ b/vm/mterp/x86/OP_NEW_ARRAY.S
@@ -20,7 +20,7 @@
     GET_VREG_R %eax %eax                      # eax<- vB (array length)
     andb    $$0xf,rINSTbl                     # rINST<- A
     testl   %eax,%eax
-    js      common_errNegativeArraySize       # bail
+    js      common_errNegativeArraySize       # bail, passing len in eax
     testl   %ecx,%ecx                         # already resolved?
     jne     .L${opcode}_finish                # yes, fast path
     jmp     .L${opcode}_resolve               # resolve now
diff --git a/vm/mterp/x86/OP_NEW_ARRAY_JUMBO.S b/vm/mterp/x86/OP_NEW_ARRAY_JUMBO.S
index b0a2ccdd..463738e 100644
--- a/vm/mterp/x86/OP_NEW_ARRAY_JUMBO.S
+++ b/vm/mterp/x86/OP_NEW_ARRAY_JUMBO.S
@@ -18,7 +18,7 @@
     movzwl  8(rPC),%eax                       # eax<- CCCC
     GET_VREG_R %eax %eax                      # eax<- vCCCC (array length)
     testl   %eax,%eax
-    js      common_errNegativeArraySize       # bail
+    js      common_errNegativeArraySize       # bail, passing len in eax
     testl   %ecx,%ecx                         # already resolved?
     jne     .L${opcode}_finish                # yes, fast path
     jmp     .L${opcode}_resolve               # resolve now
diff --git a/vm/mterp/x86/footer.S b/vm/mterp/x86/footer.S
index e1e8404..721b395 100644
--- a/vm/mterp/x86/footer.S
+++ b/vm/mterp/x86/footer.S
@@ -569,14 +569,12 @@
 
 /*
  * Attempt to allocate an array with a negative size.
+ * On entry, len in eax
  */
 common_errNegativeArraySize:
     EXPORT_PC
-    movl    $$.LstrNegativeArraySizeException,%eax
-    movl    %eax,OUT_ARG0(%esp)
-    xorl    %eax,%eax
-    movl    %eax,OUT_ARG1(%esp)
-    call    dvmThrowException
+    movl    %eax,OUT_ARG0(%esp)                  # arg0<- len
+    call    dvmThrowNegativeArraySizeException   # (len)
     jmp     common_exceptionThrown
 
 /*
@@ -652,8 +650,6 @@
     .asciz  "Ljava/lang/ArithmeticException;"
 .LstrDivideByZero:
     .asciz  "divide by zero"
-.LstrNegativeArraySizeException:
-    .asciz  "Ljava/lang/NegativeArraySizeException;"
 .LstrInstantiationError:
     .asciz  "Ljava/lang/InstantiationError;"
 .LstrNoSuchMethodError: