1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
// Copyright 2011 Google Inc. All Rights Reserved.
#include "runtime.h"
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <vector>
#include "JniConstants.h"
#include "class_linker.h"
#include "heap.h"
#include "jni_internal.h"
#include "scoped_ptr.h"
#include "thread.h"
namespace art {
Runtime* Runtime::instance_ = NULL;
Runtime::~Runtime() {
// TODO: use smart pointers instead. (we'll need the pimpl idiom.)
delete class_linker_;
Heap::Destroy();
delete thread_list_;
delete java_vm_;
Thread::Shutdown();
// TODO: acquire a static mutex on Runtime to avoid racing.
CHECK(instance_ == NULL || instance_ == this);
instance_ = NULL;
}
void Runtime::Abort(const char* file, int line) {
// Get any pending output out of the way.
fflush(NULL);
// Many people have difficulty distinguish aborts from crashes,
// so be explicit.
LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
// Perform any platform-specific pre-abort actions.
PlatformAbort(file, line);
// use abort hook if we have one
if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Runtime::Current()->abort_();
// notreached
}
// If we call abort(3) on a device, all threads in the process
// receive SIGABRT. debuggerd dumps the stack trace of the main
// thread, whether or not that was the thread that failed. By
// stuffing a value into a bogus address, we cause a segmentation
// fault in the current thread, and get a useful log from debuggerd.
// We can also trivially tell the difference between a VM crash and
// a deliberate abort by looking at the fault address.
*reinterpret_cast<char*>(0xdeadd00d) = 38;
abort();
// notreached
}
// Splits a C string using the given delimiter characters into a vector of
// strings. Empty strings will be omitted.
void Split(const char* str, const char* delim, std::vector<std::string>& vec) {
DCHECK(str != NULL);
DCHECK(delim != NULL);
scoped_ptr_malloc<char> tmp(strdup(str));
char* full = tmp.get();
char* p = full;
while (p != NULL) {
p = strpbrk(full, delim);
if (p != NULL) {
p[0] = '\0';
}
if (full[0] != '\0') {
vec.push_back(std::string(full));
}
if (p != NULL) {
full = p + 1;
}
}
}
// Splits a colon delimited list of pathname elements into a vector of
// strings. Empty strings will be omitted.
void ParseClassPath(const char* class_path, std::vector<std::string>& vec) {
Split(class_path, ":", vec);
}
// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
// [gG] gigabytes.
//
// "s" should point just past the "-Xm?" part of the string.
// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
// of 1024.
//
// The spec says the -Xmx and -Xms options must be multiples of 1024. It
// doesn't say anything about -Xss.
//
// Returns 0 (a useless size) if "s" is malformed or specifies a low or
// non-evenly-divisible value.
//
size_t ParseMemoryOption(const char *s, size_t div) {
// strtoul accepts a leading [+-], which we don't want,
// so make sure our string starts with a decimal digit.
if (isdigit(*s)) {
const char *s2;
size_t val = strtoul(s, (char **)&s2, 10);
if (s2 != s) {
// s2 should be pointing just after the number.
// If this is the end of the string, the user
// has specified a number of bytes. Otherwise,
// there should be exactly one more character
// that specifies a multiplier.
if (*s2 != '\0') {
// The remainder of the string is either a single multiplier
// character, or nothing to indicate that the value is in
// bytes.
char c = *s2++;
if (*s2 == '\0') {
size_t mul;
if (c == '\0') {
mul = 1;
} else if (c == 'k' || c == 'K') {
mul = KB;
} else if (c == 'm' || c == 'M') {
mul = MB;
} else if (c == 'g' || c == 'G') {
mul = GB;
} else {
// Unknown multiplier character.
return 0;
}
if (val <= std::numeric_limits<size_t>::max() / mul) {
val *= mul;
} else {
// Clamp to a multiple of 1024.
val = std::numeric_limits<size_t>::max() & ~(1024-1);
}
} else {
// There's more than one character after the numeric part.
return 0;
}
}
// The man page says that a -Xm value must be a multiple of 1024.
if (val % div == 0) {
return val;
}
}
}
return 0;
}
void LoadJniLibrary(JavaVMExt* vm, const char* name) {
// TODO: OS_SHARED_LIB_FORMAT_STR
std::string mapped_name(StringPrintf("lib%s.so", name));
std::string reason;
if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
<< reason;
}
}
DexFile* Open(const std::string& filename) {
if (filename.size() < 4) {
LOG(WARNING) << "Ignoring short classpath entry '" << filename << "'";
return NULL;
}
std::string suffix(filename.substr(filename.size() - 4));
if (suffix == ".zip" || suffix == ".jar" || suffix == ".apk") {
return DexFile::OpenZip(filename);
} else {
return DexFile::OpenFile(filename);
}
}
void CreateBootClassPath(const char* boot_class_path_cstr,
std::vector<const DexFile*>& boot_class_path_vector) {
CHECK(boot_class_path_cstr != NULL);
std::vector<std::string> parsed;
ParseClassPath(boot_class_path_cstr, parsed);
for (size_t i = 0; i < parsed.size(); ++i) {
DexFile* dex_file = Open(parsed[i]);
if (dex_file != NULL) {
boot_class_path_vector.push_back(dex_file);
}
}
}
Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
scoped_ptr<ParsedOptions> parsed(new ParsedOptions());
const char* boot_class_path = getenv("BOOTCLASSPATH");
parsed->boot_image_ = NULL;
#ifdef NDEBUG
// -Xcheck:jni is off by default for regular builds...
parsed->check_jni_ = false;
#else
// ...but on by default in debug builds.
parsed->check_jni_ = true;
#endif
parsed->heap_initial_size_ = Heap::kInitialSize;
parsed->heap_maximum_size_ = Heap::kMaximumSize;
parsed->stack_size_ = Thread::kDefaultStackSize;
parsed->hook_vfprintf_ = vfprintf;
parsed->hook_exit_ = exit;
parsed->hook_abort_ = abort;
for (size_t i = 0; i < options.size(); ++i) {
const StringPiece& option = options[i].first;
if (option.starts_with("-Xbootclasspath:")) {
boot_class_path = option.substr(strlen("-Xbootclasspath:")).data();
} else if (option == "bootclasspath") {
const void* dex_vector = options[i].second;
const std::vector<const DexFile*>* v
= reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
if (v == NULL) {
if (ignore_unrecognized) {
continue;
}
// TODO: usage
LOG(FATAL) << "Could not parse " << option;
return NULL;
}
parsed->boot_class_path_ = *v;
} else if (option.starts_with("-Xbootimage:")) {
parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
} else if (option.starts_with("-Xcheck:jni")) {
parsed->check_jni_ = true;
} else if (option.starts_with("-Xms")) {
size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
if (size == 0) {
if (ignore_unrecognized) {
continue;
}
// TODO: usage
LOG(FATAL) << "Could not parse " << option;
return NULL;
}
parsed->heap_initial_size_ = size;
} else if (option.starts_with("-Xmx")) {
size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
if (size == 0) {
if (ignore_unrecognized) {
continue;
}
// TODO: usage
LOG(FATAL) << "Could not parse " << option;
return NULL;
}
parsed->heap_maximum_size_ = size;
} else if (option.starts_with("-Xss")) {
size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
if (size == 0) {
if (ignore_unrecognized) {
continue;
}
// TODO: usage
LOG(FATAL) << "Could not parse " << option;
return NULL;
}
parsed->stack_size_ = size;
} else if (option.starts_with("-D")) {
parsed->properties_.push_back(option.substr(strlen("-D")).data());
} else if (option.starts_with("-verbose:")) {
std::vector<std::string> verbose_options;
Split(option.substr(strlen("-verbose:")).data(), ",", verbose_options);
for (size_t i = 0; i < verbose_options.size(); ++i) {
parsed->verbose_.insert(verbose_options[i]);
}
} else if (option == "vfprintf") {
parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
} else if (option == "exit") {
parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
} else if (option == "abort") {
parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
} else {
if (!ignore_unrecognized) {
// TODO: print usage via vfprintf
LOG(FATAL) << "Unrecognized option " << option;
return NULL;
}
}
}
if (boot_class_path == NULL) {
boot_class_path = "";
}
if (parsed->boot_class_path_.size() == 0) {
CreateBootClassPath(boot_class_path, parsed->boot_class_path_);
}
return parsed.release();
}
Runtime* Runtime::Create(const std::vector<const DexFile*>& boot_class_path) {
Runtime::Options options;
options.push_back(std::make_pair("bootclasspath", &boot_class_path));
return Runtime::Create(options, false);
}
Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
// TODO: acquire a static mutex on Runtime to avoid racing.
if (Runtime::instance_ != NULL) {
return NULL;
}
scoped_ptr<Runtime> runtime(new Runtime());
bool success = runtime->Init(options, ignore_unrecognized);
if (!success) {
return NULL;
}
instance_ = runtime.release();
// Most JNI libraries can just use System.loadLibrary, but you can't
// if you're the library that implements System.loadLibrary!
Thread* self = Thread::Current();
Thread::State old_state = self->GetState();
self->SetState(Thread::kNative);
JniConstants::init(self->GetJniEnv());
LoadJniLibrary(instance_->GetJavaVM(), "javacore");
self->SetState(old_state);
return instance_;
}
bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
scoped_ptr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
if (options == NULL) {
return false;
}
vfprintf_ = options->hook_vfprintf_;
exit_ = options->hook_exit_;
abort_ = options->hook_abort_;
stack_size_ = options->stack_size_;
thread_list_ = ThreadList::Create();
if (!Heap::Init(options->heap_initial_size_,
options->heap_maximum_size_,
options->boot_image_)) {
return false;
}
BlockSignals();
bool verbose_jni = options->verbose_.find("jni") != options->verbose_.end();
java_vm_ = new JavaVMExt(this, options->check_jni_, verbose_jni);
if (!Thread::Startup()) {
return false;
}
Thread* current_thread = Thread::Attach(this);
thread_list_->Register(current_thread);
class_linker_ = ClassLinker::Create(options->boot_class_path_, Heap::GetBootSpace());
return true;
}
void Runtime::BlockSignals() {
sigset_t sigset;
if (sigemptyset(&sigset) == -1) {
PLOG(FATAL) << "sigemptyset failed";
}
if (sigaddset(&sigset, SIGPIPE) == -1) {
PLOG(ERROR) << "sigaddset SIGPIPE failed";
}
// SIGQUIT is used to dump the runtime's state (including stack traces).
if (sigaddset(&sigset, SIGQUIT) == -1) {
PLOG(ERROR) << "sigaddset SIGQUIT failed";
}
// SIGUSR1 is used to initiate a heap dump.
if (sigaddset(&sigset, SIGUSR1) == -1) {
PLOG(ERROR) << "sigaddset SIGUSR1 failed";
}
CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
}
bool Runtime::AttachCurrentThread(const char* name, JNIEnv** penv, bool as_daemon) {
if (as_daemon) {
UNIMPLEMENTED(WARNING) << "TODO: do something different for daemon threads";
}
return Thread::Attach(instance_) != NULL;
}
bool Runtime::DetachCurrentThread() {
Thread* self = Thread::Current();
thread_list_->Unregister(self);
delete self;
return true;
}
} // namespace art
|