Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame^] | 1 | /* |
| 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 Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 16 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 17 | #include <signal.h> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | #include <cstdio> |
| 21 | #include <cstring> |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 22 | #include <string> |
Carl Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 24 | #include "ScopedLocalRef.h" |
| 25 | #include "UniquePtr.h" |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 26 | #include "jni.h" |
| 27 | #include "logging.h" |
Elliott Hughes | 0a96fe0 | 2011-08-19 10:22:04 -0700 | [diff] [blame] | 28 | #include "toStringArray.h" |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 29 | #include "object.h" |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 31 | namespace art { |
| 32 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 33 | // Determine whether or not the specified method is public. |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 34 | static bool IsMethodPublic(JNIEnv* env, jclass clazz, jmethodID method_id) { |
Elliott Hughes | 0c8c673 | 2011-10-02 16:14:13 -0700 | [diff] [blame] | 35 | ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(clazz, method_id, JNI_FALSE)); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 36 | if (reflected.get() == NULL) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 37 | fprintf(stderr, "Failed to get reflected method\n"); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 38 | return false; |
| 39 | } |
| 40 | // We now have a Method instance. We need to call its |
| 41 | // getModifiers() method. |
Elliott Hughes | 0c8c673 | 2011-10-02 16:14:13 -0700 | [diff] [blame] | 42 | ScopedLocalRef<jclass> method(env, env->FindClass("java/lang/reflect/Method")); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 43 | if (method.get() == NULL) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 44 | fprintf(stderr, "Failed to find class Method\n"); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 45 | return false; |
| 46 | } |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 47 | jmethodID get_modifiers = env->GetMethodID(method.get(), "getModifiers", "()I"); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 48 | if (get_modifiers == NULL) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 49 | fprintf(stderr, "Failed to find reflect.Method.getModifiers\n"); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 50 | return false; |
| 51 | } |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 52 | int modifiers = env->CallIntMethod(reflected.get(), get_modifiers); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 53 | if ((modifiers & kAccPublic) == 0) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 54 | return false; |
| 55 | } |
| 56 | return true; |
| 57 | } |
| 58 | |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 59 | static int InvokeMain(JNIEnv* env, int argc, char** argv) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 60 | // 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 Hughes | 0a96fe0 | 2011-08-19 10:22:04 -0700 | [diff] [blame] | 63 | ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1)); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 64 | if (args.get() == NULL) { |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 65 | env->ExceptionDescribe(); |
| 66 | return EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Find [class].main(String[]). |
| 70 | |
| 71 | // Convert "com.android.Blah" to "com/android/Blah". |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 72 | std::string class_name(argv[0]); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 73 | std::replace(class_name.begin(), class_name.end(), '.', '/'); |
| 74 | |
Elliott Hughes | 0dab4ec | 2011-08-11 12:19:32 -0700 | [diff] [blame] | 75 | ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str())); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 76 | if (klass.get() == NULL) { |
| 77 | fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str()); |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 78 | env->ExceptionDescribe(); |
| 79 | return EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 82 | jmethodID method = env->GetStaticMethodID(klass.get(), "main", "([Ljava/lang/String;)V"); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 83 | if (method == NULL) { |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 84 | fprintf(stderr, "Unable to find static main(String[]) in '%s'\n", class_name.c_str()); |
| 85 | env->ExceptionDescribe(); |
| 86 | return EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 87 | } |
| 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 Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 92 | fprintf(stderr, "Sorry, main() is not public in '%s'\n", class_name.c_str()); |
| 93 | env->ExceptionDescribe(); |
| 94 | return EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // Invoke main(). |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 98 | env->CallStaticVoidMethod(klass.get(), method, args.get()); |
Elliott Hughes | 0c8c673 | 2011-10-02 16:14:13 -0700 | [diff] [blame] | 99 | |
| 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 Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Parse arguments. Most of it just gets passed through to the VM. |
| 107 | // The JNI spec defines a handful of standard arguments. |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 108 | int oatexec(int argc, char** argv) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 109 | 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 | // |
| 118 | // We're over-allocating, because this includes the options to the VM |
| 119 | // plus the options to the program. |
| 120 | int option_count = argc; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 121 | UniquePtr<JavaVMOption[]> options(new JavaVMOption[option_count]()); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 122 | |
| 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; |
| 128 | int curr_opt, arg_idx; |
| 129 | for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) { |
| 130 | if (argv[arg_idx][0] != '-' && !need_extra) { |
| 131 | break; |
| 132 | } |
| 133 | options[curr_opt++].optionString = argv[arg_idx]; |
| 134 | |
| 135 | // Some options require an additional argument. |
| 136 | need_extra = false; |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 137 | if (strcmp(argv[arg_idx], "-classpath") == 0 || strcmp(argv[arg_idx], "-cp") == 0) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 138 | // others? |
| 139 | need_extra = true; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (need_extra) { |
| 144 | fprintf(stderr, "VM requires value after last option flag\n"); |
| 145 | return EXIT_FAILURE; |
| 146 | } |
| 147 | |
| 148 | // Make sure they provided a class name. We do this after VM init |
| 149 | // so that things like "-Xrunjdwp:help" have the opportunity to emit |
| 150 | // a usage statement. |
| 151 | if (arg_idx == argc) { |
| 152 | fprintf(stderr, "Class name required\n"); |
| 153 | return EXIT_FAILURE; |
| 154 | } |
| 155 | |
| 156 | // insert additional internal options here |
| 157 | |
| 158 | DCHECK_LE(curr_opt, option_count); |
| 159 | |
| 160 | JavaVMInitArgs init_args; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 161 | init_args.version = JNI_VERSION_1_6; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 162 | init_args.options = options.get(); |
| 163 | init_args.nOptions = curr_opt; |
| 164 | init_args.ignoreUnrecognized = JNI_FALSE; |
| 165 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 166 | // Start VM. The current thread becomes the main thread of the VM. |
| 167 | JavaVM* vm = NULL; |
| 168 | JNIEnv* env = NULL; |
| 169 | if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) { |
| 170 | fprintf(stderr, "VM init failed (check log file)\n"); |
| 171 | return EXIT_FAILURE; |
| 172 | } |
| 173 | |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 174 | int rc = InvokeMain(env, argc - arg_idx, &argv[arg_idx]); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 175 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 176 | if (vm->DetachCurrentThread() != JNI_OK) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 177 | fprintf(stderr, "Warning: unable to detach main thread\n"); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 178 | rc = EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 181 | if (vm->DestroyJavaVM() != 0) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 182 | fprintf(stderr, "Warning: VM did not shut down cleanly\n"); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 183 | rc = EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 186 | return rc; |
Carl Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 187 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 188 | |
| 189 | } // namespace art |
| 190 | |
| 191 | int main(int argc, char** argv) { |
| 192 | return art::oatexec(argc, argv); |
| 193 | } |