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