Fix CreateMultiArray to be compaction safe.

It used to be compaction safe before moving classes was enabled.
Added missing SIRTs.

Change-Id: I92963ed71fa6d2a20d16ec0e400b8fa0e41ac196
diff --git a/runtime/interpreter/interpreter.cc b/runtime/interpreter/interpreter.cc
index 02a9aa6..922e642 100644
--- a/runtime/interpreter/interpreter.cc
+++ b/runtime/interpreter/interpreter.cc
@@ -57,7 +57,10 @@
   } else if (name == "int java.lang.String.fastIndexOf(int, int)") {
     result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
   } else if (name == "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])") {
-    result->SetL(Array::CreateMultiArray(self, reinterpret_cast<Object*>(args[0])->AsClass(), reinterpret_cast<Object*>(args[1])->AsIntArray()));
+    SirtRef<mirror::Class> sirt_class(self, reinterpret_cast<Object*>(args[0])->AsClass());
+    SirtRef<mirror::IntArray> sirt_dimensions(self,
+                                              reinterpret_cast<Object*>(args[1])->AsIntArray());
+    result->SetL(Array::CreateMultiArray(self, sirt_class, sirt_dimensions));
   } else if (name == "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()") {
     ScopedObjectAccessUnchecked soa(self);
     result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace(soa)));
diff --git a/runtime/mirror/array.cc b/runtime/mirror/array.cc
index ca0d1f3..c23234e 100644
--- a/runtime/mirror/array.cc
+++ b/runtime/mirror/array.cc
@@ -40,23 +40,25 @@
 // piece and work our way in.
 // Recursively create an array with multiple dimensions.  Elements may be
 // Objects or primitive types.
-static Array* RecursiveCreateMultiArray(Thread* self, Class* array_class, int current_dimension,
-                                        SirtRef<mirror::IntArray>& dimensions)
+static Array* RecursiveCreateMultiArray(Thread* self,
+                                        const SirtRef<Class>& array_class, int current_dimension,
+                                        const SirtRef<mirror::IntArray>& dimensions)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
   int32_t array_length = dimensions->Get(current_dimension);
-  SirtRef<Array> new_array(self, Array::Alloc<true>(self, array_class, array_length));
-  if (UNLIKELY(new_array.get() == NULL)) {
+  SirtRef<Array> new_array(self, Array::Alloc<true>(self, array_class.get(), array_length));
+  if (UNLIKELY(new_array.get() == nullptr)) {
     CHECK(self->IsExceptionPending());
-    return NULL;
+    return nullptr;
   }
   if (current_dimension + 1 < dimensions->GetLength()) {
     // Create a new sub-array in every element of the array.
     for (int32_t i = 0; i < array_length; i++) {
-      Array* sub_array = RecursiveCreateMultiArray(self, array_class->GetComponentType(),
+      SirtRef<mirror::Class> sirt_component_type(self, array_class->GetComponentType());
+      Array* sub_array = RecursiveCreateMultiArray(self, sirt_component_type,
                                                    current_dimension + 1, dimensions);
-      if (UNLIKELY(sub_array == NULL)) {
+      if (UNLIKELY(sub_array == nullptr)) {
         CHECK(self->IsExceptionPending());
-        return NULL;
+        return nullptr;
       }
       new_array->AsObjectArray<Array>()->Set(i, sub_array);
     }
@@ -64,7 +66,8 @@
   return new_array.get();
 }
 
-Array* Array::CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions) {
+Array* Array::CreateMultiArray(Thread* self, const SirtRef<Class>& element_class,
+                               const SirtRef<IntArray>& dimensions) {
   // Verify dimensions.
   //
   // The caller is responsible for verifying that "dimArray" is non-null
@@ -77,28 +80,27 @@
     int dimension = dimensions->Get(i);
     if (UNLIKELY(dimension < 0)) {
       ThrowNegativeArraySizeException(StringPrintf("Dimension %d: %d", i, dimension).c_str());
-      return NULL;
+      return nullptr;
     }
   }
 
   // Generate the full name of the array class.
   std::string descriptor(num_dimensions, '[');
-  descriptor += ClassHelper(element_class).GetDescriptor();
+  descriptor += ClassHelper(element_class.get()).GetDescriptor();
 
   // Find/generate the array class.
   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
   SirtRef<mirror::ClassLoader> class_loader(self, element_class->GetClassLoader());
-  Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
-  if (UNLIKELY(array_class == NULL)) {
+  SirtRef<mirror::Class> array_class(self,
+                                     class_linker->FindClass(descriptor.c_str(), class_loader));
+  if (UNLIKELY(array_class.get() == nullptr)) {
     CHECK(self->IsExceptionPending());
-    return NULL;
+    return nullptr;
   }
   // create the array
-  SirtRef<mirror::IntArray> sirt_dimensions(self, dimensions);
-  Array* new_array = RecursiveCreateMultiArray(self, array_class, 0, sirt_dimensions);
-  if (UNLIKELY(new_array == NULL)) {
+  Array* new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions);
+  if (UNLIKELY(new_array == nullptr)) {
     CHECK(self->IsExceptionPending());
-    return NULL;
   }
   return new_array;
 }
diff --git a/runtime/mirror/array.h b/runtime/mirror/array.h
index 6e366a0..04f03c3 100644
--- a/runtime/mirror/array.h
+++ b/runtime/mirror/array.h
@@ -47,7 +47,8 @@
                       size_t component_size)
       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
 
-  static Array* CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions)
+  static Array* CreateMultiArray(Thread* self, const SirtRef<Class>& element_class,
+                                 const SirtRef<IntArray>& dimensions)
       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
 
   size_t SizeOf() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
diff --git a/runtime/mirror/object_test.cc b/runtime/mirror/object_test.cc
index 2af32da..db9723b 100644
--- a/runtime/mirror/object_test.cc
+++ b/runtime/mirror/object_test.cc
@@ -236,12 +236,12 @@
   SirtRef<Class> c(soa.Self(), class_linker_->FindSystemClass("I"));
   SirtRef<IntArray> dims(soa.Self(), IntArray::Alloc(soa.Self(), 1));
   dims->Set(0, 1);
-  Array* multi = Array::CreateMultiArray(soa.Self(), c.get(), dims.get());
+  Array* multi = Array::CreateMultiArray(soa.Self(), c, dims);
   EXPECT_TRUE(multi->GetClass() == class_linker_->FindSystemClass("[I"));
   EXPECT_EQ(1, multi->GetLength());
 
   dims->Set(0, -1);
-  multi = Array::CreateMultiArray(soa.Self(), c.get(), dims.get());
+  multi = Array::CreateMultiArray(soa.Self(), c, dims);
   EXPECT_TRUE(soa.Self()->IsExceptionPending());
   EXPECT_EQ(PrettyDescriptor(soa.Self()->GetException(NULL)->GetClass()),
             "java.lang.NegativeArraySizeException");
@@ -252,7 +252,7 @@
     for (int j = 0; j < 20; ++j) {
       dims->Set(0, i);
       dims->Set(1, j);
-      multi = Array::CreateMultiArray(soa.Self(), c.get(), dims.get());
+      multi = Array::CreateMultiArray(soa.Self(), c, dims);
       EXPECT_TRUE(multi->GetClass() == class_linker_->FindSystemClass("[[I"));
       EXPECT_EQ(i, multi->GetLength());
       for (int k = 0; k < i; ++k) {
diff --git a/runtime/native/java_lang_reflect_Array.cc b/runtime/native/java_lang_reflect_Array.cc
index 52cdb59..2197597 100644
--- a/runtime/native/java_lang_reflect_Array.cc
+++ b/runtime/native/java_lang_reflect_Array.cc
@@ -29,14 +29,16 @@
 static jobject Array_createMultiArray(JNIEnv* env, jclass, jclass javaElementClass, jobject javaDimArray) {
   ScopedFastNativeObjectAccess soa(env);
   DCHECK(javaElementClass != NULL);
-  mirror::Class* element_class = soa.Decode<mirror::Class*>(javaElementClass);
+  SirtRef<mirror::Class> element_class(soa.Self(), soa.Decode<mirror::Class*>(javaElementClass));
   DCHECK(element_class->IsClass());
   DCHECK(javaDimArray != NULL);
   mirror::Object* dimensions_obj = soa.Decode<mirror::Object*>(javaDimArray);
   DCHECK(dimensions_obj->IsArrayInstance());
   DCHECK_STREQ(ClassHelper(dimensions_obj->GetClass()).GetDescriptor(), "[I");
-  mirror::IntArray* dimensions_array = down_cast<mirror::IntArray*>(dimensions_obj);
-  mirror::Array* new_array = mirror::Array::CreateMultiArray(soa.Self(), element_class, dimensions_array);
+  SirtRef<mirror::IntArray> dimensions_array(soa.Self(),
+                                             down_cast<mirror::IntArray*>(dimensions_obj));
+  mirror::Array* new_array = mirror::Array::CreateMultiArray(soa.Self(), element_class,
+                                                             dimensions_array);
   return soa.AddLocalReference<jobject>(new_array);
 }