blob: 6108fe80773242c9f5cdd82e7bf562c1d2ed6d99 [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"
Carl Shapirofc322c72011-07-27 00:20:01 -070012#include "scoped_ptr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "thread.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070014
Carl Shapiro1fb86202011-06-27 17:43:13 -070015namespace art {
16
Carl Shapiro2ed144c2011-07-26 16:52:08 -070017Runtime* Runtime::instance_ = NULL;
18
Carl Shapiro61e019d2011-07-14 16:53:09 -070019Runtime::~Runtime() {
20 // TODO: use a smart pointer instead.
21 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070022 Heap::Destroy();
Carl Shapiro61e019d2011-07-14 16:53:09 -070023 delete thread_list_;
Carl Shapiro4acf4642011-07-26 18:54:13 -070024 // TODO: acquire a static mutex on Runtime to avoid racing.
25 CHECK(instance_ == this);
26 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070027}
28
Elliott Hughesffe67362011-07-17 12:09:27 -070029void Runtime::Abort(const char* file, int line) {
30 // Get any pending output out of the way.
31 fflush(NULL);
32
33 // Many people have difficulty distinguish aborts from crashes,
34 // so be explicit.
35 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
36
Elliott Hughesffe67362011-07-17 12:09:27 -070037 // Perform any platform-specific pre-abort actions.
38 PlatformAbort(file, line);
39
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070040 // use abort hook if we have one
41 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
42 Runtime::Current()->abort_();
43 // notreached
44 }
45
Elliott Hughesffe67362011-07-17 12:09:27 -070046 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070047 // receive SIGABRT. debuggerd dumps the stack trace of the main
48 // thread, whether or not that was the thread that failed. By
49 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070050 // fault in the current thread, and get a useful log from debuggerd.
51 // We can also trivially tell the difference between a VM crash and
52 // a deliberate abort by looking at the fault address.
53 *reinterpret_cast<char*>(0xdeadd00d) = 38;
54 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070055 // notreached
56}
57
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070058// Splits a C string using the given delimiter characters into a vector of
59// strings. Empty strings will be omitted.
60void Split(const char* str, const char* delim, std::vector<std::string>& vec) {
61 DCHECK(str != NULL);
62 DCHECK(delim != NULL);
63 scoped_ptr_malloc<char> tmp(strdup(str));
Carl Shapirofc322c72011-07-27 00:20:01 -070064 char* full = tmp.get();
65 char* p = full;
Carl Shapirod3c15752011-07-27 16:27:22 -070066 while (p != NULL) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070067 p = strpbrk(full, delim);
Carl Shapirofc322c72011-07-27 00:20:01 -070068 if (p != NULL) {
69 p[0] = '\0';
70 }
71 if (full[0] != '\0') {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070072 vec.push_back(std::string(full));
Carl Shapirofc322c72011-07-27 00:20:01 -070073 }
Carl Shapirod3c15752011-07-27 16:27:22 -070074 if (p != NULL) {
Carl Shapirofc322c72011-07-27 00:20:01 -070075 full = p + 1;
76 }
77 }
78}
79
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070080// Splits a colon delimited list of pathname elements into a vector of
81// strings. Empty strings will be omitted.
82void ParseClassPath(const char* class_path, std::vector<std::string>& vec) {
83 Split(class_path, ":", vec);
84}
Brian Carlstrom8a436592011-08-15 21:27:23 -070085
86// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
87// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
88// [gG] gigabytes.
89//
90// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -070091// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
92// of 1024.
93//
94// The spec says the -Xmx and -Xms options must be multiples of 1024. It
95// doesn't say anything about -Xss.
96//
97// Returns 0 (a useless size) if "s" is malformed or specifies a low or
98// non-evenly-divisible value.
99//
100size_t ParseMemoryOption(const char *s, size_t div) {
101 // strtoul accepts a leading [+-], which we don't want,
102 // so make sure our string starts with a decimal digit.
103 if (isdigit(*s)) {
104 const char *s2;
105 size_t val = strtoul(s, (char **)&s2, 10);
106 if (s2 != s) {
107 // s2 should be pointing just after the number.
108 // If this is the end of the string, the user
109 // has specified a number of bytes. Otherwise,
110 // there should be exactly one more character
111 // that specifies a multiplier.
112 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700113 // The remainder of the string is either a single multiplier
114 // character, or nothing to indicate that the value is in
115 // bytes.
116 char c = *s2++;
117 if (*s2 == '\0') {
118 size_t mul;
119 if (c == '\0') {
120 mul = 1;
121 } else if (c == 'k' || c == 'K') {
122 mul = KB;
123 } else if (c == 'm' || c == 'M') {
124 mul = MB;
125 } else if (c == 'g' || c == 'G') {
126 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700127 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700128 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700129 return 0;
130 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700131
132 if (val <= std::numeric_limits<size_t>::max() / mul) {
133 val *= mul;
134 } else {
135 // Clamp to a multiple of 1024.
136 val = std::numeric_limits<size_t>::max() & ~(1024-1);
137 }
138 } else {
139 // There's more than one character after the numeric part.
140 return 0;
141 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700142 }
143 // The man page says that a -Xm value must be a multiple of 1024.
144 if (val % div == 0) {
145 return val;
146 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700147 }
148 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700149 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700150}
151
Elliott Hughes0af55432011-08-17 18:37:28 -0700152void LoadJniLibrary(JavaVMExt* vm, const char* name) {
153 // TODO: OS_SHARED_LIB_FORMAT_STR
154 std::string mapped_name(StringPrintf("lib%s.so", name));
155 char* reason = NULL;
156 if (!vm->LoadNativeLibrary(mapped_name, NULL, &reason)) {
157 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
158 << reason;
159 }
160}
161
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700162DexFile* Open(const std::string& filename) {
163 if (filename.size() < 4) {
164 LOG(WARNING) << "Ignoring short classpath entry '" << filename << "'";
165 return NULL;
166 }
167 std::string suffix(filename.substr(filename.size() - 4));
168 if (suffix == ".zip" || suffix == ".jar" || suffix == ".apk") {
169 return DexFile::OpenZip(filename);
170 } else {
171 return DexFile::OpenFile(filename);
172 }
173}
174
Brian Carlstrom8a436592011-08-15 21:27:23 -0700175void CreateBootClassPath(const char* boot_class_path_cstr,
176 std::vector<DexFile*>& boot_class_path_vector) {
177 CHECK(boot_class_path_cstr != NULL);
Carl Shapirofc322c72011-07-27 00:20:01 -0700178 std::vector<std::string> parsed;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700179 ParseClassPath(boot_class_path_cstr, parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700180 for (size_t i = 0; i < parsed.size(); ++i) {
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700181 DexFile* dex_file = Open(parsed[i]);
Carl Shapirofc322c72011-07-27 00:20:01 -0700182 if (dex_file != NULL) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700183 boot_class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700184 }
185 }
186}
187
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700188Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
189 scoped_ptr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700190 const char* boot_class_path = getenv("BOOTCLASSPATH");
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700191 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700192#ifdef NDEBUG
Elliott Hughes0af55432011-08-17 18:37:28 -0700193 // -Xcheck:jni and -verbose:jni are off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700194 parsed->check_jni_ = false;
195#else
196 // ...but on by default in debug builds.
197 parsed->check_jni_ = true;
Elliott Hughes0af55432011-08-17 18:37:28 -0700198 parsed->verbose_.insert("jni");
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700199#endif
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700200 parsed->heap_initial_size_ = Heap::kInitialSize;
201 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700202 parsed->stack_size_ = Thread::kDefaultStackSize;
203
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700204 parsed->hook_vfprintf_ = vfprintf;
205 parsed->hook_exit_ = exit;
206 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700207
208 for (size_t i = 0; i < options.size(); ++i) {
209 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700210 if (option.starts_with("-Xbootclasspath:")) {
211 boot_class_path = option.substr(strlen("-Xbootclasspath:")).data();
212 } else if (option == "bootclasspath") {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700213 const void* dex_vector = options[i].second;
214 const std::vector<DexFile*>* v = reinterpret_cast<const std::vector<DexFile*>*>(dex_vector);
215 if (v == NULL) {
216 if (ignore_unrecognized) {
217 continue;
218 }
219 // TODO: usage
220 LOG(FATAL) << "Could not parse " << option;
221 return NULL;
222 }
223 parsed->boot_class_path_ = *v;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700224 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700225 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700226 } else if (option.starts_with("-Xcheck:jni")) {
227 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700228 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700229 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
230 if (size == 0) {
231 if (ignore_unrecognized) {
232 continue;
233 }
234 // TODO usage
235 LOG(FATAL) << "Could not parse " << option;
236 return NULL;
237 }
238 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700239 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700240 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
241 if (size == 0) {
242 if (ignore_unrecognized) {
243 continue;
244 }
245 // TODO usage
246 LOG(FATAL) << "Could not parse " << option;
247 return NULL;
248 }
249 parsed->heap_maximum_size_ = size;
250 } else if (option.starts_with("-Xss")) {
251 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
252 if (size == 0) {
253 if (ignore_unrecognized) {
254 continue;
255 }
256 // TODO usage
257 LOG(FATAL) << "Could not parse " << option;
258 return NULL;
259 }
260 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700261 } else if (option.starts_with("-D")) {
262 parsed->properties_.push_back(option.substr(strlen("-D")).data());
263 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700264 std::vector<std::string> verbose_options;
265 Split(option.substr(strlen("-verbose:")).data(), ",", verbose_options);
266 for (size_t i = 0; i < verbose_options.size(); ++i) {
267 parsed->verbose_.insert(verbose_options[i]);
268 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700269 } else if (option == "vfprintf") {
270 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
271 } else if (option == "exit") {
272 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
273 } else if (option == "abort") {
274 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700275 } else {
276 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700277 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700278 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700279 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700280 }
281 }
282 }
283
284 if (boot_class_path == NULL) {
285 boot_class_path = "";
286 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700287 if (parsed->boot_class_path_.size() == 0) {
288 CreateBootClassPath(boot_class_path, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700289 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700290 return parsed.release();
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700291}
292
Brian Carlstrom8a436592011-08-15 21:27:23 -0700293Runtime* Runtime::Create(const std::vector<const DexFile*>& boot_class_path) {
294 Runtime::Options options;
295 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
296 return Runtime::Create(options, false);
297}
298
299Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700300 // TODO: acquire a static mutex on Runtime to avoid racing.
301 if (Runtime::instance_ != NULL) {
302 return NULL;
303 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700304 scoped_ptr<Runtime> runtime(new Runtime());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700305 bool success = runtime->Init(options, ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700306 if (!success) {
307 return NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700308 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700309 instance_ = runtime.release();
310
311 // Most JNI libraries can just use System.loadLibrary, but you can't
312 // if you're the library that implements System.loadLibrary!
313 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
314
315 return instance_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700316}
317
Elliott Hughes0af55432011-08-17 18:37:28 -0700318bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700319 CHECK_EQ(kPageSize, sysconf(_SC_PAGE_SIZE));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700320
Elliott Hughes0af55432011-08-17 18:37:28 -0700321 scoped_ptr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
322 if (options == NULL) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700323 return false;
324 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700325 vfprintf_ = options->hook_vfprintf_;
326 exit_ = options->hook_exit_;
327 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700328
Brian Carlstromb765be02011-08-17 23:54:10 -0700329 stack_size_ = options->stack_size_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700330 thread_list_ = ThreadList::Create();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700331
Elliott Hughes0af55432011-08-17 18:37:28 -0700332 Heap::Init(options->heap_initial_size_,
333 options->heap_maximum_size_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700334
Elliott Hughes0af55432011-08-17 18:37:28 -0700335 bool verbose_jni = options->verbose_.find("jni") != options->verbose_.end();
336 java_vm_.reset(new JavaVMExt(this, options->check_jni_, verbose_jni));
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700337
Carl Shapiro61e019d2011-07-14 16:53:09 -0700338 Thread::Init();
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700339 Thread* current_thread = Thread::Attach(this);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700340 thread_list_->Register(current_thread);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700341
Elliott Hughes0af55432011-08-17 18:37:28 -0700342 class_linker_ = ClassLinker::Create(options->boot_class_path_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700343
Carl Shapiro1fb86202011-06-27 17:43:13 -0700344 return true;
345}
346
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700347bool Runtime::AttachCurrentThread(const char* name, JNIEnv** penv) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700348 return Thread::Attach(instance_) != NULL;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700349}
350
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700351bool Runtime::AttachCurrentThreadAsDaemon(const char* name, JNIEnv** penv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700352 // TODO: do something different for daemon threads.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700353 return Thread::Attach(instance_) != NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700354}
355
Ian Rogersb033c752011-07-20 12:22:35 -0700356bool Runtime::DetachCurrentThread() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700357 UNIMPLEMENTED(WARNING);
Ian Rogersb033c752011-07-20 12:22:35 -0700358 return true;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700359}
360
361} // namespace art