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" |
| 11 | |
| 12 | // TODO: move this into a publicly accessible location |
| 13 | template<typename T> |
| 14 | class scoped_local_ref { |
| 15 | public: |
| 16 | scoped_local_ref(JNIEnv* env, T ref) : env_(env), ref_(ref) {} |
| 17 | ~scoped_local_ref() { reset(); } |
| 18 | |
| 19 | T get() const { return ref_; } |
| 20 | |
| 21 | void reset() { |
| 22 | if (ref_ != NULL) { |
| 23 | env_->DeleteLocalRef(ref_); |
| 24 | ref_ = NULL; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | T release() { |
| 29 | T ref = ref_; |
| 30 | ref_ = NULL; |
| 31 | return ref; |
| 32 | } |
| 33 | |
| 34 | private: |
| 35 | JNIEnv* env_; |
| 36 | T ref_; |
| 37 | DISALLOW_COPY_AND_ASSIGN(scoped_local_ref); |
| 38 | }; |
| 39 | |
| 40 | // TODO: move this into the runtime. |
| 41 | static void BlockSigpipe() { |
| 42 | sigset_t sigset; |
| 43 | if (sigemptyset(&sigset) == -1) { |
| 44 | PLOG(ERROR) << "sigemptyset failed"; |
| 45 | return; |
| 46 | } |
| 47 | if (sigaddset(&sigset, SIGPIPE) == -1) { |
| 48 | PLOG(ERROR) << "sigaddset failed"; |
| 49 | return; |
| 50 | } |
| 51 | if (sigprocmask(SIG_BLOCK, &sigset, NULL) == -1) { |
| 52 | PLOG(ERROR) << "sigprocmask failed"; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // TODO: this code should be shared with other parts of the system |
| 57 | // that create string arrays. |
| 58 | //Create a String[] and populate it with the contents of argv. |
| 59 | static jobjectArray CreateStringArray(JNIEnv* env, char** argv, int argc) { |
| 60 | // Find the String class. |
| 61 | scoped_local_ref<jclass> klass(env, env->FindClass("java/lang/String")); |
| 62 | if (env->ExceptionCheck()) { |
| 63 | fprintf(stderr, "Got exception while finding class String\n"); |
| 64 | return NULL; |
| 65 | } |
| 66 | DCHECK(klass.get() != NULL); |
| 67 | |
| 68 | // Create an array of String elements. |
| 69 | scoped_local_ref<jobjectArray> args(env, env->NewObjectArray(argc, |
| 70 | klass.get(), |
| 71 | NULL)); |
| 72 | if (env->ExceptionCheck()) { |
| 73 | fprintf(stderr, "Got exception while creating String array\n"); |
| 74 | return NULL; |
| 75 | } |
| 76 | DCHECK(args.get() != NULL); |
| 77 | |
| 78 | // Allocate a string object for each argv element. |
| 79 | for (int i = 0; i < argc; ++i) { |
| 80 | scoped_local_ref<jstring> elt(env, env->NewStringUTF(argv[i])); |
| 81 | if (env->ExceptionCheck()) { |
| 82 | fprintf(stderr, "Got exception while allocating Strings\n"); |
| 83 | return NULL; |
| 84 | } |
| 85 | DCHECK(elt.get() != NULL); |
| 86 | env->SetObjectArrayElement(args.get(), i, elt.get()); |
| 87 | } |
| 88 | |
| 89 | // Return a local reference to the newly created array. |
| 90 | return args.release(); |
| 91 | } |
| 92 | |
| 93 | // Determine whether or not the specified method is public. |
| 94 | // |
| 95 | // Returns JNI_TRUE on success, JNI_FALSE on failure. |
| 96 | static bool IsMethodPublic(JNIEnv* env, jclass clazz, jmethodID method_id) { |
| 97 | scoped_local_ref<jobject> reflected(env, env->ToReflectedMethod(clazz, |
| 98 | method_id, |
| 99 | JNI_FALSE)); |
| 100 | if (reflected.get() == NULL) { |
| 101 | fprintf(stderr, "Unable to get reflected method\n"); |
| 102 | return false; |
| 103 | } |
| 104 | // We now have a Method instance. We need to call its |
| 105 | // getModifiers() method. |
| 106 | scoped_local_ref<jclass> method(env, |
| 107 | env->FindClass("java/lang/reflect/Method")); |
| 108 | if (method.get() == NULL) { |
| 109 | fprintf(stderr, "Unable to find class Method\n"); |
| 110 | return false; |
| 111 | } |
| 112 | jmethodID get_modifiers = env->GetMethodID(method.get(), |
| 113 | "getModifiers", |
| 114 | "()I"); |
| 115 | if (get_modifiers == NULL) { |
| 116 | fprintf(stderr, "Unable to find reflect.Method.getModifiers\n"); |
| 117 | return false; |
| 118 | } |
| 119 | static const int PUBLIC = 0x0001; // java.lang.reflect.Modifiers.PUBLIC |
| 120 | int modifiers = env->CallIntMethod(method.get(), get_modifiers); |
| 121 | if ((modifiers & PUBLIC) == 0) { |
| 122 | return false; |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | static bool InvokeMain(JavaVM* vm, JNIEnv* env, int argc, char** argv) { |
| 128 | // We want to call main() with a String array with our arguments in |
| 129 | // it. Create an array and populate it. Note argv[0] is not |
| 130 | // included. |
| 131 | scoped_local_ref<jobjectArray> args(env, CreateStringArray(env, |
| 132 | argv + 1, |
| 133 | argc - 1)); |
| 134 | if (args.get() == NULL) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | // Find [class].main(String[]). |
| 139 | |
| 140 | // Convert "com.android.Blah" to "com/android/Blah". |
| 141 | std::string class_name = argv[0]; |
| 142 | std::replace(class_name.begin(), class_name.end(), '.', '/'); |
| 143 | |
| 144 | scoped_local_ref<jclass> klass(env, env->FindClass(class_name.c_str())); |
| 145 | if (klass.get() == NULL) { |
| 146 | fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str()); |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | jmethodID method = env->GetStaticMethodID(klass.get(), |
| 151 | "main", |
| 152 | "([Ljava/lang/String;)V"); |
| 153 | if (method == NULL) { |
| 154 | fprintf(stderr, "Unable to find static main(String[]) in '%s'\n", |
| 155 | class_name.c_str()); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | // Make sure the method is public. JNI doesn't prevent us from |
| 160 | // calling a private method, so we have to check it explicitly. |
| 161 | if (!IsMethodPublic(env, klass.get(), method)) { |
| 162 | fprintf(stderr, "Sorry, main() is not public\n"); |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | // Invoke main(). |
| 167 | |
| 168 | env->CallStaticVoidMethod(klass.get(), method, args.get()); |
| 169 | if (env->ExceptionCheck()) { |
| 170 | return false; |
| 171 | } else { |
| 172 | return true; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Parse arguments. Most of it just gets passed through to the VM. |
| 177 | // The JNI spec defines a handful of standard arguments. |
Carl Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 178 | int main(int argc, char** argv) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame^] | 179 | setvbuf(stdout, NULL, _IONBF, 0); |
| 180 | |
| 181 | // Skip over argv[0]. |
| 182 | argv++; |
| 183 | argc--; |
| 184 | |
| 185 | // If we're adding any additional stuff, e.g. function hook specifiers, |
| 186 | // add them to the count here. |
| 187 | // |
| 188 | // We're over-allocating, because this includes the options to the VM |
| 189 | // plus the options to the program. |
| 190 | int option_count = argc; |
| 191 | scoped_array<JavaVMOption> options(new JavaVMOption[option_count]()); |
| 192 | |
| 193 | // Copy options over. Everything up to the name of the class starts |
| 194 | // with a '-' (the function hook stuff is strictly internal). |
| 195 | // |
| 196 | // [Do we need to catch & handle "-jar" here?] |
| 197 | bool need_extra = false; |
| 198 | int curr_opt, arg_idx; |
| 199 | for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) { |
| 200 | if (argv[arg_idx][0] != '-' && !need_extra) { |
| 201 | break; |
| 202 | } |
| 203 | options[curr_opt++].optionString = argv[arg_idx]; |
| 204 | |
| 205 | // Some options require an additional argument. |
| 206 | need_extra = false; |
| 207 | if (strcmp(argv[arg_idx], "-classpath") == 0 || |
| 208 | strcmp(argv[arg_idx], "-cp") == 0) { |
| 209 | // others? |
| 210 | need_extra = true; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (need_extra) { |
| 215 | fprintf(stderr, "VM requires value after last option flag\n"); |
| 216 | return EXIT_FAILURE; |
| 217 | } |
| 218 | |
| 219 | // Make sure they provided a class name. We do this after VM init |
| 220 | // so that things like "-Xrunjdwp:help" have the opportunity to emit |
| 221 | // a usage statement. |
| 222 | if (arg_idx == argc) { |
| 223 | fprintf(stderr, "Class name required\n"); |
| 224 | return EXIT_FAILURE; |
| 225 | } |
| 226 | |
| 227 | // insert additional internal options here |
| 228 | |
| 229 | DCHECK_LE(curr_opt, option_count); |
| 230 | |
| 231 | JavaVMInitArgs init_args; |
| 232 | init_args.version = JNI_VERSION_1_4; |
| 233 | init_args.options = options.get(); |
| 234 | init_args.nOptions = curr_opt; |
| 235 | init_args.ignoreUnrecognized = JNI_FALSE; |
| 236 | |
| 237 | BlockSigpipe(); |
| 238 | |
| 239 | // Start VM. The current thread becomes the main thread of the VM. |
| 240 | JavaVM* vm = NULL; |
| 241 | JNIEnv* env = NULL; |
| 242 | if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) { |
| 243 | fprintf(stderr, "VM init failed (check log file)\n"); |
| 244 | return EXIT_FAILURE; |
| 245 | } |
| 246 | |
| 247 | bool success = InvokeMain(vm, env, argc - arg_idx, &argv[arg_idx]); |
| 248 | |
| 249 | if (vm != NULL && vm->DetachCurrentThread() != JNI_OK) { |
| 250 | fprintf(stderr, "Warning: unable to detach main thread\n"); |
| 251 | success = false; |
| 252 | } |
| 253 | |
| 254 | if (vm != NULL && vm->DestroyJavaVM() != 0) { |
| 255 | fprintf(stderr, "Warning: VM did not shut down cleanly\n"); |
| 256 | success = false; |
| 257 | } |
| 258 | |
| 259 | int retval = success ? EXIT_SUCCESS : EXIT_FAILURE; |
| 260 | return retval; |
Carl Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 261 | } |