blob: 4f7228bb7500f9f798402041363adcbed653ae08 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070016
Carl Shapiro2ed144c2011-07-26 16:52:08 -070017#include <signal.h>
Elliott Hughes90a33692011-08-30 13:27:07 -070018
19#include <algorithm>
20#include <cstdio>
21#include <cstring>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070022#include <string>
Carl Shapiro7b216702011-06-17 15:09:26 -070023
Elliott Hughes90a33692011-08-30 13:27:07 -070024#include "ScopedLocalRef.h"
25#include "UniquePtr.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070026#include "jni.h"
27#include "logging.h"
Elliott Hughes0a96fe02011-08-19 10:22:04 -070028#include "toStringArray.h"
Jesse Wilson95caa792011-10-12 18:14:17 -040029#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070030
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080031namespace art {
32
Carl Shapiro2ed144c2011-07-26 16:52:08 -070033// Determine whether or not the specified method is public.
Elliott Hughese84278b2012-03-22 10:06:53 -070034static bool IsMethodPublic(JNIEnv* env, jclass c, jmethodID method_id) {
35 ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(c, method_id, JNI_FALSE));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070036 if (reflected.get() == NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070037 fprintf(stderr, "Failed to get reflected method\n");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070038 return false;
39 }
40 // We now have a Method instance. We need to call its
41 // getModifiers() method.
Elliott Hughes0c8c6732011-10-02 16:14:13 -070042 ScopedLocalRef<jclass> method(env, env->FindClass("java/lang/reflect/Method"));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070043 if (method.get() == NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070044 fprintf(stderr, "Failed to find class Method\n");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070045 return false;
46 }
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070047 jmethodID get_modifiers = env->GetMethodID(method.get(), "getModifiers", "()I");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070048 if (get_modifiers == NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070049 fprintf(stderr, "Failed to find reflect.Method.getModifiers\n");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070050 return false;
51 }
Elliott Hughesc1674ed2011-08-25 18:09:09 -070052 int modifiers = env->CallIntMethod(reflected.get(), get_modifiers);
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080053 if ((modifiers & kAccPublic) == 0) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070054 return false;
55 }
56 return true;
57}
58
Elliott Hughes1bac54f2012-03-16 12:48:31 -070059static int InvokeMain(JNIEnv* env, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070060 // We want to call main() with a String array with our arguments in
61 // it. Create an array and populate it. Note argv[0] is not
62 // included.
Elliott Hughes0a96fe02011-08-19 10:22:04 -070063 ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070064 if (args.get() == NULL) {
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070065 env->ExceptionDescribe();
66 return EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070067 }
68
69 // Find [class].main(String[]).
70
71 // Convert "com.android.Blah" to "com/android/Blah".
Elliott Hughese5b0dc82011-08-23 09:59:02 -070072 std::string class_name(argv[0]);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070073 std::replace(class_name.begin(), class_name.end(), '.', '/');
74
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070075 ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str()));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070076 if (klass.get() == NULL) {
77 fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str());
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070078 env->ExceptionDescribe();
79 return EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070080 }
81
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070082 jmethodID method = env->GetStaticMethodID(klass.get(), "main", "([Ljava/lang/String;)V");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070083 if (method == NULL) {
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070084 fprintf(stderr, "Unable to find static main(String[]) in '%s'\n", class_name.c_str());
85 env->ExceptionDescribe();
86 return EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070087 }
88
89 // Make sure the method is public. JNI doesn't prevent us from
90 // calling a private method, so we have to check it explicitly.
91 if (!IsMethodPublic(env, klass.get(), method)) {
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070092 fprintf(stderr, "Sorry, main() is not public in '%s'\n", class_name.c_str());
93 env->ExceptionDescribe();
94 return EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070095 }
96
97 // Invoke main().
Carl Shapiro2ed144c2011-07-26 16:52:08 -070098 env->CallStaticVoidMethod(klass.get(), method, args.get());
Elliott Hughes0c8c6732011-10-02 16:14:13 -070099
100 // Check whether there was an uncaught exception. We don't log any uncaught exception here;
101 // detaching this thread will do that for us, but it will clear the exception (and invalidate
102 // our JNIEnv), so we need to check here.
103 return env->ExceptionCheck() ? EXIT_FAILURE : EXIT_SUCCESS;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700104}
105
Elliott Hughes81ff3182012-03-23 20:35:56 -0700106// Parse arguments. Most of it just gets passed through to the runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700107// The JNI spec defines a handful of standard arguments.
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800108int oatexec(int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700109 setvbuf(stdout, NULL, _IONBF, 0);
110
111 // Skip over argv[0].
112 argv++;
113 argc--;
114
115 // If we're adding any additional stuff, e.g. function hook specifiers,
116 // add them to the count here.
117 //
Elliott Hughes81ff3182012-03-23 20:35:56 -0700118 // We're over-allocating, because this includes the options to the runtime
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700119 // plus the options to the program.
120 int option_count = argc;
Elliott Hughes90a33692011-08-30 13:27:07 -0700121 UniquePtr<JavaVMOption[]> options(new JavaVMOption[option_count]());
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700122
123 // Copy options over. Everything up to the name of the class starts
124 // with a '-' (the function hook stuff is strictly internal).
125 //
126 // [Do we need to catch & handle "-jar" here?]
127 bool need_extra = false;
Elliott Hughes81ff3182012-03-23 20:35:56 -0700128 const char* what = NULL;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700129 int curr_opt, arg_idx;
130 for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) {
131 if (argv[arg_idx][0] != '-' && !need_extra) {
132 break;
133 }
134 options[curr_opt++].optionString = argv[arg_idx];
135
136 // Some options require an additional argument.
137 need_extra = false;
Elliott Hughesd6fe38d2011-10-02 11:14:43 -0700138 if (strcmp(argv[arg_idx], "-classpath") == 0 || strcmp(argv[arg_idx], "-cp") == 0) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700139 need_extra = true;
Elliott Hughes81ff3182012-03-23 20:35:56 -0700140 what = argv[arg_idx];
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700141 }
142 }
143
144 if (need_extra) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700145 fprintf(stderr, "%s must be followed by an additional argument giving a value\n", what);
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700146 return EXIT_FAILURE;
147 }
148
Elliott Hughes81ff3182012-03-23 20:35:56 -0700149 // Make sure they provided a class name.
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700150 if (arg_idx == argc) {
151 fprintf(stderr, "Class name required\n");
152 return EXIT_FAILURE;
153 }
154
155 // insert additional internal options here
156
157 DCHECK_LE(curr_opt, option_count);
158
159 JavaVMInitArgs init_args;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700160 init_args.version = JNI_VERSION_1_6;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700161 init_args.options = options.get();
162 init_args.nOptions = curr_opt;
163 init_args.ignoreUnrecognized = JNI_FALSE;
164
Elliott Hughes81ff3182012-03-23 20:35:56 -0700165 // Start the runtime. The current thread becomes the main thread.
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700166 JavaVM* vm = NULL;
167 JNIEnv* env = NULL;
168 if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700169 fprintf(stderr, "runtime failed to initialize (check log for details)\n");
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700170 return EXIT_FAILURE;
171 }
172
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700173 int rc = InvokeMain(env, &argv[arg_idx]);
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700174
Elliott Hughesf2682d52011-08-15 16:37:04 -0700175 if (vm->DetachCurrentThread() != JNI_OK) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700176 fprintf(stderr, "Warning: unable to detach main thread\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700177 rc = EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700178 }
179
Elliott Hughesf2682d52011-08-15 16:37:04 -0700180 if (vm->DestroyJavaVM() != 0) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700181 fprintf(stderr, "Warning: runtime did not shut down cleanly\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700182 rc = EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700183 }
184
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700185 return rc;
Carl Shapiro7b216702011-06-17 15:09:26 -0700186}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800187
188} // namespace art
189
190int main(int argc, char** argv) {
191 return art::oatexec(argc, argv);
192}