blob: 335396d2aef30aca406861e3ffe0ec1258f622f5 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07004
Elliott Hughesffe67362011-07-17 12:09:27 -07005#include <cstdio>
6#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -07007#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -07008#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -07009
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "class_linker.h"
11#include "heap.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070012#include "jni_internal.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070013#include "scoped_ptr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "thread.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070015
Carl Shapiro1fb86202011-06-27 17:43:13 -070016namespace art {
17
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018Runtime* Runtime::instance_ = NULL;
19
Carl Shapiro61e019d2011-07-14 16:53:09 -070020Runtime::~Runtime() {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070021 // TODO: use smart pointers instead. (we'll need the pimpl idiom.)
Carl Shapiro61e019d2011-07-14 16:53:09 -070022 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070023 Heap::Destroy();
Carl Shapiro61e019d2011-07-14 16:53:09 -070024 delete thread_list_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070025 delete java_vm_;
Carl Shapiro4acf4642011-07-26 18:54:13 -070026 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070027 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070028 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070029}
30
Elliott Hughesffe67362011-07-17 12:09:27 -070031void Runtime::Abort(const char* file, int line) {
32 // Get any pending output out of the way.
33 fflush(NULL);
34
35 // Many people have difficulty distinguish aborts from crashes,
36 // so be explicit.
37 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
38
Elliott Hughesffe67362011-07-17 12:09:27 -070039 // Perform any platform-specific pre-abort actions.
40 PlatformAbort(file, line);
41
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070042 // use abort hook if we have one
43 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
44 Runtime::Current()->abort_();
45 // notreached
46 }
47
Elliott Hughesffe67362011-07-17 12:09:27 -070048 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070049 // receive SIGABRT. debuggerd dumps the stack trace of the main
50 // thread, whether or not that was the thread that failed. By
51 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070052 // fault in the current thread, and get a useful log from debuggerd.
53 // We can also trivially tell the difference between a VM crash and
54 // a deliberate abort by looking at the fault address.
55 *reinterpret_cast<char*>(0xdeadd00d) = 38;
56 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070057 // notreached
58}
59
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070060// Splits a C string using the given delimiter characters into a vector of
61// strings. Empty strings will be omitted.
62void Split(const char* str, const char* delim, std::vector<std::string>& vec) {
63 DCHECK(str != NULL);
64 DCHECK(delim != NULL);
65 scoped_ptr_malloc<char> tmp(strdup(str));
Carl Shapirofc322c72011-07-27 00:20:01 -070066 char* full = tmp.get();
67 char* p = full;
Carl Shapirod3c15752011-07-27 16:27:22 -070068 while (p != NULL) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070069 p = strpbrk(full, delim);
Carl Shapirofc322c72011-07-27 00:20:01 -070070 if (p != NULL) {
71 p[0] = '\0';
72 }
73 if (full[0] != '\0') {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070074 vec.push_back(std::string(full));
Carl Shapirofc322c72011-07-27 00:20:01 -070075 }
Carl Shapirod3c15752011-07-27 16:27:22 -070076 if (p != NULL) {
Carl Shapirofc322c72011-07-27 00:20:01 -070077 full = p + 1;
78 }
79 }
80}
81
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070082// Splits a colon delimited list of pathname elements into a vector of
83// strings. Empty strings will be omitted.
84void ParseClassPath(const char* class_path, std::vector<std::string>& vec) {
85 Split(class_path, ":", vec);
86}
Brian Carlstrom8a436592011-08-15 21:27:23 -070087
88// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
89// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
90// [gG] gigabytes.
91//
92// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -070093// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
94// of 1024.
95//
96// The spec says the -Xmx and -Xms options must be multiples of 1024. It
97// doesn't say anything about -Xss.
98//
99// Returns 0 (a useless size) if "s" is malformed or specifies a low or
100// non-evenly-divisible value.
101//
102size_t ParseMemoryOption(const char *s, size_t div) {
103 // strtoul accepts a leading [+-], which we don't want,
104 // so make sure our string starts with a decimal digit.
105 if (isdigit(*s)) {
106 const char *s2;
107 size_t val = strtoul(s, (char **)&s2, 10);
108 if (s2 != s) {
109 // s2 should be pointing just after the number.
110 // If this is the end of the string, the user
111 // has specified a number of bytes. Otherwise,
112 // there should be exactly one more character
113 // that specifies a multiplier.
114 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700115 // The remainder of the string is either a single multiplier
116 // character, or nothing to indicate that the value is in
117 // bytes.
118 char c = *s2++;
119 if (*s2 == '\0') {
120 size_t mul;
121 if (c == '\0') {
122 mul = 1;
123 } else if (c == 'k' || c == 'K') {
124 mul = KB;
125 } else if (c == 'm' || c == 'M') {
126 mul = MB;
127 } else if (c == 'g' || c == 'G') {
128 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700129 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700130 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700131 return 0;
132 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700133
134 if (val <= std::numeric_limits<size_t>::max() / mul) {
135 val *= mul;
136 } else {
137 // Clamp to a multiple of 1024.
138 val = std::numeric_limits<size_t>::max() & ~(1024-1);
139 }
140 } else {
141 // There's more than one character after the numeric part.
142 return 0;
143 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700144 }
145 // The man page says that a -Xm value must be a multiple of 1024.
146 if (val % div == 0) {
147 return val;
148 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700149 }
150 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700151 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700152}
153
Elliott Hughes0af55432011-08-17 18:37:28 -0700154void LoadJniLibrary(JavaVMExt* vm, const char* name) {
155 // TODO: OS_SHARED_LIB_FORMAT_STR
156 std::string mapped_name(StringPrintf("lib%s.so", name));
157 char* reason = NULL;
158 if (!vm->LoadNativeLibrary(mapped_name, NULL, &reason)) {
159 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
160 << reason;
161 }
162}
163
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700164DexFile* Open(const std::string& filename) {
165 if (filename.size() < 4) {
166 LOG(WARNING) << "Ignoring short classpath entry '" << filename << "'";
167 return NULL;
168 }
169 std::string suffix(filename.substr(filename.size() - 4));
170 if (suffix == ".zip" || suffix == ".jar" || suffix == ".apk") {
171 return DexFile::OpenZip(filename);
172 } else {
173 return DexFile::OpenFile(filename);
174 }
175}
176
Brian Carlstrom8a436592011-08-15 21:27:23 -0700177void CreateBootClassPath(const char* boot_class_path_cstr,
178 std::vector<DexFile*>& boot_class_path_vector) {
179 CHECK(boot_class_path_cstr != NULL);
Carl Shapirofc322c72011-07-27 00:20:01 -0700180 std::vector<std::string> parsed;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700181 ParseClassPath(boot_class_path_cstr, parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700182 for (size_t i = 0; i < parsed.size(); ++i) {
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700183 DexFile* dex_file = Open(parsed[i]);
Carl Shapirofc322c72011-07-27 00:20:01 -0700184 if (dex_file != NULL) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700185 boot_class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700186 }
187 }
188}
189
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700190Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
191 scoped_ptr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700192 const char* boot_class_path = getenv("BOOTCLASSPATH");
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700193 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700194#ifdef NDEBUG
Elliott Hughes0af55432011-08-17 18:37:28 -0700195 // -Xcheck:jni and -verbose:jni are off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700196 parsed->check_jni_ = false;
197#else
198 // ...but on by default in debug builds.
199 parsed->check_jni_ = true;
Elliott Hughes0af55432011-08-17 18:37:28 -0700200 parsed->verbose_.insert("jni");
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700201#endif
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700202 parsed->heap_initial_size_ = Heap::kInitialSize;
203 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700204 parsed->stack_size_ = Thread::kDefaultStackSize;
205
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700206 parsed->hook_vfprintf_ = vfprintf;
207 parsed->hook_exit_ = exit;
208 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700209
210 for (size_t i = 0; i < options.size(); ++i) {
211 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700212 if (option.starts_with("-Xbootclasspath:")) {
213 boot_class_path = option.substr(strlen("-Xbootclasspath:")).data();
214 } else if (option == "bootclasspath") {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700215 const void* dex_vector = options[i].second;
216 const std::vector<DexFile*>* v = reinterpret_cast<const std::vector<DexFile*>*>(dex_vector);
217 if (v == NULL) {
218 if (ignore_unrecognized) {
219 continue;
220 }
221 // TODO: usage
222 LOG(FATAL) << "Could not parse " << option;
223 return NULL;
224 }
225 parsed->boot_class_path_ = *v;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700226 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700227 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700228 } else if (option.starts_with("-Xcheck:jni")) {
229 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700230 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700231 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
232 if (size == 0) {
233 if (ignore_unrecognized) {
234 continue;
235 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700236 // TODO: usage
Brian Carlstromf734cf52011-08-17 16:28:14 -0700237 LOG(FATAL) << "Could not parse " << option;
238 return NULL;
239 }
240 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700241 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700242 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
243 if (size == 0) {
244 if (ignore_unrecognized) {
245 continue;
246 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700247 // TODO: usage
Brian Carlstromf734cf52011-08-17 16:28:14 -0700248 LOG(FATAL) << "Could not parse " << option;
249 return NULL;
250 }
251 parsed->heap_maximum_size_ = size;
252 } else if (option.starts_with("-Xss")) {
253 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
254 if (size == 0) {
255 if (ignore_unrecognized) {
256 continue;
257 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700258 // TODO: usage
Brian Carlstromf734cf52011-08-17 16:28:14 -0700259 LOG(FATAL) << "Could not parse " << option;
260 return NULL;
261 }
262 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700263 } else if (option.starts_with("-D")) {
264 parsed->properties_.push_back(option.substr(strlen("-D")).data());
265 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700266 std::vector<std::string> verbose_options;
267 Split(option.substr(strlen("-verbose:")).data(), ",", verbose_options);
268 for (size_t i = 0; i < verbose_options.size(); ++i) {
269 parsed->verbose_.insert(verbose_options[i]);
270 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700271 } else if (option == "vfprintf") {
272 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
273 } else if (option == "exit") {
274 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
275 } else if (option == "abort") {
276 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700277 } else {
278 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700279 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700280 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700281 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700282 }
283 }
284 }
285
286 if (boot_class_path == NULL) {
287 boot_class_path = "";
288 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700289 if (parsed->boot_class_path_.size() == 0) {
290 CreateBootClassPath(boot_class_path, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700291 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700292 return parsed.release();
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700293}
294
Brian Carlstrom8a436592011-08-15 21:27:23 -0700295Runtime* Runtime::Create(const std::vector<const DexFile*>& boot_class_path) {
296 Runtime::Options options;
297 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
298 return Runtime::Create(options, false);
299}
300
301Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700302 // TODO: acquire a static mutex on Runtime to avoid racing.
303 if (Runtime::instance_ != NULL) {
304 return NULL;
305 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700306 scoped_ptr<Runtime> runtime(new Runtime());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700307 bool success = runtime->Init(options, ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700308 if (!success) {
309 return NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700310 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700311 instance_ = runtime.release();
312
313 // Most JNI libraries can just use System.loadLibrary, but you can't
314 // if you're the library that implements System.loadLibrary!
315 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
316
317 return instance_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700318}
319
Elliott Hughes0af55432011-08-17 18:37:28 -0700320bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700321 CHECK_EQ(kPageSize, sysconf(_SC_PAGE_SIZE));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700322
Elliott Hughes0af55432011-08-17 18:37:28 -0700323 scoped_ptr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
324 if (options == NULL) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700325 return false;
326 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700327 vfprintf_ = options->hook_vfprintf_;
328 exit_ = options->hook_exit_;
329 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700330
Brian Carlstromb765be02011-08-17 23:54:10 -0700331 stack_size_ = options->stack_size_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700332 thread_list_ = ThreadList::Create();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700333
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700334 if (!Heap::Init(options->heap_initial_size_,
335 options->heap_maximum_size_,
336 options->boot_image_)) {
337 return false;
338 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700339
Elliott Hughes0af55432011-08-17 18:37:28 -0700340 bool verbose_jni = options->verbose_.find("jni") != options->verbose_.end();
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700341 java_vm_ = new JavaVMExt(this, options->check_jni_, verbose_jni);
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700342
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700343 if (!Thread::Init()) {
344 return false;
345 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700346 Thread* current_thread = Thread::Attach(this);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700347 thread_list_->Register(current_thread);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700348
Elliott Hughes0af55432011-08-17 18:37:28 -0700349 class_linker_ = ClassLinker::Create(options->boot_class_path_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700350
Carl Shapiro1fb86202011-06-27 17:43:13 -0700351 return true;
352}
353
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700354bool Runtime::AttachCurrentThread(const char* name, JNIEnv** penv) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700355 return Thread::Attach(instance_) != NULL;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700356}
357
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700358bool Runtime::AttachCurrentThreadAsDaemon(const char* name, JNIEnv** penv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700359 // TODO: do something different for daemon threads.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700360 return Thread::Attach(instance_) != NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700361}
362
Ian Rogersb033c752011-07-20 12:22:35 -0700363bool Runtime::DetachCurrentThread() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700364 UNIMPLEMENTED(WARNING);
Ian Rogersb033c752011-07-20 12:22:35 -0700365 return true;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700366}
367
368} // namespace art