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 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 24 | #include "jni.h" |
| 25 | #include "logging.h" |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 26 | #include "object.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 27 | #include "ScopedLocalRef.h" |
| 28 | #include "toStringArray.h" |
| 29 | #include "UniquePtr.h" |
| 30 | #include "well_known_classes.h" |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 32 | namespace art { |
| 33 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 34 | // Determine whether or not the specified method is public. |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 35 | static bool IsMethodPublic(JNIEnv* env, jclass c, jmethodID method_id) { |
| 36 | ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(c, method_id, JNI_FALSE)); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 37 | if (reflected.get() == NULL) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 38 | fprintf(stderr, "Failed to get reflected method\n"); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 39 | return false; |
| 40 | } |
| 41 | // We now have a Method instance. We need to call its |
| 42 | // getModifiers() method. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 43 | jmethodID mid = env->GetMethodID(WellKnownClasses::java_lang_reflect_AbstractMethod, |
| 44 | "getModifiers", "()I"); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 45 | if (mid == NULL) { |
| 46 | fprintf(stderr, "Failed to find java.lang.reflect.Method.getModifiers\n"); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 47 | return false; |
| 48 | } |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 49 | int modifiers = env->CallIntMethod(reflected.get(), mid); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 50 | if ((modifiers & kAccPublic) == 0) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | return true; |
| 54 | } |
| 55 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 56 | static int InvokeMain(JNIEnv* env, char** argv) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 57 | // We want to call main() with a String array with our arguments in |
| 58 | // it. Create an array and populate it. Note argv[0] is not |
| 59 | // included. |
Elliott Hughes | 0a96fe0 | 2011-08-19 10:22:04 -0700 | [diff] [blame] | 60 | ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1)); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 61 | if (args.get() == NULL) { |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 62 | env->ExceptionDescribe(); |
| 63 | return EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Find [class].main(String[]). |
| 67 | |
| 68 | // Convert "com.android.Blah" to "com/android/Blah". |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 69 | std::string class_name(argv[0]); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 70 | std::replace(class_name.begin(), class_name.end(), '.', '/'); |
| 71 | |
Elliott Hughes | 0dab4ec | 2011-08-11 12:19:32 -0700 | [diff] [blame] | 72 | ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str())); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 73 | if (klass.get() == NULL) { |
| 74 | fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str()); |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 75 | env->ExceptionDescribe(); |
| 76 | return EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 79 | jmethodID method = env->GetStaticMethodID(klass.get(), "main", "([Ljava/lang/String;)V"); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 80 | if (method == NULL) { |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 81 | fprintf(stderr, "Unable to find static main(String[]) in '%s'\n", class_name.c_str()); |
| 82 | env->ExceptionDescribe(); |
| 83 | return EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Make sure the method is public. JNI doesn't prevent us from |
| 87 | // calling a private method, so we have to check it explicitly. |
| 88 | if (!IsMethodPublic(env, klass.get(), method)) { |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 89 | fprintf(stderr, "Sorry, main() is not public in '%s'\n", class_name.c_str()); |
| 90 | env->ExceptionDescribe(); |
| 91 | return EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Invoke main(). |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 95 | env->CallStaticVoidMethod(klass.get(), method, args.get()); |
Elliott Hughes | 0c8c673 | 2011-10-02 16:14:13 -0700 | [diff] [blame] | 96 | |
| 97 | // Check whether there was an uncaught exception. We don't log any uncaught exception here; |
| 98 | // detaching this thread will do that for us, but it will clear the exception (and invalidate |
| 99 | // our JNIEnv), so we need to check here. |
| 100 | return env->ExceptionCheck() ? EXIT_FAILURE : EXIT_SUCCESS; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 103 | // Parse arguments. Most of it just gets passed through to the runtime. |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 104 | // The JNI spec defines a handful of standard arguments. |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 105 | static int oatexec(int argc, char** argv) { |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 106 | InitLogging(argv); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 107 | setvbuf(stdout, NULL, _IONBF, 0); |
| 108 | |
| 109 | // Skip over argv[0]. |
| 110 | argv++; |
| 111 | argc--; |
| 112 | |
| 113 | // If we're adding any additional stuff, e.g. function hook specifiers, |
| 114 | // add them to the count here. |
| 115 | // |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 116 | // We're over-allocating, because this includes the options to the runtime |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 117 | // plus the options to the program. |
| 118 | int option_count = argc; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 119 | UniquePtr<JavaVMOption[]> options(new JavaVMOption[option_count]()); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 120 | |
| 121 | // Copy options over. Everything up to the name of the class starts |
| 122 | // with a '-' (the function hook stuff is strictly internal). |
| 123 | // |
| 124 | // [Do we need to catch & handle "-jar" here?] |
| 125 | bool need_extra = false; |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 126 | const char* what = NULL; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 127 | int curr_opt, arg_idx; |
| 128 | for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) { |
| 129 | if (argv[arg_idx][0] != '-' && !need_extra) { |
| 130 | break; |
| 131 | } |
| 132 | options[curr_opt++].optionString = argv[arg_idx]; |
| 133 | |
| 134 | // Some options require an additional argument. |
| 135 | need_extra = false; |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 136 | 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] | 137 | need_extra = true; |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 138 | what = argv[arg_idx]; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | if (need_extra) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 143 | fprintf(stderr, "%s must be followed by an additional argument giving a value\n", what); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 144 | return EXIT_FAILURE; |
| 145 | } |
| 146 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 147 | // Make sure they provided a class name. |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 148 | if (arg_idx == argc) { |
| 149 | fprintf(stderr, "Class name required\n"); |
| 150 | return EXIT_FAILURE; |
| 151 | } |
| 152 | |
| 153 | // insert additional internal options here |
| 154 | |
| 155 | DCHECK_LE(curr_opt, option_count); |
| 156 | |
| 157 | JavaVMInitArgs init_args; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 158 | init_args.version = JNI_VERSION_1_6; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 159 | init_args.options = options.get(); |
| 160 | init_args.nOptions = curr_opt; |
| 161 | init_args.ignoreUnrecognized = JNI_FALSE; |
| 162 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 163 | // Start the runtime. The current thread becomes the main thread. |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 164 | JavaVM* vm = NULL; |
| 165 | JNIEnv* env = NULL; |
| 166 | if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 167 | fprintf(stderr, "runtime failed to initialize (check log for details)\n"); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 168 | return EXIT_FAILURE; |
| 169 | } |
| 170 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 171 | int rc = InvokeMain(env, &argv[arg_idx]); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 172 | |
Elliott Hughes | 17057b1 | 2012-04-03 10:22:39 -0700 | [diff] [blame] | 173 | #if defined(NDEBUG) |
| 174 | // The DestroyJavaVM call will detach this thread for us. In debug builds, we don't want to |
| 175 | // detach because detaching disables the CheckSafeToLockOrUnlock checking. |
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 | } |
Elliott Hughes | 17057b1 | 2012-04-03 10:22:39 -0700 | [diff] [blame] | 180 | #endif |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 181 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 182 | if (vm->DestroyJavaVM() != 0) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 183 | fprintf(stderr, "Warning: runtime did not shut down cleanly\n"); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 184 | rc = EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 187 | return rc; |
Carl Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 188 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 189 | |
| 190 | } // namespace art |
| 191 | |
| 192 | int main(int argc, char** argv) { |
| 193 | return art::oatexec(argc, argv); |
| 194 | } |