blob: 27709fda4a20d92124e95d5659724be911b67e62 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
Brian Carlstromfa42b442013-06-17 12:53:45 -07002 * copyright (C) 2011 The Android Open Source Project
Elliott Hughes2faa5f12012-01-30 14:42:07 -08003 *
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 Shapiro6b6b5f02011-06-21 15:05:09 -070016
Carl Shapiro2ed144c2011-07-26 16:52:08 -070017#include <signal.h>
Ian Rogersbd5ea6a2014-04-16 16:34:44 -070018#include <stdio.h>
Ian Rogers866ac802014-05-16 12:27:03 -070019#include <stdlib.h>
Ian Rogersbd5ea6a2014-04-16 16:34:44 -070020#include <string.h>
Kenny Root570c0092013-09-11 15:15:09 -070021#include <algorithm>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
Carl Shapiro7b216702011-06-17 15:09:26 -070023
Roland Levillainbdf17372021-05-06 00:19:19 +010024#include "base/fast_exit.h"
Steven Morelande431e272017-07-18 16:53:49 -070025#include "jni.h"
Steven Moreland56bd5622017-07-19 10:31:46 -070026#include "nativehelper/JniInvocation.h"
Steven Morelande431e272017-07-18 16:53:49 -070027#include "nativehelper/ScopedLocalRef.h"
28#include "nativehelper/toStringArray.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070029
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080030namespace art {
31
Carl Shapiro2ed144c2011-07-26 16:52:08 -070032// Determine whether or not the specified method is public.
Elliott Hughese84278b2012-03-22 10:06:53 -070033static bool IsMethodPublic(JNIEnv* env, jclass c, jmethodID method_id) {
34 ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(c, method_id, JNI_FALSE));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070035 if (reflected.get() == nullptr) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070036 fprintf(stderr, "Failed to get reflected method\n");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070037 return false;
38 }
39 // We now have a Method instance. We need to call its
40 // getModifiers() method.
Brian Carlstromfa42b442013-06-17 12:53:45 -070041 jclass method_class = env->FindClass("java/lang/reflect/Method");
Mathieu Chartier2cebb242015-04-21 16:50:40 -070042 if (method_class == nullptr) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070043 fprintf(stderr, "Failed to find class java.lang.reflect.Method\n");
44 return false;
45 }
46 jmethodID mid = env->GetMethodID(method_class, "getModifiers", "()I");
Mathieu Chartier2cebb242015-04-21 16:50:40 -070047 if (mid == nullptr) {
Elliott Hugheseac76672012-05-24 21:56:51 -070048 fprintf(stderr, "Failed to find java.lang.reflect.Method.getModifiers\n");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070049 return false;
50 }
Elliott Hugheseac76672012-05-24 21:56:51 -070051 int modifiers = env->CallIntMethod(reflected.get(), mid);
Brian Carlstromfa42b442013-06-17 12:53:45 -070052 static const int PUBLIC = 0x0001; // java.lang.reflect.Modifiers.PUBLIC
53 if ((modifiers & PUBLIC) == 0) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -070054 fprintf(stderr, "Modifiers mismatch\n");
Carl Shapiro2ed144c2011-07-26 16:52:08 -070055 return false;
56 }
57 return true;
58}
59
Elliott Hughes1bac54f2012-03-16 12:48:31 -070060static int InvokeMain(JNIEnv* env, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070061 // We want to call main() with a String array with our arguments in
62 // it. Create an array and populate it. Note argv[0] is not
63 // included.
Elliott Hughes0a96fe02011-08-19 10:22:04 -070064 ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070065 if (args.get() == nullptr) {
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070066 env->ExceptionDescribe();
67 return EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070068 }
69
70 // Find [class].main(String[]).
71
72 // Convert "com.android.Blah" to "com/android/Blah".
Elliott Hughese5b0dc82011-08-23 09:59:02 -070073 std::string class_name(argv[0]);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070074 std::replace(class_name.begin(), class_name.end(), '.', '/');
75
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070076 ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070077 if (klass.get() == nullptr) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070078 fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str());
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070079 env->ExceptionDescribe();
80 return EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070081 }
82
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070083 jmethodID method = env->GetStaticMethodID(klass.get(), "main", "([Ljava/lang/String;)V");
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 if (method == nullptr) {
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070085 fprintf(stderr, "Unable to find static main(String[]) in '%s'\n", class_name.c_str());
86 env->ExceptionDescribe();
87 return EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070088 }
89
90 // Make sure the method is public. JNI doesn't prevent us from
91 // calling a private method, so we have to check it explicitly.
92 if (!IsMethodPublic(env, klass.get(), method)) {
Elliott Hughesd6fe38d2011-10-02 11:14:43 -070093 fprintf(stderr, "Sorry, main() is not public in '%s'\n", class_name.c_str());
94 env->ExceptionDescribe();
95 return EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070096 }
97
98 // Invoke main().
Carl Shapiro2ed144c2011-07-26 16:52:08 -070099 env->CallStaticVoidMethod(klass.get(), method, args.get());
Elliott Hughes0c8c6732011-10-02 16:14:13 -0700100
101 // Check whether there was an uncaught exception. We don't log any uncaught exception here;
102 // detaching this thread will do that for us, but it will clear the exception (and invalidate
103 // our JNIEnv), so we need to check here.
104 return env->ExceptionCheck() ? EXIT_FAILURE : EXIT_SUCCESS;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700105}
106
Elliott Hughes81ff3182012-03-23 20:35:56 -0700107// Parse arguments. Most of it just gets passed through to the runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700108// The JNI spec defines a handful of standard arguments.
Brian Carlstromfa42b442013-06-17 12:53:45 -0700109static int dalvikvm(int argc, char** argv) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700110 setvbuf(stdout, nullptr, _IONBF, 0);
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700111
112 // Skip over argv[0].
113 argv++;
114 argc--;
115
116 // If we're adding any additional stuff, e.g. function hook specifiers,
117 // add them to the count here.
118 //
Elliott Hughes81ff3182012-03-23 20:35:56 -0700119 // We're over-allocating, because this includes the options to the runtime
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700120 // plus the options to the program.
121 int option_count = argc;
Ian Rogers700a4022014-05-19 16:49:03 -0700122 std::unique_ptr<JavaVMOption[]> options(new JavaVMOption[option_count]());
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700123
124 // Copy options over. Everything up to the name of the class starts
125 // with a '-' (the function hook stuff is strictly internal).
126 //
127 // [Do we need to catch & handle "-jar" here?]
128 bool need_extra = false;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700129 const char* lib = nullptr;
130 const char* what = nullptr;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700131 int curr_opt, arg_idx;
132 for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) {
133 if (argv[arg_idx][0] != '-' && !need_extra) {
134 break;
135 }
Brian Carlstromfa42b442013-06-17 12:53:45 -0700136 if (strncmp(argv[arg_idx], "-XXlib:", strlen("-XXlib:")) == 0) {
137 lib = argv[arg_idx] + strlen("-XXlib:");
138 continue;
139 }
140
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700141 options[curr_opt++].optionString = argv[arg_idx];
142
143 // Some options require an additional argument.
144 need_extra = false;
Elliott Hughesd6fe38d2011-10-02 11:14:43 -0700145 if (strcmp(argv[arg_idx], "-classpath") == 0 || strcmp(argv[arg_idx], "-cp") == 0) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700146 need_extra = true;
Elliott Hughes81ff3182012-03-23 20:35:56 -0700147 what = argv[arg_idx];
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700148 }
149 }
150
151 if (need_extra) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700152 fprintf(stderr, "%s must be followed by an additional argument giving a value\n", what);
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700153 return EXIT_FAILURE;
154 }
155
Brian Carlstrom686107e2014-02-25 23:08:33 -0800156 if (curr_opt > option_count) {
Piotr Jastrzebski2eebc992014-10-17 12:40:38 +0100157 fprintf(stderr, "curr_opt(%d) > option_count(%d)\n", curr_opt, option_count);
Brian Carlstromfa42b442013-06-17 12:53:45 -0700158 abort();
159 return EXIT_FAILURE;
160 }
161
162 // Find the JNI_CreateJavaVM implementation.
Brian Carlstrom0eba6332013-06-19 15:08:24 -0700163 JniInvocation jni_invocation;
164 if (!jni_invocation.Init(lib)) {
165 fprintf(stderr, "Failed to initialize JNI invocation API from %s\n", lib);
Brian Carlstromfa42b442013-06-17 12:53:45 -0700166 return EXIT_FAILURE;
167 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700168
169 JavaVMInitArgs init_args;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700170 init_args.version = JNI_VERSION_1_6;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700171 init_args.options = options.get();
172 init_args.nOptions = curr_opt;
173 init_args.ignoreUnrecognized = JNI_FALSE;
174
Elliott Hughes81ff3182012-03-23 20:35:56 -0700175 // Start the runtime. The current thread becomes the main thread.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700176 JavaVM* vm = nullptr;
177 JNIEnv* env = nullptr;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700178 if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) {
Brian Carlstromfa42b442013-06-17 12:53:45 -0700179 fprintf(stderr, "Failed to initialize runtime (check log for details)\n");
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700180 return EXIT_FAILURE;
181 }
182
Brian Carlstrom686107e2014-02-25 23:08:33 -0800183 // Make sure they provided a class name. We do this after
184 // JNI_CreateJavaVM so that things like "-help" have the opportunity
185 // to emit a usage statement.
186 if (arg_idx == argc) {
187 fprintf(stderr, "Class name required\n");
188 return EXIT_FAILURE;
189 }
190
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700191 int rc = InvokeMain(env, &argv[arg_idx]);
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700192
Elliott Hughes17057b12012-04-03 10:22:39 -0700193#if defined(NDEBUG)
194 // The DestroyJavaVM call will detach this thread for us. In debug builds, we don't want to
195 // detach because detaching disables the CheckSafeToLockOrUnlock checking.
Elliott Hughesf2682d52011-08-15 16:37:04 -0700196 if (vm->DetachCurrentThread() != JNI_OK) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700197 fprintf(stderr, "Warning: unable to detach main thread\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700198 rc = EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700199 }
Elliott Hughes17057b12012-04-03 10:22:39 -0700200#endif
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700201
Elliott Hughesf2682d52011-08-15 16:37:04 -0700202 if (vm->DestroyJavaVM() != 0) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700203 fprintf(stderr, "Warning: runtime did not shut down cleanly\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700204 rc = EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700205 }
206
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700207 return rc;
Carl Shapiro7b216702011-06-17 15:09:26 -0700208}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800209
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700210} // namespace art
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800211
Steven Moreland193efa42019-09-25 10:35:47 -0700212// TODO(b/141622862): stop leaks
213extern "C" const char *__asan_default_options() {
214 return "detect_leaks=0";
215}
216
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800217int main(int argc, char** argv) {
Hans Boehm36896be2020-03-09 14:23:23 -0700218 // Do not allow static destructors to be called, since it's conceivable that
Roland Levillainbdf17372021-05-06 00:19:19 +0100219 // daemons may still awaken (literally).
220 art::FastExit(art::dalvikvm(argc, argv));
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800221}