Working dex2oat and oatexec

adb shell dex2oatd --dex-file=/system/framework/core.jar     --image=/system/framework/boot.oat --base=0x50000000 "'--method=Ljava/lang/System;logI(Ljava/lang/String;)V'" "'--method=Ljava/lang/System;log(CLjava/lang/String;Ljava/lang/Throwable;)V'"
adb shell dex2oatd --boot-dex-file=/system/framework/core.jar --boot=/system/framework/boot.oat --dex-file=/system/framework/art-test-dex-HelloWorld.jar --image=/system/framework/art-test-dex-HelloWorld.oat
adb shell oatexecd -Xbootclasspath:/system/framework/core.jar -Xbootimage:/system/framework/boot.oat -classpath /system/framework/art-test-dex-HelloWorld.jar -Ximage:/system/framework/art-test-dex-HelloWorld.oat HelloWorld

09-05 17:58:18.912  2385  2385 I System  : Hello, world!

Change-Id: I53e534068584f0c3a837313e4d517a0e4a7154fc
diff --git a/src/oatexec.cc b/src/oatexec.cc
new file mode 100644
index 0000000..ca976af
--- /dev/null
+++ b/src/oatexec.cc
@@ -0,0 +1,181 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+
+#include <signal.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <string>
+
+#include "ScopedLocalRef.h"
+#include "UniquePtr.h"
+#include "jni.h"
+#include "logging.h"
+#include "toStringArray.h"
+
+// Determine whether or not the specified method is public.
+static bool IsMethodPublic(JNIEnv* env, jclass clazz, jmethodID method_id) {
+  ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(clazz,
+      method_id, JNI_FALSE));
+  if (reflected.get() == NULL) {
+    fprintf(stderr, "Unable to get reflected method\n");
+    return false;
+  }
+  // We now have a Method instance.  We need to call its
+  // getModifiers() method.
+  ScopedLocalRef<jclass> method(env,
+      env->FindClass("java/lang/reflect/Method"));
+  if (method.get() == NULL) {
+    fprintf(stderr, "Unable to find class Method\n");
+    return false;
+  }
+#if 0 // TODO: try restoring once iftable_ moved to managed heap
+  jmethodID get_modifiers = env->GetMethodID(method.get(),
+                                             "getModifiers",
+                                             "()I");
+  if (get_modifiers == NULL) {
+    fprintf(stderr, "Unable to find reflect.Method.getModifiers\n");
+    return false;
+  }
+#endif
+  static const int PUBLIC = 0x0001;   // java.lang.reflect.Modifiers.PUBLIC
+#if 0 // CallIntMethod not yet implemented
+  int modifiers = env->CallIntMethod(reflected.get(), get_modifiers);
+#else
+  int modifiers = PUBLIC;
+  UNIMPLEMENTED(WARNING) << "assuming main is public...";
+#endif
+  if ((modifiers & PUBLIC) == 0) {
+    return false;
+  }
+  return true;
+}
+
+static void InvokeMain(JNIEnv* env, int argc, char** argv) {
+  // We want to call main() with a String array with our arguments in
+  // it.  Create an array and populate it.  Note argv[0] is not
+  // included.
+  ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1));
+  if (args.get() == NULL) {
+    return;
+  }
+
+  // Find [class].main(String[]).
+
+  // Convert "com.android.Blah" to "com/android/Blah".
+  std::string class_name(argv[0]);
+  std::replace(class_name.begin(), class_name.end(), '.', '/');
+
+  ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str()));
+  if (klass.get() == NULL) {
+    fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str());
+    return;
+  }
+
+  jmethodID method = env->GetStaticMethodID(klass.get(),
+                                            "main",
+                                            "([Ljava/lang/String;)V");
+  if (method == NULL) {
+    fprintf(stderr, "Unable to find static main(String[]) in '%s'\n",
+            class_name.c_str());
+    return;
+  }
+
+  // Make sure the method is public.  JNI doesn't prevent us from
+  // calling a private method, so we have to check it explicitly.
+  if (!IsMethodPublic(env, klass.get(), method)) {
+    fprintf(stderr, "Sorry, main() is not public\n");
+    return;
+  }
+
+  // Invoke main().
+  env->CallStaticVoidMethod(klass.get(), method, args.get());
+}
+
+// Parse arguments.  Most of it just gets passed through to the VM.
+// The JNI spec defines a handful of standard arguments.
+int main(int argc, char** argv) {
+  setvbuf(stdout, NULL, _IONBF, 0);
+
+  // Skip over argv[0].
+  argv++;
+  argc--;
+
+  // If we're adding any additional stuff, e.g. function hook specifiers,
+  // add them to the count here.
+  //
+  // We're over-allocating, because this includes the options to the VM
+  // plus the options to the program.
+  int option_count = argc;
+  UniquePtr<JavaVMOption[]> options(new JavaVMOption[option_count]());
+
+  // Copy options over.  Everything up to the name of the class starts
+  // with a '-' (the function hook stuff is strictly internal).
+  //
+  // [Do we need to catch & handle "-jar" here?]
+  bool need_extra = false;
+  int curr_opt, arg_idx;
+  for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) {
+    if (argv[arg_idx][0] != '-' && !need_extra) {
+      break;
+    }
+    options[curr_opt++].optionString = argv[arg_idx];
+
+    // Some options require an additional argument.
+    need_extra = false;
+    if (strcmp(argv[arg_idx], "-classpath") == 0 ||
+        strcmp(argv[arg_idx], "-cp") == 0) {
+      // others?
+      need_extra = true;
+    }
+  }
+
+  if (need_extra) {
+    fprintf(stderr, "VM requires value after last option flag\n");
+    return EXIT_FAILURE;
+  }
+
+  // Make sure they provided a class name.  We do this after VM init
+  // so that things like "-Xrunjdwp:help" have the opportunity to emit
+  // a usage statement.
+  if (arg_idx == argc) {
+    fprintf(stderr, "Class name required\n");
+    return EXIT_FAILURE;
+  }
+
+  // insert additional internal options here
+
+  DCHECK_LE(curr_opt, option_count);
+
+  JavaVMInitArgs init_args;
+  init_args.version = JNI_VERSION_1_6;
+  init_args.options = options.get();
+  init_args.nOptions = curr_opt;
+  init_args.ignoreUnrecognized = JNI_FALSE;
+
+  // Start VM.  The current thread becomes the main thread of the VM.
+  JavaVM* vm = NULL;
+  JNIEnv* env = NULL;
+  if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) {
+    fprintf(stderr, "VM init failed (check log file)\n");
+    return EXIT_FAILURE;
+  }
+
+  InvokeMain(env, argc - arg_idx, &argv[arg_idx]);
+  int rc = env->ExceptionCheck() ? EXIT_FAILURE : EXIT_SUCCESS;
+  if (rc == EXIT_FAILURE) {
+    env->ExceptionDescribe();
+  }
+
+  if (vm->DetachCurrentThread() != JNI_OK) {
+    fprintf(stderr, "Warning: unable to detach main thread\n");
+    rc = EXIT_FAILURE;
+  }
+
+  if (vm->DestroyJavaVM() != 0) {
+    fprintf(stderr, "Warning: VM did not shut down cleanly\n");
+    rc = EXIT_FAILURE;
+  }
+
+  return rc;
+}