• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 <sys/stat.h>
18 
19 #include <string>
20 #include <string_view>
21 #include <unordered_map>
22 
23 #include "android-base/properties.h"
24 #include "android-base/stringprintf.h"
25 #include "android-base/strings.h"
26 #include "arch/instruction_set.h"
27 #include "base/file_utils.h"
28 #include "base/globals.h"
29 #include "base/stl_util.h"
30 #include "odr_common.h"
31 #include "odr_compilation_log.h"
32 #include "odr_config.h"
33 #include "odr_metrics.h"
34 #include "odrefresh.h"
35 #include "odrefresh/odrefresh.h"
36 
37 namespace {
38 
39 using ::android::base::GetProperty;
40 using ::android::base::StartsWith;
41 using ::art::odrefresh::CompilationOptions;
42 using ::art::odrefresh::ExitCode;
43 using ::art::odrefresh::kCheckedSystemPropertyPrefixes;
44 using ::art::odrefresh::kIgnoredSystemProperties;
45 using ::art::odrefresh::kSystemProperties;
46 using ::art::odrefresh::kSystemPropertySystemServerCompilerFilterOverride;
47 using ::art::odrefresh::OdrCompilationLog;
48 using ::art::odrefresh::OdrConfig;
49 using ::art::odrefresh::OdrMetrics;
50 using ::art::odrefresh::OnDeviceRefresh;
51 using ::art::odrefresh::QuotePath;
52 using ::art::odrefresh::ShouldDisablePartialCompilation;
53 using ::art::odrefresh::ShouldDisableRefresh;
54 using ::art::odrefresh::SystemPropertyConfig;
55 using ::art::odrefresh::SystemPropertyForeach;
56 using ::art::odrefresh::ZygoteKind;
57 
UsageMsgV(const char * fmt,va_list ap)58 void UsageMsgV(const char* fmt, va_list ap) {
59   std::string error;
60   android::base::StringAppendV(&error, fmt, ap);
61   if (isatty(fileno(stderr))) {
62     std::cerr << error << std::endl;
63   } else {
64     LOG(ERROR) << error;
65   }
66 }
67 
UsageMsg(const char * fmt,...)68 void UsageMsg(const char* fmt, ...) {
69   va_list ap;
70   va_start(ap, fmt);
71   UsageMsgV(fmt, ap);
72   va_end(ap);
73 }
74 
ArgumentError(const char * fmt,...)75 NO_RETURN void ArgumentError(const char* fmt, ...) {
76   va_list ap;
77   va_start(ap, fmt);
78   UsageMsgV(fmt, ap);
79   va_end(ap);
80   UsageMsg("Try '--help' for more information.");
81   exit(EX_USAGE);
82 }
83 
ParseZygoteKind(const char * input,ZygoteKind * zygote_kind)84 bool ParseZygoteKind(const char* input, ZygoteKind* zygote_kind) {
85   std::string_view z(input);
86   if (z == "zygote32") {
87     *zygote_kind = ZygoteKind::kZygote32;
88     return true;
89   } else if (z == "zygote32_64") {
90     *zygote_kind = ZygoteKind::kZygote32_64;
91     return true;
92   } else if (z == "zygote64_32") {
93     *zygote_kind = ZygoteKind::kZygote64_32;
94     return true;
95   } else if (z == "zygote64") {
96     *zygote_kind = ZygoteKind::kZygote64;
97     return true;
98   }
99   return false;
100 }
101 
GetEnvironmentVariableOrDie(const char * name)102 std::string GetEnvironmentVariableOrDie(const char* name) {
103   const char* value = getenv(name);
104   LOG_ALWAYS_FATAL_IF(value == nullptr, "%s is not defined.", name);
105   return value;
106 }
107 
GetEnvironmentVariableOrDefault(const char * name,std::string default_value)108 std::string GetEnvironmentVariableOrDefault(const char* name, std::string default_value) {
109   const char* value = getenv(name);
110   if (value == nullptr) {
111     return default_value;
112   }
113   return value;
114 }
115 
ArgumentMatches(std::string_view argument,std::string_view prefix,std::string * value)116 bool ArgumentMatches(std::string_view argument, std::string_view prefix, std::string* value) {
117   if (android::base::StartsWith(argument, prefix)) {
118     *value = std::string(argument.substr(prefix.size()));
119     return true;
120   }
121   return false;
122 }
123 
ArgumentEquals(std::string_view argument,std::string_view expected)124 bool ArgumentEquals(std::string_view argument, std::string_view expected) {
125   return argument == expected;
126 }
127 
InitializeConfig(int argc,char ** argv,OdrConfig * config)128 int InitializeConfig(int argc, char** argv, OdrConfig* config) {
129   config->SetApexInfoListFile("/apex/apex-info-list.xml");
130   config->SetArtBinDir(art::GetArtBinDir());
131   config->SetBootClasspath(GetEnvironmentVariableOrDie("BOOTCLASSPATH"));
132   config->SetDex2oatBootclasspath(GetEnvironmentVariableOrDie("DEX2OATBOOTCLASSPATH"));
133   config->SetSystemServerClasspath(GetEnvironmentVariableOrDie("SYSTEMSERVERCLASSPATH"));
134   config->SetStandaloneSystemServerJars(
135       GetEnvironmentVariableOrDefault("STANDALONE_SYSTEMSERVER_JARS", /*default_value=*/""));
136   config->SetIsa(art::kRuntimeISA);
137 
138   std::string zygote;
139   int n = 1;
140   for (; n < argc - 1; ++n) {
141     const char* arg = argv[n];
142     std::string value;
143     if (ArgumentEquals(arg, "--compilation-os-mode")) {
144       config->SetCompilationOsMode(true);
145     } else if (ArgumentMatches(arg, "--dalvik-cache=", &value)) {
146       art::OverrideDalvikCacheSubDirectory(value);
147       config->SetArtifactDirectory(GetApexDataDalvikCacheDirectory(art::InstructionSet::kNone));
148     } else if (ArgumentMatches(arg, "--zygote-arch=", &value)) {
149       zygote = value;
150     } else if (ArgumentMatches(arg, "--boot-image-compiler-filter=", &value)) {
151       config->SetBootImageCompilerFilter(value);
152     } else if (ArgumentMatches(arg, "--system-server-compiler-filter=", &value)) {
153       config->SetSystemServerCompilerFilter(value);
154     } else if (ArgumentMatches(arg, "--staging-dir=", &value)) {
155       config->SetStagingDir(value);
156     } else if (ArgumentEquals(arg, "--dry-run")) {
157       config->SetDryRun();
158     } else if (ArgumentEquals(arg, "--partial-compilation")) {
159       config->SetPartialCompilation(true);
160     } else if (ArgumentEquals(arg, "--no-refresh")) {
161       config->SetRefresh(false);
162     } else if (ArgumentEquals(arg, "--minimal")) {
163       config->SetMinimal(true);
164     } else {
165       ArgumentError("Unrecognized argument: '%s'", arg);
166     }
167   }
168 
169   if (zygote.empty()) {
170     // Use ro.zygote by default, if not overridden by --zygote-arch flag.
171     zygote = GetProperty("ro.zygote", {});
172   }
173   ZygoteKind zygote_kind;
174   if (!ParseZygoteKind(zygote.c_str(), &zygote_kind)) {
175     LOG(FATAL) << "Unknown zygote: " << QuotePath(zygote);
176   }
177   config->SetZygoteKind(zygote_kind);
178 
179   if (config->GetSystemServerCompilerFilter().empty()) {
180     std::string filter = GetProperty("dalvik.vm.systemservercompilerfilter", "");
181     filter = GetProperty(kSystemPropertySystemServerCompilerFilterOverride, filter);
182     config->SetSystemServerCompilerFilter(filter);
183   }
184 
185   if (!config->HasPartialCompilation() &&
186       ShouldDisablePartialCompilation(
187           GetProperty("ro.build.version.security_patch", /*default_value=*/""))) {
188     config->SetPartialCompilation(false);
189   }
190 
191   if (ShouldDisableRefresh(GetProperty("ro.build.version.sdk", /*default_value=*/""))) {
192     config->SetRefresh(false);
193   }
194 
195   return n;
196 }
197 
GetSystemProperties(std::unordered_map<std::string,std::string> * system_properties)198 void GetSystemProperties(std::unordered_map<std::string, std::string>* system_properties) {
199   SystemPropertyForeach([&](const char* name, const char* value) {
200     if (strlen(value) == 0) {
201       return;
202     }
203     for (const char* prefix : kCheckedSystemPropertyPrefixes) {
204       if (StartsWith(name, prefix) && !art::ContainsElement(kIgnoredSystemProperties, name)) {
205         (*system_properties)[name] = value;
206       }
207     }
208   });
209   for (const SystemPropertyConfig& system_property_config : *kSystemProperties.get()) {
210     (*system_properties)[system_property_config.name] =
211         GetProperty(system_property_config.name, system_property_config.default_value);
212   }
213 }
214 
UsageHelp(const char * argv0)215 NO_RETURN void UsageHelp(const char* argv0) {
216   std::string name(android::base::Basename(argv0));
217   UsageMsg("Usage: %s [OPTION...] ACTION", name.c_str());
218   UsageMsg("On-device refresh tool for boot classpath and system server");
219   UsageMsg("following an update of the ART APEX.");
220   UsageMsg("");
221   UsageMsg("Valid ACTION choices are:");
222   UsageMsg("");
223   UsageMsg("--check          Check compilation artifacts are up-to-date based on metadata.");
224   UsageMsg("--compile        Compile boot classpath and system_server jars when necessary.");
225   UsageMsg("--force-compile  Unconditionally compile the bootclass path and system_server jars.");
226   UsageMsg("--help           Display this help information.");
227   UsageMsg("");
228   UsageMsg("Available OPTIONs are:");
229   UsageMsg("");
230   UsageMsg("--dry-run");
231   UsageMsg("--partial-compilation            Only generate artifacts that are out-of-date or");
232   UsageMsg("                                 missing.");
233   UsageMsg("--no-refresh                     Do not refresh existing artifacts.");
234   UsageMsg("--compilation-os-mode            Indicate that odrefresh is running in Compilation");
235   UsageMsg("                                 OS.");
236   UsageMsg("--dalvik-cache=<DIR>             Write artifacts to .../<DIR> rather than");
237   UsageMsg("                                 .../dalvik-cache");
238   UsageMsg("--staging-dir=<DIR>              Write temporary artifacts to <DIR> rather than");
239   UsageMsg("                                 .../staging");
240   UsageMsg("--zygote-arch=<STRING>           Zygote kind that overrides ro.zygote");
241   UsageMsg("--boot-image-compiler-filter=<STRING>");
242   UsageMsg("                                 Compiler filter for the boot image. Default: ");
243   UsageMsg("                                 speed-profile");
244   UsageMsg("--system-server-compiler-filter=<STRING>");
245   UsageMsg("                                 Compiler filter that overrides");
246   UsageMsg("                                 dalvik.vm.systemservercompilerfilter");
247   UsageMsg("--minimal                        Generate a minimal boot image only.");
248 
249   exit(EX_USAGE);
250 }
251 
252 }  // namespace
253 
main(int argc,char ** argv)254 int main(int argc, char** argv) {
255   // odrefresh is launched by `init` which sets the umask of forked processed to
256   // 077 (S_IRWXG | S_IRWXO). This blocks the ability to make files and directories readable
257   // by others and prevents system_server from loading generated artifacts.
258   umask(S_IWGRP | S_IWOTH);
259 
260   OdrConfig config(argv[0]);
261   int n = InitializeConfig(argc, argv, &config);
262 
263   // Explicitly initialize logging (b/201042799).
264   // But not in CompOS mode - logd doesn't exist in Microdroid (b/265153235).
265   if (!config.GetCompilationOsMode()) {
266     android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
267   }
268 
269   argv += n;
270   argc -= n;
271   if (argc != 1) {
272     ArgumentError("Expected 1 argument, but have %d.", argc);
273   }
274 
275   GetSystemProperties(config.MutableSystemProperties());
276 
277   OdrMetrics metrics(config.GetArtifactDirectory());
278   OnDeviceRefresh odr(config);
279 
280   std::string_view action(argv[0]);
281   CompilationOptions compilation_options;
282   if (action == "--check") {
283     // Fast determination of whether artifacts are up to date.
284     ExitCode exit_code = odr.CheckArtifactsAreUpToDate(metrics, &compilation_options);
285     // Normally, `--check` should not write metrics. If compilation is not required, there's no need
286     // to write metrics; if compilation is required, `--compile` will write metrics. Therefore,
287     // `--check` should only write metrics when things went wrong.
288     metrics.SetEnabled(exit_code != ExitCode::kOkay && exit_code != ExitCode::kCompilationRequired);
289     return exit_code;
290   } else if (action == "--compile") {
291     ExitCode exit_code = odr.CheckArtifactsAreUpToDate(metrics, &compilation_options);
292     if (exit_code != ExitCode::kCompilationRequired) {
293       // No compilation required, so only write metrics when things went wrong.
294       metrics.SetEnabled(exit_code != ExitCode::kOkay);
295       return exit_code;
296     }
297     OdrCompilationLog compilation_log;
298     if (!compilation_log.ShouldAttemptCompile(metrics.GetTrigger())) {
299       LOG(INFO) << "Compilation skipped because it was attempted recently";
300       return ExitCode::kOkay;
301     }
302     // Compilation required, so always write metrics.
303     metrics.SetEnabled(true);
304     ExitCode compile_result = odr.Compile(metrics, compilation_options);
305     compilation_log.Log(metrics.GetArtApexVersion(),
306                         metrics.GetArtApexLastUpdateMillis(),
307                         metrics.GetTrigger(),
308                         compile_result);
309     return compile_result;
310   } else if (action == "--force-compile") {
311     // Clean-up existing files.
312     if (!odr.RemoveArtifactsDirectory()) {
313       metrics.SetStatus(OdrMetrics::Status::kIoError);
314       return ExitCode::kCleanupFailed;
315     }
316     return odr.Compile(metrics, CompilationOptions::CompileAll(odr));
317   } else if (action == "--help") {
318     UsageHelp(argv[0]);
319   } else {
320     ArgumentError("Unknown argument: %s", action.data());
321   }
322 }
323