Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | #include <string> |
| 18 | |
Andreas Gampe | 39b378c | 2017-12-07 15:44:13 -0800 | [diff] [blame] | 19 | #include "base/logging.h" // For InitLogging. |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 20 | #include "base/mutex.h" |
| 21 | #include "base/os.h" |
| 22 | #include "base/utils.h" |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
| 24 | #include "android-base/strings.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 25 | #include "base/file_utils.h" |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 26 | #include "compiler_filter.h" |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 27 | #include "class_loader_context.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 28 | #include "dex/dex_file.h" |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 29 | #include "noop_compiler_callbacks.h" |
| 30 | #include "oat_file_assistant.h" |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 31 | #include "runtime.h" |
| 32 | #include "thread-inl.h" |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
| 36 | // See OatFileAssistant docs for the meaning of the valid return codes. |
| 37 | enum ReturnCodes { |
| 38 | kNoDexOptNeeded = 0, |
| 39 | kDex2OatFromScratch = 1, |
| 40 | kDex2OatForBootImageOat = 2, |
| 41 | kDex2OatForFilterOat = 3, |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame^] | 42 | kDex2OatForBootImageOdex = 4, |
| 43 | kDex2OatForFilterOdex = 5, |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 44 | |
| 45 | kErrorInvalidArguments = 101, |
| 46 | kErrorCannotCreateRuntime = 102, |
| 47 | kErrorUnknownDexOptNeeded = 103 |
| 48 | }; |
| 49 | |
| 50 | static int original_argc; |
| 51 | static char** original_argv; |
| 52 | |
| 53 | static std::string CommandLine() { |
| 54 | std::vector<std::string> command; |
| 55 | for (int i = 0; i < original_argc; ++i) { |
| 56 | command.push_back(original_argv[i]); |
| 57 | } |
| 58 | return android::base::Join(command, ' '); |
| 59 | } |
| 60 | |
| 61 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 62 | std::string error; |
| 63 | android::base::StringAppendV(&error, fmt, ap); |
| 64 | LOG(ERROR) << error; |
| 65 | } |
| 66 | |
| 67 | static void UsageError(const char* fmt, ...) { |
| 68 | va_list ap; |
| 69 | va_start(ap, fmt); |
| 70 | UsageErrorV(fmt, ap); |
| 71 | va_end(ap); |
| 72 | } |
| 73 | |
| 74 | NO_RETURN static void Usage(const char *fmt, ...) { |
| 75 | va_list ap; |
| 76 | va_start(ap, fmt); |
| 77 | UsageErrorV(fmt, ap); |
| 78 | va_end(ap); |
| 79 | |
| 80 | UsageError("Command: %s", CommandLine().c_str()); |
| 81 | UsageError(" Performs a dexopt analysis on the given dex file and returns whether or not"); |
| 82 | UsageError(" the dex file needs to be dexopted."); |
| 83 | UsageError("Usage: dexoptanalyzer [options]..."); |
| 84 | UsageError(""); |
| 85 | UsageError(" --dex-file=<filename>: the dex file which should be analyzed."); |
| 86 | UsageError(""); |
| 87 | UsageError(" --isa=<string>: the instruction set for which the analysis should be performed."); |
| 88 | UsageError(""); |
| 89 | UsageError(" --compiler-filter=<string>: the target compiler filter to be used as reference"); |
| 90 | UsageError(" when deciding if the dex file needs to be optimized."); |
| 91 | UsageError(""); |
| 92 | UsageError(" --assume-profile-changed: assumes the profile information has changed"); |
| 93 | UsageError(" when deciding if the dex file needs to be optimized."); |
| 94 | UsageError(""); |
| 95 | UsageError(" --image=<filename>: optional, the image to be used to decide if the associated"); |
| 96 | UsageError(" oat file is up to date. Defaults to $ANDROID_ROOT/framework/boot.art."); |
| 97 | UsageError(" Example: --image=/system/framework/boot.art"); |
| 98 | UsageError(""); |
| 99 | UsageError(" --android-data=<directory>: optional, the directory which should be used as"); |
| 100 | UsageError(" android-data. By default ANDROID_DATA env variable is used."); |
| 101 | UsageError(""); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 102 | UsageError(" --oat-fd=number: file descriptor of the oat file which should be analyzed"); |
| 103 | UsageError(""); |
| 104 | UsageError(" --vdex-fd=number: file descriptor of the vdex file corresponding to the oat file"); |
| 105 | UsageError(""); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 106 | UsageError(" --zip-fd=number: specifies a file descriptor corresponding to the dex file."); |
| 107 | UsageError(""); |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 108 | UsageError(" --downgrade: optional, if the purpose of dexopt is to downgrade the dex file"); |
| 109 | UsageError(" By default, dexopt considers upgrade case."); |
| 110 | UsageError(""); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 111 | UsageError("Return code:"); |
| 112 | UsageError(" To make it easier to integrate with the internal tools this command will make"); |
| 113 | UsageError(" available its result (dexoptNeeded) as the exit/return code. i.e. it will not"); |
| 114 | UsageError(" return 0 for success and a non zero values for errors as the conventional"); |
| 115 | UsageError(" commands. The following return codes are possible:"); |
| 116 | UsageError(" kNoDexOptNeeded = 0"); |
| 117 | UsageError(" kDex2OatFromScratch = 1"); |
| 118 | UsageError(" kDex2OatForBootImageOat = 2"); |
| 119 | UsageError(" kDex2OatForFilterOat = 3"); |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame^] | 120 | UsageError(" kDex2OatForBootImageOdex = 4"); |
| 121 | UsageError(" kDex2OatForFilterOdex = 5"); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 122 | |
| 123 | UsageError(" kErrorInvalidArguments = 101"); |
| 124 | UsageError(" kErrorCannotCreateRuntime = 102"); |
| 125 | UsageError(" kErrorUnknownDexOptNeeded = 103"); |
| 126 | UsageError(""); |
| 127 | |
| 128 | exit(kErrorInvalidArguments); |
| 129 | } |
| 130 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 131 | class DexoptAnalyzer final { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 132 | public: |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 133 | DexoptAnalyzer() : |
| 134 | assume_profile_changed_(false), |
| 135 | downgrade_(false) {} |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 136 | |
| 137 | void ParseArgs(int argc, char **argv) { |
| 138 | original_argc = argc; |
| 139 | original_argv = argv; |
| 140 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 141 | Locks::Init(); |
Andreas Gampe | 51d80cc | 2017-06-21 21:05:13 -0700 | [diff] [blame] | 142 | InitLogging(argv, Runtime::Abort); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 143 | // Skip over the command name. |
| 144 | argv++; |
| 145 | argc--; |
| 146 | |
| 147 | if (argc == 0) { |
| 148 | Usage("No arguments specified"); |
| 149 | } |
| 150 | |
| 151 | for (int i = 0; i < argc; ++i) { |
| 152 | const StringPiece option(argv[i]); |
| 153 | if (option == "--assume-profile-changed") { |
| 154 | assume_profile_changed_ = true; |
| 155 | } else if (option.starts_with("--dex-file=")) { |
| 156 | dex_file_ = option.substr(strlen("--dex-file=")).ToString(); |
| 157 | } else if (option.starts_with("--compiler-filter=")) { |
| 158 | std::string filter_str = option.substr(strlen("--compiler-filter=")).ToString(); |
| 159 | if (!CompilerFilter::ParseCompilerFilter(filter_str.c_str(), &compiler_filter_)) { |
| 160 | Usage("Invalid compiler filter '%s'", option.data()); |
| 161 | } |
| 162 | } else if (option.starts_with("--isa=")) { |
| 163 | std::string isa_str = option.substr(strlen("--isa=")).ToString(); |
| 164 | isa_ = GetInstructionSetFromString(isa_str.c_str()); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 165 | if (isa_ == InstructionSet::kNone) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 166 | Usage("Invalid isa '%s'", option.data()); |
| 167 | } |
| 168 | } else if (option.starts_with("--image=")) { |
| 169 | image_ = option.substr(strlen("--image=")).ToString(); |
| 170 | } else if (option.starts_with("--android-data=")) { |
| 171 | // Overwrite android-data if needed (oat file assistant relies on a valid directory to |
| 172 | // compute dalvik-cache folder). This is mostly used in tests. |
| 173 | std::string new_android_data = option.substr(strlen("--android-data=")).ToString(); |
| 174 | setenv("ANDROID_DATA", new_android_data.c_str(), 1); |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 175 | } else if (option.starts_with("--downgrade")) { |
| 176 | downgrade_ = true; |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 177 | } else if (option.starts_with("--oat-fd")) { |
| 178 | oat_fd_ = std::stoi(option.substr(strlen("--oat-fd=")).ToString(), nullptr, 0); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 179 | if (oat_fd_ < 0) { |
| 180 | Usage("Invalid --oat-fd %d", oat_fd_); |
| 181 | } |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 182 | } else if (option.starts_with("--vdex-fd")) { |
| 183 | vdex_fd_ = std::stoi(option.substr(strlen("--vdex-fd=")).ToString(), nullptr, 0); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 184 | if (vdex_fd_ < 0) { |
| 185 | Usage("Invalid --vdex-fd %d", vdex_fd_); |
| 186 | } |
| 187 | } else if (option.starts_with("--zip-fd")) { |
| 188 | zip_fd_ = std::stoi(option.substr(strlen("--zip-fd=")).ToString(), nullptr, 0); |
| 189 | if (zip_fd_ < 0) { |
| 190 | Usage("Invalid --zip-fd %d", zip_fd_); |
| 191 | } |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 192 | } else if (option.starts_with("--class-loader-context=")) { |
| 193 | std::string context_str = option.substr(strlen("--class-loader-context=")).ToString(); |
| 194 | class_loader_context_ = ClassLoaderContext::Create(context_str); |
| 195 | if (class_loader_context_ == nullptr) { |
| 196 | Usage("Invalid --class-loader-context '%s'", context_str.c_str()); |
| 197 | } |
| 198 | } else { |
| 199 | Usage("Unknown argument '%s'", option.data()); |
| 200 | } |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if (image_.empty()) { |
| 204 | // If we don't receive the image, try to use the default one. |
| 205 | // Tests may specify a different image (e.g. core image). |
| 206 | std::string error_msg; |
| 207 | image_ = GetDefaultBootImageLocation(&error_msg); |
| 208 | |
| 209 | if (image_.empty()) { |
| 210 | LOG(ERROR) << error_msg; |
| 211 | Usage("--image unspecified and ANDROID_ROOT not set or image file does not exist."); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | bool CreateRuntime() { |
| 217 | RuntimeOptions options; |
| 218 | // The image could be custom, so make sure we explicitly pass it. |
| 219 | std::string img = "-Ximage:" + image_; |
| 220 | options.push_back(std::make_pair(img.c_str(), nullptr)); |
| 221 | // The instruction set of the image should match the instruction set we will test. |
| 222 | const void* isa_opt = reinterpret_cast<const void*>(GetInstructionSetString(isa_)); |
| 223 | options.push_back(std::make_pair("imageinstructionset", isa_opt)); |
| 224 | // Disable libsigchain. We don't don't need it to evaluate DexOptNeeded status. |
| 225 | options.push_back(std::make_pair("-Xno-sig-chain", nullptr)); |
| 226 | // Pretend we are a compiler so that we can re-use the same infrastructure to load a different |
| 227 | // ISA image and minimize the amount of things that get started. |
| 228 | NoopCompilerCallbacks callbacks; |
| 229 | options.push_back(std::make_pair("compilercallbacks", &callbacks)); |
| 230 | // Make sure we don't attempt to relocate. The tool should only retrieve the DexOptNeeded |
| 231 | // status and not attempt to relocate the boot image. |
| 232 | options.push_back(std::make_pair("-Xnorelocate", nullptr)); |
| 233 | |
| 234 | if (!Runtime::Create(options, false)) { |
| 235 | LOG(ERROR) << "Unable to initialize runtime"; |
| 236 | return false; |
| 237 | } |
| 238 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we |
| 239 | // Runtime::Start. Give it away now. |
| 240 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 241 | |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | int GetDexOptNeeded() { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 246 | if (!CreateRuntime()) { |
| 247 | return kErrorCannotCreateRuntime; |
| 248 | } |
Andreas Gampe | 39f44b7 | 2017-04-26 22:00:04 -0700 | [diff] [blame] | 249 | std::unique_ptr<Runtime> runtime(Runtime::Current()); |
| 250 | |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 251 | std::unique_ptr<OatFileAssistant> oat_file_assistant; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 252 | oat_file_assistant = std::make_unique<OatFileAssistant>(dex_file_.c_str(), |
| 253 | isa_, |
| 254 | false /*load_executable*/, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 255 | false /*only_load_system_executable*/, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 256 | vdex_fd_, |
| 257 | oat_fd_, |
| 258 | zip_fd_); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 259 | // Always treat elements of the bootclasspath as up-to-date. |
| 260 | // TODO(calin): this check should be in OatFileAssistant. |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 261 | if (oat_file_assistant->IsInBootClassPath()) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 262 | return kNoDexOptNeeded; |
| 263 | } |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 264 | |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 265 | int dexoptNeeded = oat_file_assistant->GetDexOptNeeded( |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 266 | compiler_filter_, assume_profile_changed_, downgrade_, class_loader_context_.get()); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 267 | |
| 268 | // Convert OatFileAssitant codes to dexoptanalyzer codes. |
| 269 | switch (dexoptNeeded) { |
| 270 | case OatFileAssistant::kNoDexOptNeeded: return kNoDexOptNeeded; |
| 271 | case OatFileAssistant::kDex2OatFromScratch: return kDex2OatFromScratch; |
| 272 | case OatFileAssistant::kDex2OatForBootImage: return kDex2OatForBootImageOat; |
| 273 | case OatFileAssistant::kDex2OatForFilter: return kDex2OatForFilterOat; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 274 | |
| 275 | case -OatFileAssistant::kDex2OatForBootImage: return kDex2OatForBootImageOdex; |
| 276 | case -OatFileAssistant::kDex2OatForFilter: return kDex2OatForFilterOdex; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 277 | default: |
| 278 | LOG(ERROR) << "Unknown dexoptNeeded " << dexoptNeeded; |
| 279 | return kErrorUnknownDexOptNeeded; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | private: |
| 284 | std::string dex_file_; |
| 285 | InstructionSet isa_; |
| 286 | CompilerFilter::Filter compiler_filter_; |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 287 | std::unique_ptr<ClassLoaderContext> class_loader_context_; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 288 | bool assume_profile_changed_; |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 289 | bool downgrade_; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 290 | std::string image_; |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 291 | int oat_fd_ = -1; |
| 292 | int vdex_fd_ = -1; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 293 | // File descriptor corresponding to apk, dex_file, or zip. |
| 294 | int zip_fd_ = -1; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | static int dexoptAnalyze(int argc, char** argv) { |
| 298 | DexoptAnalyzer analyzer; |
| 299 | |
| 300 | // Parse arguments. Argument mistakes will lead to exit(kErrorInvalidArguments) in UsageError. |
| 301 | analyzer.ParseArgs(argc, argv); |
| 302 | return analyzer.GetDexOptNeeded(); |
| 303 | } |
| 304 | |
| 305 | } // namespace art |
| 306 | |
| 307 | int main(int argc, char **argv) { |
| 308 | return art::dexoptAnalyze(argc, argv); |
| 309 | } |