Support for JNI local reference cookie.

This also fixes a cross compilation bug in reseting the top of the
indirect reference table following a down call.

Change-Id: I40d913a6f86dadfe87b58d6d13a1ff3613f270ac
diff --git a/src/jni_internal.cc b/src/jni_internal.cc
index 0436500..7adbaeb 100644
--- a/src/jni_internal.cc
+++ b/src/jni_internal.cc
@@ -68,7 +68,7 @@
   JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
   IndirectReferenceTable& locals = env->locals;
 
-  uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
+  uint32_t cookie = env->local_ref_cookie;
   IndirectRef ref = locals.Add(cookie, obj);
   if (ref == NULL) {
     // TODO: just change Add's DCHECK to CHECK and lose this?
@@ -873,7 +873,7 @@
 
     IndirectReferenceTable& locals = ts.Env()->locals;
 
-    uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
+    uint32_t cookie = ts.Env()->local_ref_cookie;
     IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
     return reinterpret_cast<jobject>(ref);
   }
@@ -886,7 +886,7 @@
 
     IndirectReferenceTable& locals = ts.Env()->locals;
 
-    uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
+    uint32_t cookie = ts.Env()->local_ref_cookie;
     if (!locals.Remove(cookie, obj)) {
       // Attempting to delete a local reference that is not in the
       // topmost local reference frame is a no-op.  DeleteLocalRef returns
@@ -2508,15 +2508,20 @@
 JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
     : self(self),
       vm(vm),
+      local_ref_cookie(IRT_FIRST_SEGMENT),
+      locals(kLocalsInitial, kLocalsMax, kLocal),
       check_jni(vm->check_jni),
       work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
       critical(false),
-      monitors("monitors", kMonitorsInitial, kMonitorsMax),
-      locals(kLocalsInitial, kLocalsMax, kLocal) {
+      monitors("monitors", kMonitorsInitial, kMonitorsMax) {
   functions = unchecked_functions = &gNativeInterface;
   if (check_jni) {
     functions = GetCheckJniNativeInterface();
   }
+  // The JniEnv local reference values must be at a consistent offset or else cross-compilation
+  // errors will ensue.
+  CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
+  CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
 }
 
 JNIEnvExt::~JNIEnvExt() {