Fix compiler warnings.

Added GetReference, GetHandle to StackHandleScope to prevent the
compiler from optimizing away these loads/stores from inline
functions.

Change-Id: I4db02dd3194665d844292e74e3a7d7c80e730e06
diff --git a/runtime/class_linker-inl.h b/runtime/class_linker-inl.h
index 9d8888c..f745088 100644
--- a/runtime/class_linker-inl.h
+++ b/runtime/class_linker-inl.h
@@ -20,7 +20,7 @@
 #include "class_linker.h"
 #include "mirror/art_field.h"
 #include "mirror/class_loader.h"
-#include "mirror/dex_cache.h"
+#include "mirror/dex_cache-inl.h"
 #include "mirror/iftable.h"
 #include "mirror/object_array.h"
 #include "object_utils.h"
diff --git a/runtime/handle_scope-inl.h b/runtime/handle_scope-inl.h
index 634f2be..62c7614 100644
--- a/runtime/handle_scope-inl.h
+++ b/runtime/handle_scope-inl.h
@@ -28,8 +28,7 @@
 inline StackHandleScope<kNumReferences>::StackHandleScope(Thread* self)
     : HandleScope(kNumReferences), self_(self), pos_(0) {
   // TODO: Figure out how to use a compile assert.
-  DCHECK_EQ(OFFSETOF_MEMBER(HandleScope, references_),
-            OFFSETOF_MEMBER(StackHandleScope<1>, references_storage_));
+  DCHECK_EQ(&references_[0], &references_storage_[0]);
   for (size_t i = 0; i < kNumReferences; ++i) {
     SetReference(i, nullptr);
   }
diff --git a/runtime/handle_scope.h b/runtime/handle_scope.h
index 8ff7086..629e4ec 100644
--- a/runtime/handle_scope.h
+++ b/runtime/handle_scope.h
@@ -31,7 +31,7 @@
 
 // HandleScopes can be allocated within the bridge frame between managed and native code backed by
 // stack storage or manually allocated in native.
-class HandleScope {
+class PACKED(4) HandleScope {
  public:
   ~HandleScope() {}
 
@@ -46,7 +46,7 @@
 
   // Returns the size of a HandleScope containing num_references handles.
   static size_t SizeOf(uint32_t num_references) {
-    size_t header_size = OFFSETOF_MEMBER(HandleScope, references_);
+    size_t header_size = sizeof(HandleScope);
     size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
     return header_size + data_size;
   }
@@ -98,8 +98,8 @@
     // jni_compiler should have a jobject/jclass as a native method is
     // passed in a this pointer or a class
     DCHECK_GT(number_of_references_, 0U);
-    return ((&references_[0] <= handle_scope_entry)
-            && (handle_scope_entry <= (&references_[number_of_references_ - 1])));
+    return &references_[0] <= handle_scope_entry &&
+        handle_scope_entry <= &references_[number_of_references_ - 1];
   }
 
   // Offset of link within HandleScope, used by generated code
@@ -152,11 +152,32 @@
 
 // Scoped handle storage of a fixed size that is usually stack allocated.
 template<size_t kNumReferences>
-class StackHandleScope : public HandleScope {
+class PACKED(4) StackHandleScope : public HandleScope {
  public:
   explicit StackHandleScope(Thread* self);
   ~StackHandleScope();
 
+  // Currently unused, using this GetReference instead of the one in HandleScope is preferred to
+  // avoid compiler optimizations incorrectly optimizing out of bound array accesses.
+  // TODO: Remove this when it is un-necessary.
+  mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
+      ALWAYS_INLINE {
+    DCHECK_LT(i, number_of_references_);
+    return references_storage_[i].AsMirrorPtr();
+  }
+
+  Handle<mirror::Object> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
+      ALWAYS_INLINE {
+    DCHECK_LT(i, number_of_references_);
+    return Handle<mirror::Object>(&references_storage_[i]);
+  }
+
+  void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
+      ALWAYS_INLINE {
+    DCHECK_LT(i, number_of_references_);
+    references_storage_[i].Assign(object);
+  }
+
   template<class T>
   Handle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     SetReference(pos_, object);
diff --git a/runtime/mirror/dex_cache-inl.h b/runtime/mirror/dex_cache-inl.h
index f59c3a2..7e40f64 100644
--- a/runtime/mirror/dex_cache-inl.h
+++ b/runtime/mirror/dex_cache-inl.h
@@ -19,6 +19,8 @@
 
 #include "dex_cache.h"
 
+#include "runtime.h"
+
 namespace art {
 namespace mirror {