Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 3 | #include <cstring> |
| 4 | #include <cstdio> |
| 5 | #include <signal.h> |
| 6 | #include <string> |
Carl Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 7 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 8 | #include "jni.h" |
| 9 | #include "logging.h" |
| 10 | #include "scoped_ptr.h" |
Elliott Hughes | 0a96fe0 | 2011-08-19 10:22:04 -0700 | [diff] [blame^] | 11 | #include "toStringArray.h" |
Elliott Hughes | 0dab4ec | 2011-08-11 12:19:32 -0700 | [diff] [blame] | 12 | #include "ScopedLocalRef.h" |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 13 | |
| 14 | // TODO: move this into the runtime. |
| 15 | static void BlockSigpipe() { |
| 16 | sigset_t sigset; |
| 17 | if (sigemptyset(&sigset) == -1) { |
| 18 | PLOG(ERROR) << "sigemptyset failed"; |
| 19 | return; |
| 20 | } |
| 21 | if (sigaddset(&sigset, SIGPIPE) == -1) { |
| 22 | PLOG(ERROR) << "sigaddset failed"; |
| 23 | return; |
| 24 | } |
| 25 | if (sigprocmask(SIG_BLOCK, &sigset, NULL) == -1) { |
| 26 | PLOG(ERROR) << "sigprocmask failed"; |
| 27 | } |
| 28 | } |
| 29 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 30 | // Determine whether or not the specified method is public. |
| 31 | // |
| 32 | // Returns JNI_TRUE on success, JNI_FALSE on failure. |
| 33 | static bool IsMethodPublic(JNIEnv* env, jclass clazz, jmethodID method_id) { |
Elliott Hughes | 0dab4ec | 2011-08-11 12:19:32 -0700 | [diff] [blame] | 34 | ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(clazz, |
| 35 | method_id, JNI_FALSE)); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 36 | if (reflected.get() == NULL) { |
| 37 | fprintf(stderr, "Unable to get reflected method\n"); |
| 38 | return false; |
| 39 | } |
| 40 | // We now have a Method instance. We need to call its |
| 41 | // getModifiers() method. |
Elliott Hughes | 0dab4ec | 2011-08-11 12:19:32 -0700 | [diff] [blame] | 42 | ScopedLocalRef<jclass> method(env, |
| 43 | env->FindClass("java/lang/reflect/Method")); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 44 | if (method.get() == NULL) { |
| 45 | fprintf(stderr, "Unable to find class Method\n"); |
| 46 | return false; |
| 47 | } |
| 48 | jmethodID get_modifiers = env->GetMethodID(method.get(), |
| 49 | "getModifiers", |
| 50 | "()I"); |
| 51 | if (get_modifiers == NULL) { |
| 52 | fprintf(stderr, "Unable to find reflect.Method.getModifiers\n"); |
| 53 | return false; |
| 54 | } |
| 55 | static const int PUBLIC = 0x0001; // java.lang.reflect.Modifiers.PUBLIC |
| 56 | int modifiers = env->CallIntMethod(method.get(), get_modifiers); |
| 57 | if ((modifiers & PUBLIC) == 0) { |
| 58 | return false; |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 63 | static bool InvokeMain(JNIEnv* env, int argc, char** argv) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 64 | // We want to call main() with a String array with our arguments in |
| 65 | // it. Create an array and populate it. Note argv[0] is not |
| 66 | // included. |
Elliott Hughes | 0a96fe0 | 2011-08-19 10:22:04 -0700 | [diff] [blame^] | 67 | ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1)); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 68 | if (args.get() == NULL) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | // Find [class].main(String[]). |
| 73 | |
| 74 | // Convert "com.android.Blah" to "com/android/Blah". |
| 75 | std::string class_name = argv[0]; |
| 76 | std::replace(class_name.begin(), class_name.end(), '.', '/'); |
| 77 | |
Elliott Hughes | 0dab4ec | 2011-08-11 12:19:32 -0700 | [diff] [blame] | 78 | ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str())); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 79 | if (klass.get() == NULL) { |
| 80 | fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str()); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | jmethodID method = env->GetStaticMethodID(klass.get(), |
| 85 | "main", |
| 86 | "([Ljava/lang/String;)V"); |
| 87 | if (method == NULL) { |
| 88 | fprintf(stderr, "Unable to find static main(String[]) in '%s'\n", |
| 89 | class_name.c_str()); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | // Make sure the method is public. JNI doesn't prevent us from |
| 94 | // calling a private method, so we have to check it explicitly. |
| 95 | if (!IsMethodPublic(env, klass.get(), method)) { |
| 96 | fprintf(stderr, "Sorry, main() is not public\n"); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | // Invoke main(). |
| 101 | |
| 102 | env->CallStaticVoidMethod(klass.get(), method, args.get()); |
| 103 | if (env->ExceptionCheck()) { |
| 104 | return false; |
| 105 | } else { |
| 106 | return true; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Parse arguments. Most of it just gets passed through to the VM. |
| 111 | // The JNI spec defines a handful of standard arguments. |
Carl Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 112 | int main(int argc, char** argv) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 113 | setvbuf(stdout, NULL, _IONBF, 0); |
| 114 | |
| 115 | // Skip over argv[0]. |
| 116 | argv++; |
| 117 | argc--; |
| 118 | |
| 119 | // If we're adding any additional stuff, e.g. function hook specifiers, |
| 120 | // add them to the count here. |
| 121 | // |
| 122 | // We're over-allocating, because this includes the options to the VM |
| 123 | // plus the options to the program. |
| 124 | int option_count = argc; |
| 125 | scoped_array<JavaVMOption> options(new JavaVMOption[option_count]()); |
| 126 | |
| 127 | // Copy options over. Everything up to the name of the class starts |
| 128 | // with a '-' (the function hook stuff is strictly internal). |
| 129 | // |
| 130 | // [Do we need to catch & handle "-jar" here?] |
| 131 | bool need_extra = false; |
| 132 | int curr_opt, arg_idx; |
| 133 | for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) { |
| 134 | if (argv[arg_idx][0] != '-' && !need_extra) { |
| 135 | break; |
| 136 | } |
| 137 | options[curr_opt++].optionString = argv[arg_idx]; |
| 138 | |
| 139 | // Some options require an additional argument. |
| 140 | need_extra = false; |
| 141 | if (strcmp(argv[arg_idx], "-classpath") == 0 || |
| 142 | strcmp(argv[arg_idx], "-cp") == 0) { |
| 143 | // others? |
| 144 | need_extra = true; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (need_extra) { |
| 149 | fprintf(stderr, "VM requires value after last option flag\n"); |
| 150 | return EXIT_FAILURE; |
| 151 | } |
| 152 | |
| 153 | // Make sure they provided a class name. We do this after VM init |
| 154 | // so that things like "-Xrunjdwp:help" have the opportunity to emit |
| 155 | // a usage statement. |
| 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 | |
| 163 | DCHECK_LE(curr_opt, option_count); |
| 164 | |
| 165 | JavaVMInitArgs init_args; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 166 | init_args.version = JNI_VERSION_1_6; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 167 | init_args.options = options.get(); |
| 168 | init_args.nOptions = curr_opt; |
| 169 | init_args.ignoreUnrecognized = JNI_FALSE; |
| 170 | |
| 171 | BlockSigpipe(); |
| 172 | |
| 173 | // Start VM. The current thread becomes the main thread of the VM. |
| 174 | JavaVM* vm = NULL; |
| 175 | JNIEnv* env = NULL; |
| 176 | if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) { |
| 177 | fprintf(stderr, "VM init failed (check log file)\n"); |
| 178 | return EXIT_FAILURE; |
| 179 | } |
| 180 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 181 | bool success = InvokeMain(env, argc - arg_idx, &argv[arg_idx]); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 182 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 183 | if (vm->DetachCurrentThread() != JNI_OK) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 184 | fprintf(stderr, "Warning: unable to detach main thread\n"); |
| 185 | success = false; |
| 186 | } |
| 187 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 188 | if (vm->DestroyJavaVM() != 0) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 189 | fprintf(stderr, "Warning: VM did not shut down cleanly\n"); |
| 190 | success = false; |
| 191 | } |
| 192 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 193 | return success ? EXIT_SUCCESS : EXIT_FAILURE; |
Carl Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 194 | } |