GoogleGit

blob: c4cce2ff732e48e9e8ede1651911d340828f492b [file] [log] [blame]
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  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. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <sys/stat.h>
  19. #include <valgrind.h>
  20. #include <fstream>
  21. #include <iostream>
  22. #include <sstream>
  23. #include <string>
  24. #include <vector>
  25. #include "base/stl_util.h"
  26. #include "base/stringpiece.h"
  27. #include "base/timing_logger.h"
  28. #include "base/unix_file/fd_file.h"
  29. #include "class_linker.h"
  30. #include "dex_file-inl.h"
  31. #include "driver/compiler_driver.h"
  32. #include "elf_fixup.h"
  33. #include "elf_stripper.h"
  34. #include "gc/space/image_space.h"
  35. #include "gc/space/space-inl.h"
  36. #include "image_writer.h"
  37. #include "leb128.h"
  38. #include "mirror/art_method-inl.h"
  39. #include "mirror/class-inl.h"
  40. #include "mirror/class_loader.h"
  41. #include "mirror/object-inl.h"
  42. #include "mirror/object_array-inl.h"
  43. #include "oat_writer.h"
  44. #include "object_utils.h"
  45. #include "os.h"
  46. #include "runtime.h"
  47. #include "ScopedLocalRef.h"
  48. #include "scoped_thread_state_change.h"
  49. #include "sirt_ref.h"
  50. #include "vector_output_stream.h"
  51. #include "well_known_classes.h"
  52. #include "zip_archive.h"
  53. namespace art {
  54. static void UsageErrorV(const char* fmt, va_list ap) {
  55. std::string error;
  56. StringAppendV(&error, fmt, ap);
  57. LOG(ERROR) << error;
  58. }
  59. static void UsageError(const char* fmt, ...) {
  60. va_list ap;
  61. va_start(ap, fmt);
  62. UsageErrorV(fmt, ap);
  63. va_end(ap);
  64. }
  65. static void Usage(const char* fmt, ...) {
  66. va_list ap;
  67. va_start(ap, fmt);
  68. UsageErrorV(fmt, ap);
  69. va_end(ap);
  70. UsageError("Usage: dex2oat [options]...");
  71. UsageError("");
  72. UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile.");
  73. UsageError(" Example: --dex-file=/system/framework/core.jar");
  74. UsageError("");
  75. UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
  76. UsageError(" containing a classes.dex file to compile.");
  77. UsageError(" Example: --zip-fd=5");
  78. UsageError("");
  79. UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file");
  80. UsageError(" corresponding to the file descriptor specified by --zip-fd.");
  81. UsageError(" Example: --zip-location=/system/app/Calculator.apk");
  82. UsageError("");
  83. UsageError(" --oat-file=<file.oat>: specifies the oat output destination via a filename.");
  84. UsageError(" Example: --oat-file=/system/framework/boot.oat");
  85. UsageError("");
  86. UsageError(" --oat-fd=<number>: specifies the oat output destination via a file descriptor.");
  87. UsageError(" Example: --oat-file=/system/framework/boot.oat");
  88. UsageError("");
  89. UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
  90. UsageError(" to the file descriptor specified by --oat-fd.");
  91. UsageError(" Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat");
  92. UsageError("");
  93. UsageError(" --oat-symbols=<file.oat>: specifies the oat output destination with full symbols.");
  94. UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat");
  95. UsageError("");
  96. UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename.");
  97. UsageError(" Example: --bitcode=/system/framework/boot.bc");
  98. UsageError("");
  99. UsageError(" --image=<file.art>: specifies the output image filename.");
  100. UsageError(" Example: --image=/system/framework/boot.art");
  101. UsageError("");
  102. UsageError(" --image-classes=<classname-file>: specifies classes to include in an image.");
  103. UsageError(" Example: --image=frameworks/base/preloaded-classes");
  104. UsageError("");
  105. UsageError(" --base=<hex-address>: specifies the base address when creating a boot image.");
  106. UsageError(" Example: --base=0x50000000");
  107. UsageError("");
  108. UsageError(" --boot-image=<file.art>: provide the image file for the boot class path.");
  109. UsageError(" Example: --boot-image=/system/framework/boot.art");
  110. UsageError(" Default: <host-prefix>/system/framework/boot.art");
  111. UsageError("");
  112. UsageError(" --host-prefix=<path>: used to translate host paths to target paths during");
  113. UsageError(" cross compilation.");
  114. UsageError(" Example: --host-prefix=out/target/product/crespo");
  115. UsageError(" Default: $ANDROID_PRODUCT_OUT");
  116. UsageError("");
  117. UsageError(" --android-root=<path>: used to locate libraries for portable linking.");
  118. UsageError(" Example: --android-root=out/host/linux-x86");
  119. UsageError(" Default: $ANDROID_ROOT");
  120. UsageError("");
  121. UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction");
  122. UsageError(" set.");
  123. UsageError(" Example: --instruction-set=x86");
  124. UsageError(" Default: arm");
  125. UsageError("");
  126. UsageError(" --compiler-backend=(Quick|QuickGBC|Portable): select compiler backend");
  127. UsageError(" set.");
  128. UsageError(" Example: --instruction-set=Portable");
  129. UsageError(" Default: Quick");
  130. UsageError("");
  131. UsageError(" --host: used with Portable backend to link against host runtime libraries");
  132. UsageError("");
  133. UsageError(" --dump-timing: display a breakdown of where time was spent");
  134. UsageError("");
  135. UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
  136. UsageError(" such as initial heap size, maximum heap size, and verbose output.");
  137. UsageError(" Use a separate --runtime-arg switch for each argument.");
  138. UsageError(" Example: --runtime-arg -Xms256m");
  139. UsageError("");
  140. std::cerr << "See log for usage error information\n";
  141. exit(EXIT_FAILURE);
  142. }
  143. class Dex2Oat {
  144. public:
  145. static bool Create(Dex2Oat** p_dex2oat,
  146. Runtime::Options& options,
  147. CompilerBackend compiler_backend,
  148. InstructionSet instruction_set,
  149. size_t thread_count)
  150. SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
  151. if (!CreateRuntime(options, instruction_set)) {
  152. *p_dex2oat = NULL;
  153. return false;
  154. }
  155. *p_dex2oat = new Dex2Oat(Runtime::Current(), compiler_backend, instruction_set, thread_count);
  156. return true;
  157. }
  158. ~Dex2Oat() {
  159. delete runtime_;
  160. VLOG(compiler) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_)
  161. << " (threads: " << thread_count_ << ")";
  162. }
  163. // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;)
  164. CompilerDriver::DescriptorSet* ReadImageClassesFromFile(const char* image_classes_filename) {
  165. UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename,
  166. std::ifstream::in));
  167. if (image_classes_file.get() == NULL) {
  168. LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
  169. return NULL;
  170. }
  171. UniquePtr<CompilerDriver::DescriptorSet> result(ReadImageClasses(*image_classes_file.get()));
  172. image_classes_file->close();
  173. return result.release();
  174. }
  175. CompilerDriver::DescriptorSet* ReadImageClasses(std::istream& image_classes_stream) {
  176. UniquePtr<CompilerDriver::DescriptorSet> image_classes(new CompilerDriver::DescriptorSet);
  177. while (image_classes_stream.good()) {
  178. std::string dot;
  179. std::getline(image_classes_stream, dot);
  180. if (StartsWith(dot, "#") || dot.empty()) {
  181. continue;
  182. }
  183. std::string descriptor(DotToDescriptor(dot.c_str()));
  184. image_classes->insert(descriptor);
  185. }
  186. return image_classes.release();
  187. }
  188. // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;)
  189. CompilerDriver::DescriptorSet* ReadImageClassesFromZip(const std::string& zip_filename,
  190. const char* image_classes_filename) {
  191. UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(zip_filename));
  192. if (zip_archive.get() == NULL) {
  193. LOG(ERROR) << "Failed to open zip file " << zip_filename;
  194. return NULL;
  195. }
  196. UniquePtr<ZipEntry> zip_entry(zip_archive->Find(image_classes_filename));
  197. if (zip_entry.get() == NULL) {
  198. LOG(ERROR) << "Failed to find " << image_classes_filename << " within " << zip_filename;
  199. return NULL;
  200. }
  201. UniquePtr<MemMap> image_classes_file(zip_entry->ExtractToMemMap(image_classes_filename));
  202. if (image_classes_file.get() == NULL) {
  203. LOG(ERROR) << "Failed to extract " << image_classes_filename << " from " << zip_filename;
  204. return NULL;
  205. }
  206. const std::string image_classes_string(reinterpret_cast<char*>(image_classes_file->Begin()),
  207. image_classes_file->Size());
  208. std::istringstream image_classes_stream(image_classes_string);
  209. return ReadImageClasses(image_classes_stream);
  210. }
  211. const CompilerDriver* CreateOatFile(const std::string& boot_image_option,
  212. const std::string* host_prefix,
  213. const std::string& android_root,
  214. bool is_host,
  215. const std::vector<const DexFile*>& dex_files,
  216. File* oat_file,
  217. const std::string& bitcode_filename,
  218. bool image,
  219. UniquePtr<CompilerDriver::DescriptorSet>& image_classes,
  220. bool dump_stats,
  221. base::TimingLogger& timings) {
  222. // SirtRef and ClassLoader creation needs to come after Runtime::Create
  223. jobject class_loader = NULL;
  224. Thread* self = Thread::Current();
  225. if (!boot_image_option.empty()) {
  226. ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
  227. std::vector<const DexFile*> class_path_files(dex_files);
  228. OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
  229. ScopedObjectAccess soa(self);
  230. for (size_t i = 0; i < class_path_files.size(); i++) {
  231. class_linker->RegisterDexFile(*class_path_files[i]);
  232. }
  233. soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
  234. ScopedLocalRef<jobject> class_loader_local(soa.Env(),
  235. soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
  236. class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
  237. Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files);
  238. }
  239. UniquePtr<CompilerDriver> driver(new CompilerDriver(compiler_backend_,
  240. instruction_set_,
  241. image,
  242. image_classes.release(),
  243. thread_count_,
  244. dump_stats));
  245. if (compiler_backend_ == kPortable) {
  246. driver->SetBitcodeFileName(bitcode_filename);
  247. }
  248. driver->CompileAll(class_loader, dex_files, timings);
  249. timings.NewSplit("dex2oat OatWriter");
  250. std::string image_file_location;
  251. uint32_t image_file_location_oat_checksum = 0;
  252. uint32_t image_file_location_oat_data_begin = 0;
  253. if (!driver->IsImage()) {
  254. gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace();
  255. image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum();
  256. image_file_location_oat_data_begin =
  257. reinterpret_cast<uint32_t>(image_space->GetImageHeader().GetOatDataBegin());
  258. image_file_location = image_space->GetImageFilename();
  259. if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) {
  260. image_file_location = image_file_location.substr(host_prefix->size());
  261. }
  262. }
  263. OatWriter oat_writer(dex_files,
  264. image_file_location_oat_checksum,
  265. image_file_location_oat_data_begin,
  266. image_file_location,
  267. driver.get());
  268. if (!driver->WriteElf(android_root, is_host, dex_files, oat_writer, oat_file)) {
  269. LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath();
  270. return NULL;
  271. }
  272. return driver.release();
  273. }
  274. bool CreateImageFile(const std::string& image_filename,
  275. uintptr_t image_base,
  276. const std::string& oat_filename,
  277. const std::string& oat_location,
  278. const CompilerDriver& compiler)
  279. LOCKS_EXCLUDED(Locks::mutator_lock_) {
  280. uintptr_t oat_data_begin;
  281. {
  282. // ImageWriter is scoped so it can free memory before doing FixupElf
  283. ImageWriter image_writer(compiler);
  284. if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location)) {
  285. LOG(ERROR) << "Failed to create image file " << image_filename;
  286. return false;
  287. }
  288. oat_data_begin = image_writer.GetOatDataBegin();
  289. }
  290. UniquePtr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str()));
  291. if (oat_file.get() == NULL) {
  292. PLOG(ERROR) << "Failed to open ELF file: " << oat_filename;
  293. return false;
  294. }
  295. if (!ElfFixup::Fixup(oat_file.get(), oat_data_begin)) {
  296. LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath();
  297. return false;
  298. }
  299. return true;
  300. }
  301. private:
  302. explicit Dex2Oat(Runtime* runtime,
  303. CompilerBackend compiler_backend,
  304. InstructionSet instruction_set,
  305. size_t thread_count)
  306. : compiler_backend_(compiler_backend),
  307. instruction_set_(instruction_set),
  308. runtime_(runtime),
  309. thread_count_(thread_count),
  310. start_ns_(NanoTime()) {
  311. }
  312. static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set)
  313. SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
  314. if (!Runtime::Create(options, false)) {
  315. LOG(ERROR) << "Failed to create runtime";
  316. return false;
  317. }
  318. Runtime* runtime = Runtime::Current();
  319. // if we loaded an existing image, we will reuse values from the image roots.
  320. if (!runtime->HasResolutionMethod()) {
  321. runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
  322. }
  323. for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
  324. Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
  325. if (!runtime->HasCalleeSaveMethod(type)) {
  326. runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
  327. }
  328. }
  329. runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
  330. return true;
  331. }
  332. // Appends to dex_files any elements of class_path that it doesn't already
  333. // contain. This will open those dex files as necessary.
  334. static void OpenClassPathFiles(const std::string& class_path,
  335. std::vector<const DexFile*>& dex_files) {
  336. std::vector<std::string> parsed;
  337. Split(class_path, ':', parsed);
  338. // Take Locks::mutator_lock_ so that lock ordering on the ClassLinker::dex_lock_ is maintained.
  339. ScopedObjectAccess soa(Thread::Current());
  340. for (size_t i = 0; i < parsed.size(); ++i) {
  341. if (DexFilesContains(dex_files, parsed[i])) {
  342. continue;
  343. }
  344. const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
  345. if (dex_file == NULL) {
  346. LOG(WARNING) << "Failed to open dex file " << parsed[i];
  347. } else {
  348. dex_files.push_back(dex_file);
  349. }
  350. }
  351. }
  352. // Returns true if dex_files has a dex with the named location.
  353. static bool DexFilesContains(const std::vector<const DexFile*>& dex_files,
  354. const std::string& location) {
  355. for (size_t i = 0; i < dex_files.size(); ++i) {
  356. if (dex_files[i]->GetLocation() == location) {
  357. return true;
  358. }
  359. }
  360. return false;
  361. }
  362. const CompilerBackend compiler_backend_;
  363. const InstructionSet instruction_set_;
  364. Runtime* runtime_;
  365. size_t thread_count_;
  366. uint64_t start_ns_;
  367. DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
  368. };
  369. static bool ParseInt(const char* in, int* out) {
  370. char* end;
  371. int result = strtol(in, &end, 10);
  372. if (in == end || *end != '\0') {
  373. return false;
  374. }
  375. *out = result;
  376. return true;
  377. }
  378. static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames,
  379. const std::vector<const char*>& dex_locations,
  380. std::vector<const DexFile*>& dex_files) {
  381. size_t failure_count = 0;
  382. for (size_t i = 0; i < dex_filenames.size(); i++) {
  383. const char* dex_filename = dex_filenames[i];
  384. const char* dex_location = dex_locations[i];
  385. const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
  386. if (dex_file == NULL) {
  387. LOG(WARNING) << "Failed to open .dex from file '" << dex_filename << "'\n";
  388. ++failure_count;
  389. } else {
  390. dex_files.push_back(dex_file);
  391. }
  392. }
  393. return failure_count;
  394. }
  395. // The primary goal of the watchdog is to prevent stuck build servers
  396. // during development when fatal aborts lead to a cascade of failures
  397. // that result in a deadlock.
  398. class WatchDog {
  399. // WatchDog defines its own CHECK_PTHREAD_CALL to avoid using Log which uses locks
  400. #undef CHECK_PTHREAD_CALL
  401. #define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \
  402. do { \
  403. int rc = call args; \
  404. if (rc != 0) { \
  405. errno = rc; \
  406. std::string message(# call); \
  407. message += " failed for "; \
  408. message += reason; \
  409. Fatal(message); \
  410. } \
  411. } while (false)
  412. public:
  413. explicit WatchDog(bool is_watch_dog_enabled) {
  414. is_watch_dog_enabled_ = is_watch_dog_enabled;
  415. if (!is_watch_dog_enabled_) {
  416. return;
  417. }
  418. shutting_down_ = false;
  419. const char* reason = "dex2oat watch dog thread startup";
  420. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, NULL), reason);
  421. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, NULL), reason);
  422. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason);
  423. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason);
  424. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason);
  425. }
  426. ~WatchDog() {
  427. if (!is_watch_dog_enabled_) {
  428. return;
  429. }
  430. const char* reason = "dex2oat watch dog thread shutdown";
  431. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
  432. shutting_down_ = true;
  433. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason);
  434. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
  435. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, NULL), reason);
  436. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason);
  437. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason);
  438. }
  439. private:
  440. static void* CallBack(void* arg) {
  441. WatchDog* self = reinterpret_cast<WatchDog*>(arg);
  442. ::art::SetThreadName("dex2oat watch dog");
  443. self->Wait();
  444. return NULL;
  445. }
  446. static void Message(char severity, const std::string& message) {
  447. // TODO: Remove when we switch to LOG when we can guarantee it won't prevent shutdown in error
  448. // cases.
  449. fprintf(stderr, "dex2oat%s %c %d %d %s\n",
  450. kIsDebugBuild ? "d" : "",
  451. severity,
  452. getpid(),
  453. GetTid(),
  454. message.c_str());
  455. }
  456. static void Warn(const std::string& message) {
  457. Message('W', message);
  458. }
  459. static void Fatal(const std::string& message) {
  460. Message('F', message);
  461. exit(1);
  462. }
  463. void Wait() {
  464. bool warning = true;
  465. CHECK_GT(kWatchDogTimeoutSeconds, kWatchDogWarningSeconds);
  466. // TODO: tune the multiplier for GC verification, the following is just to make the timeout
  467. // large.
  468. int64_t multiplier = gc::kDesiredHeapVerification > gc::kVerifyAllFast ? 100 : 1;
  469. timespec warning_ts;
  470. InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogWarningSeconds * 1000, 0, &warning_ts);
  471. timespec timeout_ts;
  472. InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts);
  473. const char* reason = "dex2oat watch dog thread waiting";
  474. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
  475. while (!shutting_down_) {
  476. int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_,
  477. warning ? &warning_ts
  478. : &timeout_ts));
  479. if (rc == ETIMEDOUT) {
  480. std::string message(StringPrintf("dex2oat did not finish after %d seconds",
  481. warning ? kWatchDogWarningSeconds
  482. : kWatchDogTimeoutSeconds));
  483. if (warning) {
  484. Warn(message.c_str());
  485. warning = false;
  486. } else {
  487. Fatal(message.c_str());
  488. }
  489. } else if (rc != 0) {
  490. std::string message(StringPrintf("pthread_cond_timedwait failed: %s",
  491. strerror(errno)));
  492. Fatal(message.c_str());
  493. }
  494. }
  495. CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
  496. }
  497. // When setting timeouts, keep in mind that the build server may not be as fast as your desktop.
  498. #if ART_USE_PORTABLE_COMPILER
  499. static const unsigned int kWatchDogWarningSeconds = 2 * 60; // 2 minutes.
  500. static const unsigned int kWatchDogTimeoutSeconds = 30 * 60; // 25 minutes + buffer.
  501. #else
  502. static const unsigned int kWatchDogWarningSeconds = 1 * 60; // 1 minute.
  503. static const unsigned int kWatchDogTimeoutSeconds = 6 * 60; // 5 minutes + buffer.
  504. #endif
  505. bool is_watch_dog_enabled_;
  506. bool shutting_down_;
  507. // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases.
  508. pthread_mutex_t mutex_;
  509. pthread_cond_t cond_;
  510. pthread_attr_t attr_;
  511. pthread_t pthread_;
  512. };
  513. const unsigned int WatchDog::kWatchDogWarningSeconds;
  514. const unsigned int WatchDog::kWatchDogTimeoutSeconds;
  515. static int dex2oat(int argc, char** argv) {
  516. base::TimingLogger timings("compiler", false, false);
  517. InitLogging(argv);
  518. // Skip over argv[0].
  519. argv++;
  520. argc--;
  521. if (argc == 0) {
  522. Usage("No arguments specified");
  523. }
  524. std::vector<const char*> dex_filenames;
  525. std::vector<const char*> dex_locations;
  526. int zip_fd = -1;
  527. std::string zip_location;
  528. std::string oat_filename;
  529. std::string oat_symbols;
  530. std::string oat_location;
  531. int oat_fd = -1;
  532. std::string bitcode_filename;
  533. const char* image_classes_zip_filename = NULL;
  534. const char* image_classes_filename = NULL;
  535. std::string image_filename;
  536. std::string boot_image_filename;
  537. uintptr_t image_base = 0;
  538. UniquePtr<std::string> host_prefix;
  539. std::string android_root;
  540. std::vector<const char*> runtime_args;
  541. int thread_count = sysconf(_SC_NPROCESSORS_CONF);
  542. #if defined(ART_USE_PORTABLE_COMPILER)
  543. CompilerBackend compiler_backend = kPortable;
  544. #else
  545. CompilerBackend compiler_backend = kQuick;
  546. #endif
  547. #if defined(__arm__)
  548. InstructionSet instruction_set = kThumb2;
  549. #elif defined(__i386__)
  550. InstructionSet instruction_set = kX86;
  551. #elif defined(__mips__)
  552. InstructionSet instruction_set = kMips;
  553. #else
  554. #error "Unsupported architecture"
  555. #endif
  556. bool is_host = false;
  557. bool dump_stats = kIsDebugBuild;
  558. bool dump_timing = false;
  559. bool dump_slow_timing = kIsDebugBuild;
  560. bool watch_dog_enabled = !kIsTargetBuild;
  561. for (int i = 0; i < argc; i++) {
  562. const StringPiece option(argv[i]);
  563. bool log_options = false;
  564. if (log_options) {
  565. LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
  566. }
  567. if (option.starts_with("--dex-file=")) {
  568. dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
  569. } else if (option.starts_with("--dex-location=")) {
  570. dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
  571. } else if (option.starts_with("--zip-fd=")) {
  572. const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
  573. if (!ParseInt(zip_fd_str, &zip_fd)) {
  574. Usage("Failed to parse --zip-fd argument '%s' as an integer", zip_fd_str);
  575. }
  576. } else if (option.starts_with("--zip-location=")) {
  577. zip_location = option.substr(strlen("--zip-location=")).data();
  578. } else if (option.starts_with("--oat-file=")) {
  579. oat_filename = option.substr(strlen("--oat-file=")).data();
  580. } else if (option.starts_with("--oat-symbols=")) {
  581. oat_symbols = option.substr(strlen("--oat-symbols=")).data();
  582. } else if (option.starts_with("--oat-fd=")) {
  583. const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
  584. if (!ParseInt(oat_fd_str, &oat_fd)) {
  585. Usage("Failed to parse --oat-fd argument '%s' as an integer", oat_fd_str);
  586. }
  587. } else if (option == "--watch-dog") {
  588. watch_dog_enabled = true;
  589. } else if (option == "--no-watch-dog") {
  590. watch_dog_enabled = false;
  591. } else if (option.starts_with("-j")) {
  592. const char* thread_count_str = option.substr(strlen("-j")).data();
  593. if (!ParseInt(thread_count_str, &thread_count)) {
  594. Usage("Failed to parse -j argument '%s' as an integer", thread_count_str);
  595. }
  596. } else if (option.starts_with("--oat-location=")) {
  597. oat_location = option.substr(strlen("--oat-location=")).data();
  598. } else if (option.starts_with("--bitcode=")) {
  599. bitcode_filename = option.substr(strlen("--bitcode=")).data();
  600. } else if (option.starts_with("--image=")) {
  601. image_filename = option.substr(strlen("--image=")).data();
  602. } else if (option.starts_with("--image-classes=")) {
  603. image_classes_filename = option.substr(strlen("--image-classes=")).data();
  604. } else if (option.starts_with("--image-classes-zip=")) {
  605. image_classes_zip_filename = option.substr(strlen("--image-classes-zip=")).data();
  606. } else if (option.starts_with("--base=")) {
  607. const char* image_base_str = option.substr(strlen("--base=")).data();
  608. char* end;
  609. image_base = strtoul(image_base_str, &end, 16);
  610. if (end == image_base_str || *end != '\0') {
  611. Usage("Failed to parse hexadecimal value for option %s", option.data());
  612. }
  613. } else if (option.starts_with("--boot-image=")) {
  614. boot_image_filename = option.substr(strlen("--boot-image=")).data();
  615. } else if (option.starts_with("--host-prefix=")) {
  616. host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
  617. } else if (option.starts_with("--android-root=")) {
  618. android_root = option.substr(strlen("--android-root=")).data();
  619. } else if (option.starts_with("--instruction-set=")) {
  620. StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
  621. if (instruction_set_str == "arm") {
  622. instruction_set = kThumb2;
  623. } else if (instruction_set_str == "mips") {
  624. instruction_set = kMips;
  625. } else if (instruction_set_str == "x86") {
  626. instruction_set = kX86;
  627. }
  628. } else if (option.starts_with("--compiler-backend=")) {
  629. StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data();
  630. if (backend_str == "Quick") {
  631. compiler_backend = kQuick;
  632. } else if (backend_str == "Portable") {
  633. compiler_backend = kPortable;
  634. }
  635. } else if (option == "--host") {
  636. is_host = true;
  637. } else if (option == "--runtime-arg") {
  638. if (++i >= argc) {
  639. Usage("Missing required argument for --runtime-arg");
  640. }
  641. if (log_options) {
  642. LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
  643. }
  644. runtime_args.push_back(argv[i]);
  645. } else if (option == "--dump-timing") {
  646. dump_timing = true;
  647. } else {
  648. Usage("Unknown argument %s", option.data());
  649. }
  650. }
  651. if (oat_filename.empty() && oat_fd == -1) {
  652. Usage("Output must be supplied with either --oat-file or --oat-fd");
  653. }
  654. if (!oat_filename.empty() && oat_fd != -1) {
  655. Usage("--oat-file should not be used with --oat-fd");
  656. }
  657. if (!oat_symbols.empty() && oat_fd != -1) {
  658. Usage("--oat-symbols should not be used with --oat-fd");
  659. }
  660. if (!oat_symbols.empty() && is_host) {
  661. Usage("--oat-symbols should not be used with --host");
  662. }
  663. if (oat_fd != -1 && !image_filename.empty()) {
  664. Usage("--oat-fd should not be used with --image");
  665. }
  666. if (host_prefix.get() == NULL) {
  667. const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
  668. if (android_product_out != NULL) {
  669. host_prefix.reset(new std::string(android_product_out));
  670. }
  671. }
  672. if (android_root.empty()) {
  673. const char* android_root_env_var = getenv("ANDROID_ROOT");
  674. if (android_root_env_var == NULL) {
  675. Usage("--android-root unspecified and ANDROID_ROOT not set");
  676. }
  677. android_root += android_root_env_var;
  678. }
  679. bool image = (!image_filename.empty());
  680. if (!image && boot_image_filename.empty()) {
  681. if (host_prefix.get() == NULL) {
  682. boot_image_filename += GetAndroidRoot();
  683. } else {
  684. boot_image_filename += *host_prefix.get();
  685. boot_image_filename += "/system";
  686. }
  687. boot_image_filename += "/framework/boot.art";
  688. }
  689. std::string boot_image_option;
  690. if (!boot_image_filename.empty()) {
  691. boot_image_option += "-Ximage:";
  692. boot_image_option += boot_image_filename;
  693. }
  694. if (image_classes_filename != NULL && !image) {
  695. Usage("--image-classes should only be used with --image");
  696. }
  697. if (image_classes_filename != NULL && !boot_image_option.empty()) {
  698. Usage("--image-classes should not be used with --boot-image");
  699. }
  700. if (image_classes_zip_filename != NULL && image_classes_filename == NULL) {
  701. Usage("--image-classes-zip should be used with --image-classes");
  702. }
  703. if (dex_filenames.empty() && zip_fd == -1) {
  704. Usage("Input must be supplied with either --dex-file or --zip-fd");
  705. }
  706. if (!dex_filenames.empty() && zip_fd != -1) {
  707. Usage("--dex-file should not be used with --zip-fd");
  708. }
  709. if (!dex_filenames.empty() && !zip_location.empty()) {
  710. Usage("--dex-file should not be used with --zip-location");
  711. }
  712. if (dex_locations.empty()) {
  713. for (size_t i = 0; i < dex_filenames.size(); i++) {
  714. dex_locations.push_back(dex_filenames[i]);
  715. }
  716. } else if (dex_locations.size() != dex_filenames.size()) {
  717. Usage("--dex-location arguments do not match --dex-file arguments");
  718. }
  719. if (zip_fd != -1 && zip_location.empty()) {
  720. Usage("--zip-location should be supplied with --zip-fd");
  721. }
  722. if (boot_image_option.empty()) {
  723. if (image_base == 0) {
  724. Usage("Non-zero --base not specified");
  725. }
  726. }
  727. std::string oat_stripped(oat_filename);
  728. std::string oat_unstripped;
  729. if (!oat_symbols.empty()) {
  730. oat_unstripped += oat_symbols;
  731. } else {
  732. oat_unstripped += oat_filename;
  733. }
  734. // Done with usage checks, enable watchdog if requested
  735. WatchDog watch_dog(watch_dog_enabled);
  736. // Check early that the result of compilation can be written
  737. UniquePtr<File> oat_file;
  738. bool create_file = !oat_unstripped.empty(); // as opposed to using open file descriptor
  739. if (create_file) {
  740. oat_file.reset(OS::CreateEmptyFile(oat_unstripped.c_str()));
  741. if (oat_location.empty()) {
  742. oat_location = oat_filename;
  743. }
  744. } else {
  745. oat_file.reset(new File(oat_fd, oat_location));
  746. oat_file->DisableAutoClose();
  747. }
  748. if (oat_file.get() == NULL) {
  749. PLOG(ERROR) << "Failed to create oat file: " << oat_location;
  750. return EXIT_FAILURE;
  751. }
  752. if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
  753. PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
  754. return EXIT_FAILURE;
  755. }
  756. timings.StartSplit("dex2oat Setup");
  757. LOG(INFO) << "dex2oat: " << oat_location;
  758. if (image) {
  759. bool has_compiler_filter = false;
  760. for (const char* r : runtime_args) {
  761. if (strncmp(r, "-compiler-filter:", 17) == 0) {
  762. has_compiler_filter = true;
  763. break;
  764. }
  765. }
  766. if (!has_compiler_filter) {
  767. runtime_args.push_back("-compiler-filter:everything");
  768. }
  769. }
  770. Runtime::Options options;
  771. options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
  772. std::vector<const DexFile*> boot_class_path;
  773. if (boot_image_option.empty()) {
  774. size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
  775. if (failure_count > 0) {
  776. LOG(ERROR) << "Failed to open some dex files: " << failure_count;
  777. return EXIT_FAILURE;
  778. }
  779. options.push_back(std::make_pair("bootclasspath", &boot_class_path));
  780. } else {
  781. options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
  782. }
  783. if (host_prefix.get() != NULL) {
  784. options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
  785. }
  786. for (size_t i = 0; i < runtime_args.size(); i++) {
  787. options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
  788. }
  789. #ifdef ART_SEA_IR_MODE
  790. options.push_back(std::make_pair("-sea_ir", reinterpret_cast<void*>(NULL)));
  791. #endif
  792. Dex2Oat* p_dex2oat;
  793. if (!Dex2Oat::Create(&p_dex2oat, options, compiler_backend, instruction_set, thread_count)) {
  794. LOG(ERROR) << "Failed to create dex2oat";
  795. return EXIT_FAILURE;
  796. }
  797. UniquePtr<Dex2Oat> dex2oat(p_dex2oat);
  798. // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
  799. // give it away now so that we don't starve GC.
  800. Thread* self = Thread::Current();
  801. self->TransitionFromRunnableToSuspended(kNative);
  802. // If we're doing the image, override the compiler filter to force full compilation. Must be
  803. // done ahead of WellKnownClasses::Init that causes verification. Note: doesn't force
  804. // compilation of class initializers.
  805. // Whilst we're in native take the opportunity to initialize well known classes.
  806. WellKnownClasses::Init(self->GetJniEnv());
  807. // If --image-classes was specified, calculate the full list of classes to include in the image
  808. UniquePtr<CompilerDriver::DescriptorSet> image_classes(NULL);
  809. if (image_classes_filename != NULL) {
  810. if (image_classes_zip_filename != NULL) {
  811. image_classes.reset(dex2oat->ReadImageClassesFromZip(image_classes_zip_filename,
  812. image_classes_filename));
  813. } else {
  814. image_classes.reset(dex2oat->ReadImageClassesFromFile(image_classes_filename));
  815. }
  816. if (image_classes.get() == NULL) {
  817. LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
  818. return EXIT_FAILURE;
  819. }
  820. }
  821. std::vector<const DexFile*> dex_files;
  822. if (boot_image_option.empty()) {
  823. dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
  824. } else {
  825. if (dex_filenames.empty()) {
  826. UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
  827. if (zip_archive.get() == NULL) {
  828. LOG(ERROR) << "Failed to open zip from file descriptor for " << zip_location;
  829. return EXIT_FAILURE;
  830. }
  831. const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
  832. if (dex_file == NULL) {
  833. LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
  834. return EXIT_FAILURE;
  835. }
  836. dex_files.push_back(dex_file);
  837. } else {
  838. size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files);
  839. if (failure_count > 0) {
  840. LOG(ERROR) << "Failed to open some dex files: " << failure_count;
  841. return EXIT_FAILURE;
  842. }
  843. }
  844. // Ensure opened dex files are writable for dex-to-dex transformations.
  845. for (const auto& dex_file : dex_files) {
  846. if (!dex_file->EnableWrite()) {
  847. PLOG(ERROR) << "Failed to make .dex file writeable '" << dex_file->GetLocation() << "'\n";
  848. }
  849. }
  850. }
  851. /*
  852. * If we're not in interpret-only mode, go ahead and compile small applications. Don't
  853. * bother to check if we're doing the image.
  854. */
  855. if (!image && (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly)) {
  856. size_t num_methods = 0;
  857. for (size_t i = 0; i != dex_files.size(); ++i) {
  858. const DexFile* dex_file = dex_files[i];
  859. CHECK(dex_file != NULL);
  860. num_methods += dex_file->NumMethodIds();
  861. }
  862. if (num_methods <= Runtime::Current()->GetNumDexMethodsThreshold()) {
  863. Runtime::Current()->SetCompilerFilter(Runtime::kSpeed);
  864. VLOG(compiler) << "Below method threshold, compiling anyways";
  865. }
  866. }
  867. UniquePtr<const CompilerDriver> compiler(dex2oat->CreateOatFile(boot_image_option,
  868. host_prefix.get(),
  869. android_root,
  870. is_host,
  871. dex_files,
  872. oat_file.get(),
  873. bitcode_filename,
  874. image,
  875. image_classes,
  876. dump_stats,
  877. timings));
  878. if (compiler.get() == NULL) {
  879. LOG(ERROR) << "Failed to create oat file: " << oat_location;
  880. return EXIT_FAILURE;
  881. }
  882. VLOG(compiler) << "Oat file written successfully (unstripped): " << oat_location;
  883. // Notes on the interleaving of creating the image and oat file to
  884. // ensure the references between the two are correct.
  885. //
  886. // Currently we have a memory layout that looks something like this:
  887. //
  888. // +--------------+
  889. // | image |
  890. // +--------------+
  891. // | boot oat |
  892. // +--------------+
  893. // | alloc spaces |
  894. // +--------------+
  895. //
  896. // There are several constraints on the loading of the image and boot.oat.
  897. //
  898. // 1. The image is expected to be loaded at an absolute address and
  899. // contains Objects with absolute pointers within the image.
  900. //
  901. // 2. There are absolute pointers from Methods in the image to their
  902. // code in the oat.
  903. //
  904. // 3. There are absolute pointers from the code in the oat to Methods
  905. // in the image.
  906. //
  907. // 4. There are absolute pointers from code in the oat to other code
  908. // in the oat.
  909. //
  910. // To get this all correct, we go through several steps.
  911. //
  912. // 1. We have already created that oat file above with
  913. // CreateOatFile. Originally this was just our own proprietary file
  914. // but now it is contained within an ELF dynamic object (aka an .so
  915. // file). The Compiler returned by CreateOatFile provides
  916. // PatchInformation for references to oat code and Methods that need
  917. // to be update once we know where the oat file will be located
  918. // after the image.
  919. //
  920. // 2. We create the image file. It needs to know where the oat file
  921. // will be loaded after itself. Originally when oat file was simply
  922. // memory mapped so we could predict where its contents were based
  923. // on the file size. Now that it is an ELF file, we need to inspect
  924. // the ELF file to understand the in memory segment layout including
  925. // where the oat header is located within. ImageWriter's
  926. // PatchOatCodeAndMethods uses the PatchInformation from the
  927. // Compiler to touch up absolute references in the oat file.
  928. //
  929. // 3. We fixup the ELF program headers so that dlopen will try to
  930. // load the .so at the desired location at runtime by offsetting the
  931. // Elf32_Phdr.p_vaddr values by the desired base address.
  932. //
  933. if (image) {
  934. timings.NewSplit("dex2oat ImageWriter");
  935. bool image_creation_success = dex2oat->CreateImageFile(image_filename,
  936. image_base,
  937. oat_unstripped,
  938. oat_location,
  939. *compiler.get());
  940. if (!image_creation_success) {
  941. return EXIT_FAILURE;
  942. }
  943. VLOG(compiler) << "Image written successfully: " << image_filename;
  944. }
  945. if (is_host) {
  946. if (dump_timing || (dump_slow_timing && timings.GetTotalNs() > MsToNs(1000))) {
  947. LOG(INFO) << Dumpable<base::TimingLogger>(timings);
  948. }
  949. return EXIT_SUCCESS;
  950. }
  951. // If we don't want to strip in place, copy from unstripped location to stripped location.
  952. // We need to strip after image creation because FixupElf needs to use .strtab.
  953. if (oat_unstripped != oat_stripped) {
  954. timings.NewSplit("dex2oat OatFile copy");
  955. oat_file.reset();
  956. UniquePtr<File> in(OS::OpenFileForReading(oat_unstripped.c_str()));
  957. UniquePtr<File> out(OS::CreateEmptyFile(oat_stripped.c_str()));
  958. size_t buffer_size = 8192;
  959. UniquePtr<uint8_t> buffer(new uint8_t[buffer_size]);
  960. while (true) {
  961. int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size));
  962. if (bytes_read <= 0) {
  963. break;
  964. }
  965. bool write_ok = out->WriteFully(buffer.get(), bytes_read);
  966. CHECK(write_ok);
  967. }
  968. oat_file.reset(out.release());
  969. VLOG(compiler) << "Oat file copied successfully (stripped): " << oat_stripped;
  970. }
  971. #if ART_USE_PORTABLE_COMPILER // We currently only generate symbols on Portable
  972. timings.NewSplit("dex2oat ElfStripper");
  973. // Strip unneeded sections for target
  974. off_t seek_actual = lseek(oat_file->Fd(), 0, SEEK_SET);
  975. CHECK_EQ(0, seek_actual);
  976. ElfStripper::Strip(oat_file.get());
  977. // We wrote the oat file successfully, and want to keep it.
  978. VLOG(compiler) << "Oat file written successfully (stripped): " << oat_location;
  979. #endif // ART_USE_PORTABLE_COMPILER
  980. timings.EndSplit();
  981. if (dump_timing || (dump_slow_timing && timings.GetTotalNs() > MsToNs(1000))) {
  982. LOG(INFO) << Dumpable<base::TimingLogger>(timings);
  983. }
  984. // Everything was successfully written, do an explicit exit here to avoid running Runtime
  985. // destructors that take time (bug 10645725) unless we're a debug build or running on valgrind.
  986. if (!kIsDebugBuild || (RUNNING_ON_VALGRIND == 0)) {
  987. exit(EXIT_SUCCESS);
  988. }
  989. return EXIT_SUCCESS;
  990. }
  991. } // namespace art
  992. int main(int argc, char** argv) {
  993. return art::dex2oat(argc, argv);
  994. }