Refactor 904 for safety

Some functions that could potentially cause allocations were being
performed during the ObjectAllocated callback in test 904. If these
allocations occurred the test could enter an infinite loop until it
stack-overflows in native code.

To prevent this we move all potentially allocation causing code (such
as calling java methods and getting jmethodIDs) out to the
getTrackingEventMessages function.

Test: ./test.py --host
Change-Id: Iccd2bea90ef037cc2fc496f46690770cfa2d00f7
diff --git a/test/904-object-allocation/tracking.cc b/test/904-object-allocation/tracking.cc
index f7296b1..abb6083 100644
--- a/test/904-object-allocation/tracking.cc
+++ b/test/904-object-allocation/tracking.cc
@@ -61,7 +61,7 @@
   }
 
   T Get(JNIEnv* env) const {
-    return env->NewLocalRef(obj_);
+    return reinterpret_cast<T>(env->NewLocalRef(obj_));
   }
 
  private:
@@ -75,7 +75,9 @@
 };
 
 struct EventLog {
-  std::string msg_;
+  ScopedGlobalRef<jclass> object_klass;
+  ScopedGlobalRef<jclass> object_klass2;
+  jlong size;
   ScopedGlobalRef<jthread> thr_;
 };
 
@@ -88,15 +90,11 @@
                                     jobject object,
                                     jclass object_klass,
                                     jlong size) {
-  std::string object_klass_descriptor = GetClassName(jni_env, object_klass);
   ScopedLocalRef<jclass> object_klass2(jni_env, jni_env->GetObjectClass(object));
-  std::string object_klass_descriptor2 = GetClassName(jni_env, object_klass2.get());
-
   std::lock_guard<std::mutex> guard(gEventsMutex);
-  gEvents.push_back({android::base::StringPrintf("ObjectAllocated type %s/%s size %zu",
-                                                 object_klass_descriptor.c_str(),
-                                                 object_klass_descriptor2.c_str(),
-                                                 static_cast<size_t>(size)),
+  gEvents.push_back({ScopedGlobalRef<jclass>(jni_env, object_klass),
+                     ScopedGlobalRef<jclass>(jni_env, object_klass2.get()),
+                     size,
                      ScopedGlobalRef<jthread>(jni_env, thread)});
 }
 
@@ -135,7 +133,15 @@
       ScopedLocalRef<jthread> thr(env, ev.thr_.Get(env));
       for (jthread req_thread : thread_lst) {
         if (env->IsSameObject(req_thread, thr.get())) {
-          real_events.push_back(ev.msg_);
+          ScopedLocalRef<jclass> klass(env, ev.object_klass.Get(env));
+          ScopedLocalRef<jclass> klass2(env, ev.object_klass2.Get(env));
+          std::string object_klass_descriptor = GetClassName(env, klass.get());
+          std::string object_klass_descriptor2 = GetClassName(env, klass2.get());
+          std::string res(android::base::StringPrintf("ObjectAllocated type %s/%s size %zu",
+                                                      object_klass_descriptor.c_str(),
+                                                      object_klass_descriptor2.c_str(),
+                                                      static_cast<size_t>(ev.size)));
+          real_events.push_back(res);
           break;
         }
       }