Implement VMRuntime.

(Some of this forwards to unimplemented stuff in Heap.)

Change-Id: I01e51d5673e61ddfdbfa2098310122502c3afcf1
diff --git a/build/Android.common.mk b/build/Android.common.mk
index 05f20ff..9481df2 100644
--- a/build/Android.common.mk
+++ b/build/Android.common.mk
@@ -76,6 +76,7 @@
 	src/compiler/codegen/arm/Assemble.cc \
 	src/compiler/codegen/arm/LocalOptimizations.cc \
 	src/compiler/codegen/arm/armv7-a/Codegen.cc \
+	src/dalvik_system_VMRuntime.cc \
 	src/dalvik_system_VMStack.cc \
 	src/dex_cache.cc \
 	src/dex_file.cc \
diff --git a/src/dalvik_system_VMRuntime.cc b/src/dalvik_system_VMRuntime.cc
new file mode 100644
index 0000000..1e95637
--- /dev/null
+++ b/src/dalvik_system_VMRuntime.cc
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "class_linker.h"
+#include "jni_internal.h"
+#include "object.h"
+#include "thread.h"
+
+#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
+#include "toStringArray.h"
+
+#include <limits.h>
+
+namespace art {
+
+namespace {
+
+jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) {
+  return Heap::GetTargetHeapUtilization();
+}
+
+void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) {
+  Heap::SetTargetHeapUtilization(target);
+}
+
+void VMRuntime_startJitCompilation(JNIEnv*, jobject) {
+}
+
+void VMRuntime_disableJitCompilation(JNIEnv*, jobject) {
+}
+
+jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass, jint length) {
+#ifdef MOVING_GARBAGE_COLLECTOR
+  // TODO: right now, we don't have a copying collector, so there's no need
+  // to do anything special here, but we ought to pass the non-movability
+  // through to the allocator.
+  UNIMPLEMENTED(FATAL);
+#endif
+
+  Class* element_class = Decode<Class*>(env, javaElementClass);
+  if (element_class == NULL) {
+    Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "element class == null");
+    return NULL;
+  }
+  if (length < 0) {
+    Thread::Current()->ThrowNewException("Ljava/lang/NegativeArraySizeException;", "%d", length);
+    return NULL;
+  }
+
+  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
+  std::string descriptor;
+  descriptor += "[";
+  descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
+  Class* array_class = class_linker->FindClass(descriptor, NULL);
+  Array* result = Array::Alloc(array_class, length);
+  if (result == NULL) {
+    return NULL;
+  }
+  return AddLocalReference<jobject>(env, result);
+}
+
+jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) {
+  Array* array = Decode<Array*>(env, javaArray);
+  if (!array->IsArrayInstance()) {
+    Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", "not an array");
+    return 0;
+  }
+  // TODO: we should also check that this is a non-movable array.
+  return reinterpret_cast<uintptr_t>(array->GetRawData());
+}
+
+void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
+  Heap::ClearGrowthLimit();
+}
+
+jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) {
+  // TODO: debugger!
+  return false;
+}
+
+jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
+  return toStringArray(env, Runtime::Current()->GetProperties());
+}
+
+jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
+  return env->NewStringUTF(Runtime::Current()->GetBootClassPath().c_str());
+}
+
+jstring VMRuntime_classPath(JNIEnv* env, jobject) {
+  return env->NewStringUTF(Runtime::Current()->GetClassPath().c_str());
+}
+
+jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
+  return env->NewStringUTF(Runtime::Current()->GetVersion());
+}
+
+void VMRuntime_setTargetSdkVersion(JNIEnv* env, jobject, jint targetSdkVersion) {
+  // This is the target SDK version of the app we're about to run.
+  // Note that this value may be CUR_DEVELOPMENT (10000).
+  // Note that this value may be 0, meaning "current".
+  if (targetSdkVersion > 0 && targetSdkVersion <= 13 /* honeycomb-mr2 */) {
+    // TODO: running with CheckJNI should override this and force you to obey the strictest rules.
+    LOG(INFO) << "Turning on JNI app bug workarounds for target SDK version " << targetSdkVersion << "...";
+    UNIMPLEMENTED(FATAL) << "can we get this as a command-line argument?";
+    //gDvmJni.work_around_app_jni_bugs = true;
+  }
+}
+
+JNINativeMethod gMethods[] = {
+  NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
+  NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
+  NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
+  NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
+  NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"),
+  NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
+  NATIVE_METHOD(VMRuntime, isDebuggerActive, "()Z"),
+  NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
+  NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
+  NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
+  NATIVE_METHOD(VMRuntime, setTargetSdkVersion, "(I)V"),
+  NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"),
+  NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
+};
+
+}  // namespace
+
+void register_dalvik_system_VMRuntime(JNIEnv* env) {
+  jniRegisterNativeMethods(env, "dalvik/system/VMRuntime", gMethods, NELEM(gMethods));
+}
+
+}  // namespace art
diff --git a/src/heap.h b/src/heap.h
index 8ffb558..ab7564a 100644
--- a/src/heap.h
+++ b/src/heap.h
@@ -64,6 +64,20 @@
   // Implements java.lang.Runtime.freeMemory.
   static int64_t GetFreeMemory();
 
+  // Implements dalvik.system.VMRuntime.clearGrowthLimit.
+  static void ClearGrowthLimit() {
+    UNIMPLEMENTED(WARNING);
+  }
+  // Implements dalvik.system.VMRuntime.getTargetHeapUtilization.
+  static float GetTargetHeapUtilization() {
+    UNIMPLEMENTED(WARNING);
+    return 0.0f;
+  }
+  // Implements dalvik.system.VMRuntime.setTargetHeapUtilization.
+  static void SetTargetHeapUtilization(float target) {
+    UNIMPLEMENTED(WARNING);
+  }
+
   // Blocks the caller until the garbage collector becomes idle.
   static void WaitForConcurrentGcToComplete();
 
diff --git a/src/jni_internal.cc b/src/jni_internal.cc
index fce79fb..56be2f4a 100644
--- a/src/jni_internal.cc
+++ b/src/jni_internal.cc
@@ -107,6 +107,7 @@
   return reinterpret_cast<T>(env->self->DecodeJObject(obj));
 }
 // Explicit instantiations.
+template Array* Decode<Array*>(JNIEnv*, jobject);
 template Class* Decode<Class*>(JNIEnv*, jobject);
 template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
 template Object* Decode<Object*>(JNIEnv*, jobject);
diff --git a/src/runtime.cc b/src/runtime.cc
index 8b3838f..0d48aea 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -166,11 +166,10 @@
   }
 }
 
-void CreateClassPath(const char* class_path_cstr,
+void CreateClassPath(const std::string& class_path,
                      std::vector<const DexFile*>& class_path_vector) {
-  CHECK(class_path_cstr != NULL);
   std::vector<std::string> parsed;
-  Split(class_path_cstr, ':', parsed);
+  Split(class_path, ':', parsed);
   for (size_t i = 0; i < parsed.size(); ++i) {
     const DexFile* dex_file = DexFile::Open(parsed[i]);
     if (dex_file != NULL) {
@@ -181,8 +180,6 @@
 
 Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
   UniquePtr<ParsedOptions> parsed(new ParsedOptions());
-  const char* boot_class_path = NULL;
-  const char* class_path = NULL;
   parsed->boot_image_ = NULL;
 #ifdef NDEBUG
   // -Xcheck:jni is off by default for regular builds...
@@ -207,8 +204,9 @@
   for (size_t i = 0; i < options.size(); ++i) {
     const StringPiece& option = options[i].first;
     if (option.starts_with("-Xbootclasspath:")) {
-      boot_class_path = option.substr(strlen("-Xbootclasspath:")).data();
+      parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
     } else if (option == "bootclasspath") {
+      UNIMPLEMENTED(WARNING) << "what should VMRuntime.getBootClassPath return here?";
       const void* dex_vector = options[i].second;
       const std::vector<const DexFile*>* v
           = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
@@ -230,7 +228,7 @@
         return NULL;
       }
       const StringPiece& value = options[i].first;
-      class_path = value.data();
+      parsed->class_path_string_ = value.data();
     } else if (option.starts_with("-Xbootimage:")) {
       // TODO: remove when intern_addr_ is removed, just use -Ximage:
       parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
@@ -296,31 +294,29 @@
     }
   }
 
-  // consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
+  // Consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
   // TODO: remove bootclasspath which is only mostly just used by tests?
-  if (!parsed->boot_class_path_.empty() && boot_class_path != NULL) {
+  if (!parsed->boot_class_path_.empty() && !parsed->boot_class_path_string_.empty()) {
     // TODO: usage
     LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
     return NULL;
   }
   if (parsed->boot_class_path_.empty()) {
-    if (boot_class_path == NULL) {
-      boot_class_path = getenv("BOOTCLASSPATH");
-      if (boot_class_path == NULL) {
-        boot_class_path = "";
-      }
+    if (parsed->boot_class_path_string_ == NULL) {
+      const char* BOOTCLASSPATH = getenv("BOOTCLASSPATH");
+      parsed->boot_class_path_string_ = BOOTCLASSPATH;
     }
-    CreateClassPath(boot_class_path, parsed->boot_class_path_);
+    CreateClassPath(parsed->boot_class_path_string_, parsed->boot_class_path_);
   }
 
-  if (class_path == NULL) {
-    class_path = getenv("CLASSPATH");
-    if (class_path == NULL) {
-      class_path = "";
+  if (parsed->class_path_string_ == NULL) {
+    const char* CLASSPATH = getenv("CLASSPATH");
+    if (CLASSPATH != NULL) {
+      parsed->class_path_string_ = CLASSPATH;
     }
   }
   CHECK_EQ(parsed->class_path_.size(), 0U);
-  CreateClassPath(class_path, parsed->class_path_);
+  CreateClassPath(parsed->class_path_string_, parsed->class_path_);
 
   return parsed.release();
 }
@@ -361,13 +357,18 @@
     LOG(WARNING) << "Failed to parse options";
     return false;
   }
+
+  boot_class_path_ = options->boot_class_path_string_;
+  class_path_ = options->class_path_string_;
+  properties_ = options->properties_;
+
   vfprintf_ = options->hook_vfprintf_;
   exit_ = options->hook_exit_;
   abort_ = options->hook_abort_;
 
   default_stack_size_ = options->stack_size_;
-  thread_list_ = new ThreadList;
 
+  thread_list_ = new ThreadList;
   intern_table_ = new InternTable;
 
   Heap::Init(options->heap_initial_size_, options->heap_maximum_size_,
@@ -412,7 +413,7 @@
 #define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
   //REGISTER(register_dalvik_system_DexFile);
   //REGISTER(register_dalvik_system_VMDebug);
-  //REGISTER(register_dalvik_system_VMRuntime);
+  REGISTER(register_dalvik_system_VMRuntime);
   REGISTER(register_dalvik_system_VMStack);
   //REGISTER(register_dalvik_system_Zygote);
   REGISTER(register_java_lang_Class);
diff --git a/src/runtime.h b/src/runtime.h
index bcb118c..5e57018 100644
--- a/src/runtime.h
+++ b/src/runtime.h
@@ -39,7 +39,9 @@
     // returns null if problem parsing and ignore_unrecognized is false
     static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
 
+    std::string boot_class_path_string_;
     std::vector<const DexFile*> boot_class_path_;
+    std::string class_path_string_;
     std::vector<const DexFile*> class_path_;
     const char* boot_image_;
     std::vector<const char*> images_;
@@ -96,14 +98,22 @@
 
   ~Runtime();
 
-  size_t GetDefaultStackSize() const {
-    return default_stack_size_;
+  const std::string& GetBootClassPath() const {
+    return boot_class_path_;
   }
 
   ClassLinker* GetClassLinker() const {
     return class_linker_;
   }
 
+  const std::string& GetClassPath() const {
+    return class_path_;
+  }
+
+  size_t GetDefaultStackSize() const {
+    return default_stack_size_;
+  }
+
   InternTable* GetInternTable() const {
     return intern_table_;
   }
@@ -112,10 +122,18 @@
     return java_vm_;
   }
 
+  const std::vector<std::string>& GetProperties() const {
+    return properties_;
+  }
+
   ThreadList* GetThreadList() const {
     return thread_list_;
   }
 
+  const char* GetVersion() const {
+    return "2.0.0";
+  }
+
   void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
 
  private:
@@ -129,6 +147,10 @@
   void InitLibraries();
   void RegisterRuntimeNativeMethods(JNIEnv*);
 
+  std::string boot_class_path_;
+  std::string class_path_;
+  std::vector<std::string> properties_;
+
   // The default stack size for managed threads created by the runtime.
   size_t default_stack_size_;