blob: 347cdf73fca45d013a02c875d7580b6baf9e322c [file] [log] [blame]
Carl Shapiro6b6b5f02011-06-21 15:05:09 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003#include <signal.h>
Elliott Hughes90a33692011-08-30 13:27:07 -07004
5#include <algorithm>
6#include <cstdio>
7#include <cstring>
Carl Shapiro2ed144c2011-07-26 16:52:08 -07008#include <string>
Carl Shapiro7b216702011-06-17 15:09:26 -07009
Elliott Hughes90a33692011-08-30 13:27:07 -070010#include "ScopedLocalRef.h"
11#include "UniquePtr.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012#include "jni.h"
13#include "logging.h"
Elliott Hughes0a96fe02011-08-19 10:22:04 -070014#include "toStringArray.h"
Jesse Wilson95caa792011-10-12 18:14:17 -040015#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070016
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080017namespace art {
18
Carl Shapiro2ed144c2011-07-26 16:52:08 -070019// Determine whether or not the specified method is public.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070020static bool IsMethodPublic(JNIEnv* env, jclass clazz, jmethodID method_id) {
Elliott Hughes0c8c6732011-10-02 16:14:13 -070021 ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(clazz, method_id, JNI_FALSE));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070022 if (reflected.get() == NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070023 fprintf(stderr, "Failed to get reflected method\n");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024 return false;
25 }
26 // We now have a Method instance. We need to call its
27 // getModifiers() method.
Elliott Hughes0c8c6732011-10-02 16:14:13 -070028 ScopedLocalRef<jclass> method(env, env->FindClass("java/lang/reflect/Method"));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070029 if (method.get() == NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070030 fprintf(stderr, "Failed to find class Method\n");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070031 return false;
32 }
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070033 jmethodID get_modifiers = env->GetMethodID(method.get(), "getModifiers", "()I");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070034 if (get_modifiers == NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070035 fprintf(stderr, "Failed to find reflect.Method.getModifiers\n");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070036 return false;
37 }
Elliott Hughesc1674ed2011-08-25 18:09:09 -070038 int modifiers = env->CallIntMethod(reflected.get(), get_modifiers);
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080039 if ((modifiers & kAccPublic) == 0) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070040 return false;
41 }
42 return true;
43}
44
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070045static int InvokeMain(JNIEnv* env, int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070046 // We want to call main() with a String array with our arguments in
47 // it. Create an array and populate it. Note argv[0] is not
48 // included.
Elliott Hughes0a96fe02011-08-19 10:22:04 -070049 ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070050 if (args.get() == NULL) {
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070051 env->ExceptionDescribe();
52 return EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070053 }
54
55 // Find [class].main(String[]).
56
57 // Convert "com.android.Blah" to "com/android/Blah".
Elliott Hughese5b0dc82011-08-23 09:59:02 -070058 std::string class_name(argv[0]);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070059 std::replace(class_name.begin(), class_name.end(), '.', '/');
60
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070061 ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str()));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070062 if (klass.get() == NULL) {
63 fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str());
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070064 env->ExceptionDescribe();
65 return EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070066 }
67
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070068 jmethodID method = env->GetStaticMethodID(klass.get(), "main", "([Ljava/lang/String;)V");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070069 if (method == NULL) {
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070070 fprintf(stderr, "Unable to find static main(String[]) in '%s'\n", class_name.c_str());
71 env->ExceptionDescribe();
72 return EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070073 }
74
75 // Make sure the method is public. JNI doesn't prevent us from
76 // calling a private method, so we have to check it explicitly.
77 if (!IsMethodPublic(env, klass.get(), method)) {
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070078 fprintf(stderr, "Sorry, main() is not public in '%s'\n", class_name.c_str());
79 env->ExceptionDescribe();
80 return EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070081 }
82
83 // Invoke main().
Carl Shapiro2ed144c2011-07-26 16:52:08 -070084 env->CallStaticVoidMethod(klass.get(), method, args.get());
Elliott Hughes0c8c6732011-10-02 16:14:13 -070085
86 // Check whether there was an uncaught exception. We don't log any uncaught exception here;
87 // detaching this thread will do that for us, but it will clear the exception (and invalidate
88 // our JNIEnv), so we need to check here.
89 return env->ExceptionCheck() ? EXIT_FAILURE : EXIT_SUCCESS;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070090}
91
92// Parse arguments. Most of it just gets passed through to the VM.
93// The JNI spec defines a handful of standard arguments.
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080094int oatexec(int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070095 setvbuf(stdout, NULL, _IONBF, 0);
96
97 // Skip over argv[0].
98 argv++;
99 argc--;
100
101 // If we're adding any additional stuff, e.g. function hook specifiers,
102 // add them to the count here.
103 //
104 // We're over-allocating, because this includes the options to the VM
105 // plus the options to the program.
106 int option_count = argc;
Elliott Hughes90a33692011-08-30 13:27:07 -0700107 UniquePtr<JavaVMOption[]> options(new JavaVMOption[option_count]());
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700108
109 // Copy options over. Everything up to the name of the class starts
110 // with a '-' (the function hook stuff is strictly internal).
111 //
112 // [Do we need to catch & handle "-jar" here?]
113 bool need_extra = false;
114 int curr_opt, arg_idx;
115 for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) {
116 if (argv[arg_idx][0] != '-' && !need_extra) {
117 break;
118 }
119 options[curr_opt++].optionString = argv[arg_idx];
120
121 // Some options require an additional argument.
122 need_extra = false;
Elliott Hughesd6fe38d2011-10-02 11:14:43 -0700123 if (strcmp(argv[arg_idx], "-classpath") == 0 || strcmp(argv[arg_idx], "-cp") == 0) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700124 // others?
125 need_extra = true;
126 }
127 }
128
129 if (need_extra) {
130 fprintf(stderr, "VM requires value after last option flag\n");
131 return EXIT_FAILURE;
132 }
133
134 // Make sure they provided a class name. We do this after VM init
135 // so that things like "-Xrunjdwp:help" have the opportunity to emit
136 // a usage statement.
137 if (arg_idx == argc) {
138 fprintf(stderr, "Class name required\n");
139 return EXIT_FAILURE;
140 }
141
142 // insert additional internal options here
143
144 DCHECK_LE(curr_opt, option_count);
145
146 JavaVMInitArgs init_args;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700147 init_args.version = JNI_VERSION_1_6;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700148 init_args.options = options.get();
149 init_args.nOptions = curr_opt;
150 init_args.ignoreUnrecognized = JNI_FALSE;
151
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700152 // Start VM. The current thread becomes the main thread of the VM.
153 JavaVM* vm = NULL;
154 JNIEnv* env = NULL;
155 if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) {
156 fprintf(stderr, "VM init failed (check log file)\n");
157 return EXIT_FAILURE;
158 }
159
Elliott Hughesd6fe38d2011-10-02 11:14:43 -0700160 int rc = InvokeMain(env, argc - arg_idx, &argv[arg_idx]);
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700161
Elliott Hughesf2682d52011-08-15 16:37:04 -0700162 if (vm->DetachCurrentThread() != JNI_OK) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700163 fprintf(stderr, "Warning: unable to detach main thread\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700164 rc = EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700165 }
166
Elliott Hughesf2682d52011-08-15 16:37:04 -0700167 if (vm->DestroyJavaVM() != 0) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700168 fprintf(stderr, "Warning: VM did not shut down cleanly\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700169 rc = EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700170 }
171
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700172 return rc;
Carl Shapiro7b216702011-06-17 15:09:26 -0700173}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800174
175} // namespace art
176
177int main(int argc, char** argv) {
178 return art::oatexec(argc, argv);
179}