1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "file_utils.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <inttypes.h>
20*795d594fSAndroid Build Coastguard Worker #include <sys/stat.h>
21*795d594fSAndroid Build Coastguard Worker #include <sys/types.h>
22*795d594fSAndroid Build Coastguard Worker
23*795d594fSAndroid Build Coastguard Worker #ifndef _WIN32
24*795d594fSAndroid Build Coastguard Worker #include <sys/wait.h>
25*795d594fSAndroid Build Coastguard Worker #endif
26*795d594fSAndroid Build Coastguard Worker #include <unistd.h>
27*795d594fSAndroid Build Coastguard Worker
28*795d594fSAndroid Build Coastguard Worker // We need dladdr.
29*795d594fSAndroid Build Coastguard Worker #if !defined(__APPLE__) && !defined(_WIN32)
30*795d594fSAndroid Build Coastguard Worker #ifndef _GNU_SOURCE
31*795d594fSAndroid Build Coastguard Worker #define _GNU_SOURCE
32*795d594fSAndroid Build Coastguard Worker #define DEFINED_GNU_SOURCE
33*795d594fSAndroid Build Coastguard Worker #endif
34*795d594fSAndroid Build Coastguard Worker #include <dlfcn.h>
35*795d594fSAndroid Build Coastguard Worker #include <libgen.h>
36*795d594fSAndroid Build Coastguard Worker #ifdef DEFINED_GNU_SOURCE
37*795d594fSAndroid Build Coastguard Worker #undef _GNU_SOURCE
38*795d594fSAndroid Build Coastguard Worker #undef DEFINED_GNU_SOURCE
39*795d594fSAndroid Build Coastguard Worker #endif
40*795d594fSAndroid Build Coastguard Worker #endif
41*795d594fSAndroid Build Coastguard Worker
42*795d594fSAndroid Build Coastguard Worker #include <memory>
43*795d594fSAndroid Build Coastguard Worker #include <sstream>
44*795d594fSAndroid Build Coastguard Worker #include <vector>
45*795d594fSAndroid Build Coastguard Worker
46*795d594fSAndroid Build Coastguard Worker #include "android-base/file.h"
47*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
48*795d594fSAndroid Build Coastguard Worker #include "android-base/properties.h"
49*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
50*795d594fSAndroid Build Coastguard Worker #include "android-base/strings.h"
51*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h"
52*795d594fSAndroid Build Coastguard Worker #include "base/globals.h"
53*795d594fSAndroid Build Coastguard Worker #include "base/os.h"
54*795d594fSAndroid Build Coastguard Worker #include "base/stl_util.h"
55*795d594fSAndroid Build Coastguard Worker #include "base/unix_file/fd_file.h"
56*795d594fSAndroid Build Coastguard Worker #include "base/utils.h"
57*795d594fSAndroid Build Coastguard Worker
58*795d594fSAndroid Build Coastguard Worker #if defined(__APPLE__)
59*795d594fSAndroid Build Coastguard Worker #include <crt_externs.h>
60*795d594fSAndroid Build Coastguard Worker #include <sys/syscall.h>
61*795d594fSAndroid Build Coastguard Worker
62*795d594fSAndroid Build Coastguard Worker #include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
63*795d594fSAndroid Build Coastguard Worker #endif
64*795d594fSAndroid Build Coastguard Worker
65*795d594fSAndroid Build Coastguard Worker #if defined(__linux__)
66*795d594fSAndroid Build Coastguard Worker #include <linux/unistd.h>
67*795d594fSAndroid Build Coastguard Worker #endif
68*795d594fSAndroid Build Coastguard Worker
69*795d594fSAndroid Build Coastguard Worker #ifdef ART_TARGET_ANDROID
70*795d594fSAndroid Build Coastguard Worker #include "android-modules-utils/sdk_level.h"
71*795d594fSAndroid Build Coastguard Worker #endif
72*795d594fSAndroid Build Coastguard Worker
73*795d594fSAndroid Build Coastguard Worker namespace art {
74*795d594fSAndroid Build Coastguard Worker
75*795d594fSAndroid Build Coastguard Worker using android::base::ConsumePrefix;
76*795d594fSAndroid Build Coastguard Worker using android::base::GetBoolProperty;
77*795d594fSAndroid Build Coastguard Worker using android::base::GetProperty;
78*795d594fSAndroid Build Coastguard Worker using android::base::StringPrintf;
79*795d594fSAndroid Build Coastguard Worker
80*795d594fSAndroid Build Coastguard Worker static constexpr const char* kClassesDex = "classes.dex";
81*795d594fSAndroid Build Coastguard Worker static constexpr const char* kAndroidRootEnvVar = "ANDROID_ROOT";
82*795d594fSAndroid Build Coastguard Worker static constexpr const char* kAndroidRootDefaultPath = "/system";
83*795d594fSAndroid Build Coastguard Worker static constexpr const char* kAndroidSystemExtRootEnvVar = "SYSTEM_EXT_ROOT";
84*795d594fSAndroid Build Coastguard Worker static constexpr const char* kAndroidSystemExtRootDefaultPath = "/system_ext";
85*795d594fSAndroid Build Coastguard Worker static constexpr const char* kAndroidDataEnvVar = "ANDROID_DATA";
86*795d594fSAndroid Build Coastguard Worker static constexpr const char* kAndroidDataDefaultPath = "/data";
87*795d594fSAndroid Build Coastguard Worker static constexpr const char* kAndroidExpandEnvVar = "ANDROID_EXPAND";
88*795d594fSAndroid Build Coastguard Worker static constexpr const char* kAndroidExpandDefaultPath = "/mnt/expand";
89*795d594fSAndroid Build Coastguard Worker static constexpr const char* kAndroidArtRootEnvVar = "ANDROID_ART_ROOT";
90*795d594fSAndroid Build Coastguard Worker static constexpr const char* kAndroidConscryptRootEnvVar = "ANDROID_CONSCRYPT_ROOT";
91*795d594fSAndroid Build Coastguard Worker static constexpr const char* kAndroidI18nRootEnvVar = "ANDROID_I18N_ROOT";
92*795d594fSAndroid Build Coastguard Worker static constexpr const char* kApexDefaultPath = "/apex/";
93*795d594fSAndroid Build Coastguard Worker static constexpr const char* kArtApexDataEnvVar = "ART_APEX_DATA";
94*795d594fSAndroid Build Coastguard Worker static constexpr const char* kBootImageStem = "boot";
95*795d594fSAndroid Build Coastguard Worker
96*795d594fSAndroid Build Coastguard Worker // Get the "root" directory containing the "lib" directory where this instance
97*795d594fSAndroid Build Coastguard Worker // of the libartbase library (which contains `GetRootContainingLibartbase`) is
98*795d594fSAndroid Build Coastguard Worker // located:
99*795d594fSAndroid Build Coastguard Worker // - on host this "root" is normally the Android Root (e.g. something like
100*795d594fSAndroid Build Coastguard Worker // "$ANDROID_BUILD_TOP/out/host/linux-x86/");
101*795d594fSAndroid Build Coastguard Worker // - on target this "root" is normally the ART Root ("/apex/com.android.art").
102*795d594fSAndroid Build Coastguard Worker // Return the empty string if that directory cannot be found or if this code is
103*795d594fSAndroid Build Coastguard Worker // run on Windows or macOS.
GetRootContainingLibartbase()104*795d594fSAndroid Build Coastguard Worker static std::string GetRootContainingLibartbase() {
105*795d594fSAndroid Build Coastguard Worker #if !defined(_WIN32) && !defined(__APPLE__)
106*795d594fSAndroid Build Coastguard Worker // Check where libartbase is from, and derive from there.
107*795d594fSAndroid Build Coastguard Worker Dl_info info;
108*795d594fSAndroid Build Coastguard Worker if (dladdr(reinterpret_cast<const void*>(&GetRootContainingLibartbase), /* out */ &info) != 0) {
109*795d594fSAndroid Build Coastguard Worker // Make a duplicate of the fname so dirname can modify it.
110*795d594fSAndroid Build Coastguard Worker UniqueCPtr<char> fname(strdup(info.dli_fname));
111*795d594fSAndroid Build Coastguard Worker
112*795d594fSAndroid Build Coastguard Worker char* dir1 = dirname(fname.get()); // This is the lib directory.
113*795d594fSAndroid Build Coastguard Worker char* dir2 = dirname(dir1); // This is the "root" directory.
114*795d594fSAndroid Build Coastguard Worker if (OS::DirectoryExists(dir2)) {
115*795d594fSAndroid Build Coastguard Worker std::string tmp = dir2; // Make a copy here so that fname can be released.
116*795d594fSAndroid Build Coastguard Worker return tmp;
117*795d594fSAndroid Build Coastguard Worker }
118*795d594fSAndroid Build Coastguard Worker }
119*795d594fSAndroid Build Coastguard Worker #endif
120*795d594fSAndroid Build Coastguard Worker return "";
121*795d594fSAndroid Build Coastguard Worker }
122*795d594fSAndroid Build Coastguard Worker
GetAndroidDirSafe(const char * env_var,const char * default_dir,bool must_exist,std::string * error_msg)123*795d594fSAndroid Build Coastguard Worker static const char* GetAndroidDirSafe(const char* env_var,
124*795d594fSAndroid Build Coastguard Worker const char* default_dir,
125*795d594fSAndroid Build Coastguard Worker bool must_exist,
126*795d594fSAndroid Build Coastguard Worker std::string* error_msg) {
127*795d594fSAndroid Build Coastguard Worker const char* android_dir = getenv(env_var);
128*795d594fSAndroid Build Coastguard Worker if (android_dir == nullptr) {
129*795d594fSAndroid Build Coastguard Worker if (!must_exist || OS::DirectoryExists(default_dir)) {
130*795d594fSAndroid Build Coastguard Worker android_dir = default_dir;
131*795d594fSAndroid Build Coastguard Worker } else {
132*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("%s not set and %s does not exist", env_var, default_dir);
133*795d594fSAndroid Build Coastguard Worker return nullptr;
134*795d594fSAndroid Build Coastguard Worker }
135*795d594fSAndroid Build Coastguard Worker }
136*795d594fSAndroid Build Coastguard Worker if (must_exist && !OS::DirectoryExists(android_dir)) {
137*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("Failed to find directory %s", android_dir);
138*795d594fSAndroid Build Coastguard Worker return nullptr;
139*795d594fSAndroid Build Coastguard Worker }
140*795d594fSAndroid Build Coastguard Worker return android_dir;
141*795d594fSAndroid Build Coastguard Worker }
142*795d594fSAndroid Build Coastguard Worker
GetAndroidDir(const char * env_var,const char * default_dir,bool must_exist=true)143*795d594fSAndroid Build Coastguard Worker static const char* GetAndroidDir(const char* env_var,
144*795d594fSAndroid Build Coastguard Worker const char* default_dir,
145*795d594fSAndroid Build Coastguard Worker bool must_exist = true) {
146*795d594fSAndroid Build Coastguard Worker std::string error_msg;
147*795d594fSAndroid Build Coastguard Worker const char* dir = GetAndroidDirSafe(env_var, default_dir, must_exist, &error_msg);
148*795d594fSAndroid Build Coastguard Worker if (dir != nullptr) {
149*795d594fSAndroid Build Coastguard Worker return dir;
150*795d594fSAndroid Build Coastguard Worker } else {
151*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << error_msg;
152*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
153*795d594fSAndroid Build Coastguard Worker }
154*795d594fSAndroid Build Coastguard Worker }
155*795d594fSAndroid Build Coastguard Worker
GetAndroidRootSafe(std::string * error_msg)156*795d594fSAndroid Build Coastguard Worker std::string GetAndroidRootSafe(std::string* error_msg) {
157*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
158*795d594fSAndroid Build Coastguard Worker UNUSED(kAndroidRootEnvVar, kAndroidRootDefaultPath, GetRootContainingLibartbase);
159*795d594fSAndroid Build Coastguard Worker *error_msg = "GetAndroidRootSafe unsupported for Windows.";
160*795d594fSAndroid Build Coastguard Worker return "";
161*795d594fSAndroid Build Coastguard Worker #else
162*795d594fSAndroid Build Coastguard Worker std::string local_error_msg;
163*795d594fSAndroid Build Coastguard Worker const char* dir = GetAndroidDirSafe(kAndroidRootEnvVar, kAndroidRootDefaultPath,
164*795d594fSAndroid Build Coastguard Worker /*must_exist=*/ true, &local_error_msg);
165*795d594fSAndroid Build Coastguard Worker if (dir == nullptr) {
166*795d594fSAndroid Build Coastguard Worker // On host, libartbase is currently installed in "$ANDROID_ROOT/lib"
167*795d594fSAndroid Build Coastguard Worker // (e.g. something like "$ANDROID_BUILD_TOP/out/host/linux-x86/lib". Use this
168*795d594fSAndroid Build Coastguard Worker // information to infer the location of the Android Root (on host only).
169*795d594fSAndroid Build Coastguard Worker //
170*795d594fSAndroid Build Coastguard Worker // Note that this could change in the future, if we decided to install ART
171*795d594fSAndroid Build Coastguard Worker // artifacts in a different location, e.g. within an "ART APEX" directory.
172*795d594fSAndroid Build Coastguard Worker if (!kIsTargetBuild) {
173*795d594fSAndroid Build Coastguard Worker std::string root_containing_libartbase = GetRootContainingLibartbase();
174*795d594fSAndroid Build Coastguard Worker if (!root_containing_libartbase.empty()) {
175*795d594fSAndroid Build Coastguard Worker return root_containing_libartbase;
176*795d594fSAndroid Build Coastguard Worker }
177*795d594fSAndroid Build Coastguard Worker }
178*795d594fSAndroid Build Coastguard Worker *error_msg = std::move(local_error_msg);
179*795d594fSAndroid Build Coastguard Worker return "";
180*795d594fSAndroid Build Coastguard Worker }
181*795d594fSAndroid Build Coastguard Worker
182*795d594fSAndroid Build Coastguard Worker return dir;
183*795d594fSAndroid Build Coastguard Worker #endif
184*795d594fSAndroid Build Coastguard Worker }
185*795d594fSAndroid Build Coastguard Worker
GetAndroidRoot()186*795d594fSAndroid Build Coastguard Worker std::string GetAndroidRoot() {
187*795d594fSAndroid Build Coastguard Worker std::string error_msg;
188*795d594fSAndroid Build Coastguard Worker std::string ret = GetAndroidRootSafe(&error_msg);
189*795d594fSAndroid Build Coastguard Worker CHECK(!ret.empty()) << error_msg;
190*795d594fSAndroid Build Coastguard Worker return ret;
191*795d594fSAndroid Build Coastguard Worker }
192*795d594fSAndroid Build Coastguard Worker
GetSystemExtRootSafe(std::string * error_msg)193*795d594fSAndroid Build Coastguard Worker std::string GetSystemExtRootSafe(std::string* error_msg) {
194*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
195*795d594fSAndroid Build Coastguard Worker UNUSED(kAndroidSystemExtRootEnvVar, kAndroidSystemExtRootDefaultPath);
196*795d594fSAndroid Build Coastguard Worker *error_msg = "GetSystemExtRootSafe unsupported for Windows.";
197*795d594fSAndroid Build Coastguard Worker return "";
198*795d594fSAndroid Build Coastguard Worker #else
199*795d594fSAndroid Build Coastguard Worker const char* dir = GetAndroidDirSafe(kAndroidSystemExtRootEnvVar, kAndroidSystemExtRootDefaultPath,
200*795d594fSAndroid Build Coastguard Worker /*must_exist=*/ true, error_msg);
201*795d594fSAndroid Build Coastguard Worker return dir ? dir : "";
202*795d594fSAndroid Build Coastguard Worker #endif
203*795d594fSAndroid Build Coastguard Worker }
204*795d594fSAndroid Build Coastguard Worker
GetSystemExtRoot()205*795d594fSAndroid Build Coastguard Worker std::string GetSystemExtRoot() {
206*795d594fSAndroid Build Coastguard Worker std::string error_msg;
207*795d594fSAndroid Build Coastguard Worker std::string ret = GetSystemExtRootSafe(&error_msg);
208*795d594fSAndroid Build Coastguard Worker CHECK(!ret.empty()) << error_msg;
209*795d594fSAndroid Build Coastguard Worker return ret;
210*795d594fSAndroid Build Coastguard Worker }
211*795d594fSAndroid Build Coastguard Worker
GetArtRootSafe(bool must_exist,std::string * error_msg)212*795d594fSAndroid Build Coastguard Worker static std::string GetArtRootSafe(bool must_exist, /*out*/ std::string* error_msg) {
213*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
214*795d594fSAndroid Build Coastguard Worker UNUSED(kAndroidArtRootEnvVar, kAndroidArtApexDefaultPath, GetRootContainingLibartbase);
215*795d594fSAndroid Build Coastguard Worker UNUSED(must_exist);
216*795d594fSAndroid Build Coastguard Worker *error_msg = "GetArtRootSafe unsupported for Windows.";
217*795d594fSAndroid Build Coastguard Worker return "";
218*795d594fSAndroid Build Coastguard Worker #else
219*795d594fSAndroid Build Coastguard Worker // Prefer ANDROID_ART_ROOT if it's set.
220*795d594fSAndroid Build Coastguard Worker const char* android_art_root_from_env = getenv(kAndroidArtRootEnvVar);
221*795d594fSAndroid Build Coastguard Worker if (android_art_root_from_env != nullptr) {
222*795d594fSAndroid Build Coastguard Worker if (must_exist && !OS::DirectoryExists(android_art_root_from_env)) {
223*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf(
224*795d594fSAndroid Build Coastguard Worker "Failed to find %s directory %s", kAndroidArtRootEnvVar, android_art_root_from_env);
225*795d594fSAndroid Build Coastguard Worker return "";
226*795d594fSAndroid Build Coastguard Worker }
227*795d594fSAndroid Build Coastguard Worker return android_art_root_from_env;
228*795d594fSAndroid Build Coastguard Worker }
229*795d594fSAndroid Build Coastguard Worker
230*795d594fSAndroid Build Coastguard Worker // On target, libartbase is normally installed in
231*795d594fSAndroid Build Coastguard Worker // "$ANDROID_ART_ROOT/lib(64)" (e.g. something like
232*795d594fSAndroid Build Coastguard Worker // "/apex/com.android.art/lib(64)". Use this information to infer the
233*795d594fSAndroid Build Coastguard Worker // location of the ART Root (on target only).
234*795d594fSAndroid Build Coastguard Worker if (kIsTargetBuild) {
235*795d594fSAndroid Build Coastguard Worker // *However*, a copy of libartbase may still be installed outside the
236*795d594fSAndroid Build Coastguard Worker // ART Root on some occasions, as ART target gtests install their binaries
237*795d594fSAndroid Build Coastguard Worker // and their dependencies under the Android Root, i.e. "/system" (see
238*795d594fSAndroid Build Coastguard Worker // b/129534335). For that reason, we cannot reliably use
239*795d594fSAndroid Build Coastguard Worker // `GetRootContainingLibartbase` to find the ART Root. (Note that this is
240*795d594fSAndroid Build Coastguard Worker // not really a problem in practice, as Android Q devices define
241*795d594fSAndroid Build Coastguard Worker // ANDROID_ART_ROOT in their default environment, and will instead use
242*795d594fSAndroid Build Coastguard Worker // the logic above anyway.)
243*795d594fSAndroid Build Coastguard Worker //
244*795d594fSAndroid Build Coastguard Worker // TODO(b/129534335): Re-enable this logic when the only instance of
245*795d594fSAndroid Build Coastguard Worker // libartbase on target is the one from the ART APEX.
246*795d594fSAndroid Build Coastguard Worker if ((false)) {
247*795d594fSAndroid Build Coastguard Worker std::string root_containing_libartbase = GetRootContainingLibartbase();
248*795d594fSAndroid Build Coastguard Worker if (!root_containing_libartbase.empty()) {
249*795d594fSAndroid Build Coastguard Worker return root_containing_libartbase;
250*795d594fSAndroid Build Coastguard Worker }
251*795d594fSAndroid Build Coastguard Worker }
252*795d594fSAndroid Build Coastguard Worker }
253*795d594fSAndroid Build Coastguard Worker
254*795d594fSAndroid Build Coastguard Worker // Try the default path.
255*795d594fSAndroid Build Coastguard Worker if (must_exist && !OS::DirectoryExists(kAndroidArtApexDefaultPath)) {
256*795d594fSAndroid Build Coastguard Worker *error_msg =
257*795d594fSAndroid Build Coastguard Worker StringPrintf("Failed to find default ART root directory %s", kAndroidArtApexDefaultPath);
258*795d594fSAndroid Build Coastguard Worker return "";
259*795d594fSAndroid Build Coastguard Worker }
260*795d594fSAndroid Build Coastguard Worker return kAndroidArtApexDefaultPath;
261*795d594fSAndroid Build Coastguard Worker #endif
262*795d594fSAndroid Build Coastguard Worker }
263*795d594fSAndroid Build Coastguard Worker
GetArtRootSafe(std::string * error_msg)264*795d594fSAndroid Build Coastguard Worker std::string GetArtRootSafe(std::string* error_msg) {
265*795d594fSAndroid Build Coastguard Worker return GetArtRootSafe(/* must_exist= */ true, error_msg);
266*795d594fSAndroid Build Coastguard Worker }
267*795d594fSAndroid Build Coastguard Worker
GetArtRoot()268*795d594fSAndroid Build Coastguard Worker std::string GetArtRoot() {
269*795d594fSAndroid Build Coastguard Worker std::string error_msg;
270*795d594fSAndroid Build Coastguard Worker std::string ret = GetArtRootSafe(&error_msg);
271*795d594fSAndroid Build Coastguard Worker if (ret.empty()) {
272*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << error_msg;
273*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
274*795d594fSAndroid Build Coastguard Worker }
275*795d594fSAndroid Build Coastguard Worker return ret;
276*795d594fSAndroid Build Coastguard Worker }
277*795d594fSAndroid Build Coastguard Worker
GetArtBinDir()278*795d594fSAndroid Build Coastguard Worker std::string GetArtBinDir() {
279*795d594fSAndroid Build Coastguard Worker // Environment variable `ANDROID_ART_ROOT` is defined as
280*795d594fSAndroid Build Coastguard Worker // `$ANDROID_HOST_OUT/com.android.art` on host. However, host ART binaries are
281*795d594fSAndroid Build Coastguard Worker // still installed in `$ANDROID_HOST_OUT/bin` (i.e. outside the ART Root). The
282*795d594fSAndroid Build Coastguard Worker // situation is cleaner on target, where `ANDROID_ART_ROOT` is
283*795d594fSAndroid Build Coastguard Worker // `$ANDROID_ROOT/apex/com.android.art` and ART binaries are installed in
284*795d594fSAndroid Build Coastguard Worker // `$ANDROID_ROOT/apex/com.android.art/bin`.
285*795d594fSAndroid Build Coastguard Worker std::string android_art_root = kIsTargetBuild ? GetArtRoot() : GetAndroidRoot();
286*795d594fSAndroid Build Coastguard Worker return android_art_root + "/bin";
287*795d594fSAndroid Build Coastguard Worker }
288*795d594fSAndroid Build Coastguard Worker
GetAndroidDataSafe(std::string * error_msg)289*795d594fSAndroid Build Coastguard Worker std::string GetAndroidDataSafe(std::string* error_msg) {
290*795d594fSAndroid Build Coastguard Worker const char* android_dir = GetAndroidDirSafe(kAndroidDataEnvVar,
291*795d594fSAndroid Build Coastguard Worker kAndroidDataDefaultPath,
292*795d594fSAndroid Build Coastguard Worker /* must_exist= */ true,
293*795d594fSAndroid Build Coastguard Worker error_msg);
294*795d594fSAndroid Build Coastguard Worker return (android_dir != nullptr) ? android_dir : "";
295*795d594fSAndroid Build Coastguard Worker }
296*795d594fSAndroid Build Coastguard Worker
GetAndroidData()297*795d594fSAndroid Build Coastguard Worker std::string GetAndroidData() { return GetAndroidDir(kAndroidDataEnvVar, kAndroidDataDefaultPath); }
298*795d594fSAndroid Build Coastguard Worker
GetAndroidExpandSafe(std::string * error_msg)299*795d594fSAndroid Build Coastguard Worker std::string GetAndroidExpandSafe(std::string* error_msg) {
300*795d594fSAndroid Build Coastguard Worker const char* android_dir = GetAndroidDirSafe(kAndroidExpandEnvVar,
301*795d594fSAndroid Build Coastguard Worker kAndroidExpandDefaultPath,
302*795d594fSAndroid Build Coastguard Worker /*must_exist=*/true,
303*795d594fSAndroid Build Coastguard Worker error_msg);
304*795d594fSAndroid Build Coastguard Worker return (android_dir != nullptr) ? android_dir : "";
305*795d594fSAndroid Build Coastguard Worker }
306*795d594fSAndroid Build Coastguard Worker
GetAndroidExpand()307*795d594fSAndroid Build Coastguard Worker std::string GetAndroidExpand() {
308*795d594fSAndroid Build Coastguard Worker return GetAndroidDir(kAndroidExpandEnvVar, kAndroidExpandDefaultPath);
309*795d594fSAndroid Build Coastguard Worker }
310*795d594fSAndroid Build Coastguard Worker
GetArtApexData()311*795d594fSAndroid Build Coastguard Worker std::string GetArtApexData() {
312*795d594fSAndroid Build Coastguard Worker return GetAndroidDir(kArtApexDataEnvVar, kArtApexDataDefaultPath, /*must_exist=*/false);
313*795d594fSAndroid Build Coastguard Worker }
314*795d594fSAndroid Build Coastguard Worker
GetPrebuiltPrimaryBootImageDir(const std::string & android_root)315*795d594fSAndroid Build Coastguard Worker static std::string GetPrebuiltPrimaryBootImageDir(const std::string& android_root) {
316*795d594fSAndroid Build Coastguard Worker return StringPrintf("%s/framework", android_root.c_str());
317*795d594fSAndroid Build Coastguard Worker }
318*795d594fSAndroid Build Coastguard Worker
GetPrebuiltPrimaryBootImageDir()319*795d594fSAndroid Build Coastguard Worker std::string GetPrebuiltPrimaryBootImageDir() {
320*795d594fSAndroid Build Coastguard Worker std::string android_root = GetAndroidRoot();
321*795d594fSAndroid Build Coastguard Worker if (android_root.empty()) {
322*795d594fSAndroid Build Coastguard Worker return "";
323*795d594fSAndroid Build Coastguard Worker }
324*795d594fSAndroid Build Coastguard Worker return GetPrebuiltPrimaryBootImageDir(android_root);
325*795d594fSAndroid Build Coastguard Worker }
326*795d594fSAndroid Build Coastguard Worker
GetFirstMainlineFrameworkLibraryFilename(std::string * error_msg)327*795d594fSAndroid Build Coastguard Worker std::string GetFirstMainlineFrameworkLibraryFilename(std::string* error_msg) {
328*795d594fSAndroid Build Coastguard Worker const char* env_bcp = getenv("BOOTCLASSPATH");
329*795d594fSAndroid Build Coastguard Worker const char* env_dex2oat_bcp = getenv("DEX2OATBOOTCLASSPATH");
330*795d594fSAndroid Build Coastguard Worker if (env_bcp == nullptr || env_dex2oat_bcp == nullptr) {
331*795d594fSAndroid Build Coastguard Worker *error_msg = "BOOTCLASSPATH and DEX2OATBOOTCLASSPATH must not be empty";
332*795d594fSAndroid Build Coastguard Worker return "";
333*795d594fSAndroid Build Coastguard Worker }
334*795d594fSAndroid Build Coastguard Worker
335*795d594fSAndroid Build Coastguard Worker // DEX2OATBOOTCLASSPATH contains core libraries and framework libraries. We used to only compile
336*795d594fSAndroid Build Coastguard Worker // those libraries. Now we compile mainline framework libraries as well, and we have repurposed
337*795d594fSAndroid Build Coastguard Worker // DEX2OATBOOTCLASSPATH to indicate the separation between mainline framework libraries and other
338*795d594fSAndroid Build Coastguard Worker // libraries.
339*795d594fSAndroid Build Coastguard Worker std::string_view mainline_bcp(env_bcp);
340*795d594fSAndroid Build Coastguard Worker if (!android::base::ConsumePrefix(&mainline_bcp, env_dex2oat_bcp)) {
341*795d594fSAndroid Build Coastguard Worker *error_msg = "DEX2OATBOOTCLASSPATH must be a prefix of BOOTCLASSPATH";
342*795d594fSAndroid Build Coastguard Worker return "";
343*795d594fSAndroid Build Coastguard Worker }
344*795d594fSAndroid Build Coastguard Worker
345*795d594fSAndroid Build Coastguard Worker std::vector<std::string_view> mainline_bcp_jars;
346*795d594fSAndroid Build Coastguard Worker Split(mainline_bcp, ':', &mainline_bcp_jars);
347*795d594fSAndroid Build Coastguard Worker if (mainline_bcp_jars.empty()) {
348*795d594fSAndroid Build Coastguard Worker *error_msg = "No mainline framework library found";
349*795d594fSAndroid Build Coastguard Worker return "";
350*795d594fSAndroid Build Coastguard Worker }
351*795d594fSAndroid Build Coastguard Worker
352*795d594fSAndroid Build Coastguard Worker return std::string(mainline_bcp_jars[0]);
353*795d594fSAndroid Build Coastguard Worker }
354*795d594fSAndroid Build Coastguard Worker
GetFirstMainlineFrameworkLibraryName(std::string * error_msg)355*795d594fSAndroid Build Coastguard Worker static std::string GetFirstMainlineFrameworkLibraryName(std::string* error_msg) {
356*795d594fSAndroid Build Coastguard Worker std::string filename = GetFirstMainlineFrameworkLibraryFilename(error_msg);
357*795d594fSAndroid Build Coastguard Worker if (filename.empty()) {
358*795d594fSAndroid Build Coastguard Worker return "";
359*795d594fSAndroid Build Coastguard Worker }
360*795d594fSAndroid Build Coastguard Worker
361*795d594fSAndroid Build Coastguard Worker std::string jar_name = android::base::Basename(filename);
362*795d594fSAndroid Build Coastguard Worker
363*795d594fSAndroid Build Coastguard Worker std::string_view library_name(jar_name);
364*795d594fSAndroid Build Coastguard Worker if (!android::base::ConsumeSuffix(&library_name, ".jar")) {
365*795d594fSAndroid Build Coastguard Worker *error_msg = "Invalid mainline framework jar: " + jar_name;
366*795d594fSAndroid Build Coastguard Worker return "";
367*795d594fSAndroid Build Coastguard Worker }
368*795d594fSAndroid Build Coastguard Worker
369*795d594fSAndroid Build Coastguard Worker return std::string(library_name);
370*795d594fSAndroid Build Coastguard Worker }
371*795d594fSAndroid Build Coastguard Worker
372*795d594fSAndroid Build Coastguard Worker // Returns true when no error occurs, even if the extension doesn't exist.
MaybeAppendBootImageMainlineExtension(const std::string & android_root,bool deny_system_files,bool deny_art_apex_data_files,std::string * location,std::string * error_msg)373*795d594fSAndroid Build Coastguard Worker static bool MaybeAppendBootImageMainlineExtension(const std::string& android_root,
374*795d594fSAndroid Build Coastguard Worker bool deny_system_files,
375*795d594fSAndroid Build Coastguard Worker bool deny_art_apex_data_files,
376*795d594fSAndroid Build Coastguard Worker /*inout*/ std::string* location,
377*795d594fSAndroid Build Coastguard Worker /*out*/ std::string* error_msg) {
378*795d594fSAndroid Build Coastguard Worker if (!kIsTargetAndroid || RunningOnVM()) {
379*795d594fSAndroid Build Coastguard Worker return true;
380*795d594fSAndroid Build Coastguard Worker }
381*795d594fSAndroid Build Coastguard Worker // Due to how the runtime determines the mapping between boot images and bootclasspath jars, the
382*795d594fSAndroid Build Coastguard Worker // name of the boot image extension must be in the format of
383*795d594fSAndroid Build Coastguard Worker // `<primary-boot-image-stem>-<first-library-name>.art`.
384*795d594fSAndroid Build Coastguard Worker std::string library_name = GetFirstMainlineFrameworkLibraryName(error_msg);
385*795d594fSAndroid Build Coastguard Worker if (library_name.empty()) {
386*795d594fSAndroid Build Coastguard Worker return false;
387*795d594fSAndroid Build Coastguard Worker }
388*795d594fSAndroid Build Coastguard Worker
389*795d594fSAndroid Build Coastguard Worker if (!deny_art_apex_data_files) {
390*795d594fSAndroid Build Coastguard Worker std::string mainline_extension_location =
391*795d594fSAndroid Build Coastguard Worker StringPrintf("%s/%s-%s.art",
392*795d594fSAndroid Build Coastguard Worker GetApexDataDalvikCacheDirectory(InstructionSet::kNone).c_str(),
393*795d594fSAndroid Build Coastguard Worker kBootImageStem,
394*795d594fSAndroid Build Coastguard Worker library_name.c_str());
395*795d594fSAndroid Build Coastguard Worker std::string mainline_extension_path =
396*795d594fSAndroid Build Coastguard Worker GetSystemImageFilename(mainline_extension_location.c_str(), kRuntimeISA);
397*795d594fSAndroid Build Coastguard Worker if (OS::FileExists(mainline_extension_path.c_str(), /*check_file_type=*/true)) {
398*795d594fSAndroid Build Coastguard Worker *location += ":" + mainline_extension_location;
399*795d594fSAndroid Build Coastguard Worker return true;
400*795d594fSAndroid Build Coastguard Worker }
401*795d594fSAndroid Build Coastguard Worker }
402*795d594fSAndroid Build Coastguard Worker
403*795d594fSAndroid Build Coastguard Worker if (!deny_system_files) {
404*795d594fSAndroid Build Coastguard Worker std::string mainline_extension_location = StringPrintf(
405*795d594fSAndroid Build Coastguard Worker "%s/framework/%s-%s.art", android_root.c_str(), kBootImageStem, library_name.c_str());
406*795d594fSAndroid Build Coastguard Worker std::string mainline_extension_path =
407*795d594fSAndroid Build Coastguard Worker GetSystemImageFilename(mainline_extension_location.c_str(), kRuntimeISA);
408*795d594fSAndroid Build Coastguard Worker // It is expected that the file doesn't exist when the ART module is preloaded on an old source
409*795d594fSAndroid Build Coastguard Worker // tree that doesn't dexpreopt mainline BCP jars, so it shouldn't be considered as an error.
410*795d594fSAndroid Build Coastguard Worker if (OS::FileExists(mainline_extension_path.c_str(), /*check_file_type=*/true)) {
411*795d594fSAndroid Build Coastguard Worker *location += ":" + mainline_extension_location;
412*795d594fSAndroid Build Coastguard Worker return true;
413*795d594fSAndroid Build Coastguard Worker }
414*795d594fSAndroid Build Coastguard Worker }
415*795d594fSAndroid Build Coastguard Worker
416*795d594fSAndroid Build Coastguard Worker return true;
417*795d594fSAndroid Build Coastguard Worker }
418*795d594fSAndroid Build Coastguard Worker
GetDefaultBootImageLocationSafe(const std::string & android_root,bool deny_art_apex_data_files,std::string * error_msg)419*795d594fSAndroid Build Coastguard Worker std::string GetDefaultBootImageLocationSafe(const std::string& android_root,
420*795d594fSAndroid Build Coastguard Worker bool deny_art_apex_data_files,
421*795d594fSAndroid Build Coastguard Worker std::string* error_msg) {
422*795d594fSAndroid Build Coastguard Worker constexpr static const char* kEtcBootImageProf = "etc/boot-image.prof";
423*795d594fSAndroid Build Coastguard Worker constexpr static const char* kMinimalBootImageStem = "boot_minimal";
424*795d594fSAndroid Build Coastguard Worker
425*795d594fSAndroid Build Coastguard Worker // If an update for the ART module has been been installed, a single boot image for the entire
426*795d594fSAndroid Build Coastguard Worker // bootclasspath is in the ART APEX data directory.
427*795d594fSAndroid Build Coastguard Worker if (kIsTargetBuild && !deny_art_apex_data_files) {
428*795d594fSAndroid Build Coastguard Worker const std::string boot_image = GetApexDataDalvikCacheDirectory(InstructionSet::kNone) + "/" +
429*795d594fSAndroid Build Coastguard Worker kBootImageStem + kArtExtension;
430*795d594fSAndroid Build Coastguard Worker const std::string boot_image_filename = GetSystemImageFilename(boot_image.c_str(), kRuntimeISA);
431*795d594fSAndroid Build Coastguard Worker if (OS::FileExists(boot_image_filename.c_str(), /*check_file_type=*/true)) {
432*795d594fSAndroid Build Coastguard Worker // Boot image consists of two parts:
433*795d594fSAndroid Build Coastguard Worker // - the primary boot image (contains the Core Libraries and framework libraries)
434*795d594fSAndroid Build Coastguard Worker // - the boot image mainline extension (contains mainline framework libraries)
435*795d594fSAndroid Build Coastguard Worker // Typically
436*795d594fSAndroid Build Coastguard Worker // "/data/misc/apexdata/com.android.art/dalvik-cache/boot.art!/apex/com.android.art
437*795d594fSAndroid Build Coastguard Worker // /etc/boot-image.prof!/system/etc/boot-image.prof:
438*795d594fSAndroid Build Coastguard Worker // /data/misc/apexdata/com.android.art/dalvik-cache/boot-framework-adservices.art".
439*795d594fSAndroid Build Coastguard Worker std::string location = StringPrintf("%s!%s/%s!%s/%s",
440*795d594fSAndroid Build Coastguard Worker boot_image.c_str(),
441*795d594fSAndroid Build Coastguard Worker kAndroidArtApexDefaultPath,
442*795d594fSAndroid Build Coastguard Worker kEtcBootImageProf,
443*795d594fSAndroid Build Coastguard Worker android_root.c_str(),
444*795d594fSAndroid Build Coastguard Worker kEtcBootImageProf);
445*795d594fSAndroid Build Coastguard Worker if (!MaybeAppendBootImageMainlineExtension(android_root,
446*795d594fSAndroid Build Coastguard Worker /*deny_system_files=*/true,
447*795d594fSAndroid Build Coastguard Worker deny_art_apex_data_files,
448*795d594fSAndroid Build Coastguard Worker &location,
449*795d594fSAndroid Build Coastguard Worker error_msg)) {
450*795d594fSAndroid Build Coastguard Worker return "";
451*795d594fSAndroid Build Coastguard Worker }
452*795d594fSAndroid Build Coastguard Worker return location;
453*795d594fSAndroid Build Coastguard Worker } else if (errno == EACCES) {
454*795d594fSAndroid Build Coastguard Worker // Additional warning for potential SELinux misconfiguration.
455*795d594fSAndroid Build Coastguard Worker PLOG(ERROR) << "Default boot image check failed, could not stat: " << boot_image_filename;
456*795d594fSAndroid Build Coastguard Worker }
457*795d594fSAndroid Build Coastguard Worker
458*795d594fSAndroid Build Coastguard Worker // odrefresh can generate a minimal boot image, which only includes code from BCP jars in the
459*795d594fSAndroid Build Coastguard Worker // ART module, when it fails to generate a single boot image for the entire bootclasspath (i.e.,
460*795d594fSAndroid Build Coastguard Worker // full boot image). Use it if it exists.
461*795d594fSAndroid Build Coastguard Worker const std::string minimal_boot_image = GetApexDataDalvikCacheDirectory(InstructionSet::kNone) +
462*795d594fSAndroid Build Coastguard Worker "/" + kMinimalBootImageStem + kArtExtension;
463*795d594fSAndroid Build Coastguard Worker const std::string minimal_boot_image_filename =
464*795d594fSAndroid Build Coastguard Worker GetSystemImageFilename(minimal_boot_image.c_str(), kRuntimeISA);
465*795d594fSAndroid Build Coastguard Worker if (OS::FileExists(minimal_boot_image_filename.c_str(), /*check_file_type=*/true)) {
466*795d594fSAndroid Build Coastguard Worker // Typically "/data/misc/apexdata/com.android.art/dalvik-cache/boot_minimal.art!/apex
467*795d594fSAndroid Build Coastguard Worker // /com.android.art/etc/boot-image.prof:/nonx/boot_minimal-framework.art!/system/etc
468*795d594fSAndroid Build Coastguard Worker // /boot-image.prof".
469*795d594fSAndroid Build Coastguard Worker return StringPrintf("%s!%s/%s:/nonx/%s-framework.art!%s/%s",
470*795d594fSAndroid Build Coastguard Worker minimal_boot_image.c_str(),
471*795d594fSAndroid Build Coastguard Worker kAndroidArtApexDefaultPath,
472*795d594fSAndroid Build Coastguard Worker kEtcBootImageProf,
473*795d594fSAndroid Build Coastguard Worker kMinimalBootImageStem,
474*795d594fSAndroid Build Coastguard Worker android_root.c_str(),
475*795d594fSAndroid Build Coastguard Worker kEtcBootImageProf);
476*795d594fSAndroid Build Coastguard Worker } else if (errno == EACCES) {
477*795d594fSAndroid Build Coastguard Worker // Additional warning for potential SELinux misconfiguration.
478*795d594fSAndroid Build Coastguard Worker PLOG(ERROR) << "Minimal boot image check failed, could not stat: " << boot_image_filename;
479*795d594fSAndroid Build Coastguard Worker }
480*795d594fSAndroid Build Coastguard Worker }
481*795d594fSAndroid Build Coastguard Worker
482*795d594fSAndroid Build Coastguard Worker // Boot image consists of two parts:
483*795d594fSAndroid Build Coastguard Worker // - the primary boot image (contains the Core Libraries and framework libraries)
484*795d594fSAndroid Build Coastguard Worker // - the boot image mainline extension (contains mainline framework libraries)
485*795d594fSAndroid Build Coastguard Worker // Typically "/system/framework/boot.art
486*795d594fSAndroid Build Coastguard Worker // !/apex/com.android.art/etc/boot-image.prof!/system/etc/boot-image.prof:
487*795d594fSAndroid Build Coastguard Worker // /system/framework/boot-framework-adservices.art".
488*795d594fSAndroid Build Coastguard Worker
489*795d594fSAndroid Build Coastguard Worker std::string location = StringPrintf("%s/%s.art!%s/%s!%s/%s",
490*795d594fSAndroid Build Coastguard Worker GetPrebuiltPrimaryBootImageDir(android_root).c_str(),
491*795d594fSAndroid Build Coastguard Worker kBootImageStem,
492*795d594fSAndroid Build Coastguard Worker kAndroidArtApexDefaultPath,
493*795d594fSAndroid Build Coastguard Worker kEtcBootImageProf,
494*795d594fSAndroid Build Coastguard Worker android_root.c_str(),
495*795d594fSAndroid Build Coastguard Worker kEtcBootImageProf);
496*795d594fSAndroid Build Coastguard Worker
497*795d594fSAndroid Build Coastguard Worker #ifdef ART_TARGET_ANDROID
498*795d594fSAndroid Build Coastguard Worker // Prior to U, there was a framework extension.
499*795d594fSAndroid Build Coastguard Worker if (!android::modules::sdklevel::IsAtLeastU()) {
500*795d594fSAndroid Build Coastguard Worker location = StringPrintf("%s/%s.art!%s/%s:%s/framework/%s-framework.art!%s/%s",
501*795d594fSAndroid Build Coastguard Worker GetPrebuiltPrimaryBootImageDir(android_root).c_str(),
502*795d594fSAndroid Build Coastguard Worker kBootImageStem,
503*795d594fSAndroid Build Coastguard Worker kAndroidArtApexDefaultPath,
504*795d594fSAndroid Build Coastguard Worker kEtcBootImageProf,
505*795d594fSAndroid Build Coastguard Worker android_root.c_str(),
506*795d594fSAndroid Build Coastguard Worker kBootImageStem,
507*795d594fSAndroid Build Coastguard Worker android_root.c_str(),
508*795d594fSAndroid Build Coastguard Worker kEtcBootImageProf);
509*795d594fSAndroid Build Coastguard Worker }
510*795d594fSAndroid Build Coastguard Worker #endif
511*795d594fSAndroid Build Coastguard Worker
512*795d594fSAndroid Build Coastguard Worker if (!MaybeAppendBootImageMainlineExtension(android_root,
513*795d594fSAndroid Build Coastguard Worker /*deny_system_files=*/false,
514*795d594fSAndroid Build Coastguard Worker deny_art_apex_data_files,
515*795d594fSAndroid Build Coastguard Worker &location,
516*795d594fSAndroid Build Coastguard Worker error_msg)) {
517*795d594fSAndroid Build Coastguard Worker return "";
518*795d594fSAndroid Build Coastguard Worker }
519*795d594fSAndroid Build Coastguard Worker return location;
520*795d594fSAndroid Build Coastguard Worker }
521*795d594fSAndroid Build Coastguard Worker
GetDefaultBootImageLocation(const std::string & android_root,bool deny_art_apex_data_files)522*795d594fSAndroid Build Coastguard Worker std::string GetDefaultBootImageLocation(const std::string& android_root,
523*795d594fSAndroid Build Coastguard Worker bool deny_art_apex_data_files) {
524*795d594fSAndroid Build Coastguard Worker std::string error_msg;
525*795d594fSAndroid Build Coastguard Worker std::string location =
526*795d594fSAndroid Build Coastguard Worker GetDefaultBootImageLocationSafe(android_root, deny_art_apex_data_files, &error_msg);
527*795d594fSAndroid Build Coastguard Worker CHECK(!location.empty()) << error_msg;
528*795d594fSAndroid Build Coastguard Worker return location;
529*795d594fSAndroid Build Coastguard Worker }
530*795d594fSAndroid Build Coastguard Worker
GetJitZygoteBootImageLocation()531*795d594fSAndroid Build Coastguard Worker std::string GetJitZygoteBootImageLocation() {
532*795d594fSAndroid Build Coastguard Worker // Intentionally use a non-existing location so that the runtime will fail to find the boot image
533*795d594fSAndroid Build Coastguard Worker // and JIT bootclasspath with the given profiles.
534*795d594fSAndroid Build Coastguard Worker return "/nonx/boot.art!/apex/com.android.art/etc/boot-image.prof!/system/etc/boot-image.prof";
535*795d594fSAndroid Build Coastguard Worker }
536*795d594fSAndroid Build Coastguard Worker
GetBootImageLocationForDefaultBcp(bool no_boot_image,std::string user_defined_boot_image,bool deny_art_apex_data_files,std::string * error_msg)537*795d594fSAndroid Build Coastguard Worker std::string GetBootImageLocationForDefaultBcp(bool no_boot_image,
538*795d594fSAndroid Build Coastguard Worker std::string user_defined_boot_image,
539*795d594fSAndroid Build Coastguard Worker bool deny_art_apex_data_files,
540*795d594fSAndroid Build Coastguard Worker std::string* error_msg) {
541*795d594fSAndroid Build Coastguard Worker if (no_boot_image) {
542*795d594fSAndroid Build Coastguard Worker return GetJitZygoteBootImageLocation();
543*795d594fSAndroid Build Coastguard Worker }
544*795d594fSAndroid Build Coastguard Worker if (!user_defined_boot_image.empty()) {
545*795d594fSAndroid Build Coastguard Worker return user_defined_boot_image;
546*795d594fSAndroid Build Coastguard Worker }
547*795d594fSAndroid Build Coastguard Worker std::string android_root = GetAndroidRootSafe(error_msg);
548*795d594fSAndroid Build Coastguard Worker if (!error_msg->empty()) {
549*795d594fSAndroid Build Coastguard Worker return "";
550*795d594fSAndroid Build Coastguard Worker }
551*795d594fSAndroid Build Coastguard Worker return GetDefaultBootImageLocationSafe(android_root, deny_art_apex_data_files, error_msg);
552*795d594fSAndroid Build Coastguard Worker }
553*795d594fSAndroid Build Coastguard Worker
GetBootImageLocationForDefaultBcpRespectingSysProps(std::string * error_msg)554*795d594fSAndroid Build Coastguard Worker std::string GetBootImageLocationForDefaultBcpRespectingSysProps(std::string* error_msg) {
555*795d594fSAndroid Build Coastguard Worker bool no_boot_image =
556*795d594fSAndroid Build Coastguard Worker GetBoolProperty("persist.device_config.runtime_native_boot.profilebootclasspath",
557*795d594fSAndroid Build Coastguard Worker GetBoolProperty("dalvik.vm.profilebootclasspath", /*default_value=*/false));
558*795d594fSAndroid Build Coastguard Worker std::string user_defined_boot_image = GetProperty("dalvik.vm.boot-image", /*default_value=*/"");
559*795d594fSAndroid Build Coastguard Worker bool deny_art_apex_data_files =
560*795d594fSAndroid Build Coastguard Worker !GetBoolProperty("odsign.verification.success", /*default_value=*/false);
561*795d594fSAndroid Build Coastguard Worker return GetBootImageLocationForDefaultBcp(
562*795d594fSAndroid Build Coastguard Worker no_boot_image, user_defined_boot_image, deny_art_apex_data_files, error_msg);
563*795d594fSAndroid Build Coastguard Worker }
564*795d594fSAndroid Build Coastguard Worker
565*795d594fSAndroid Build Coastguard Worker static /*constinit*/ std::string_view dalvik_cache_sub_dir = "dalvik-cache";
566*795d594fSAndroid Build Coastguard Worker
OverrideDalvikCacheSubDirectory(std::string sub_dir)567*795d594fSAndroid Build Coastguard Worker void OverrideDalvikCacheSubDirectory(std::string sub_dir) {
568*795d594fSAndroid Build Coastguard Worker static std::string overridden_dalvik_cache_sub_dir;
569*795d594fSAndroid Build Coastguard Worker overridden_dalvik_cache_sub_dir = std::move(sub_dir);
570*795d594fSAndroid Build Coastguard Worker dalvik_cache_sub_dir = overridden_dalvik_cache_sub_dir;
571*795d594fSAndroid Build Coastguard Worker }
572*795d594fSAndroid Build Coastguard Worker
GetDalvikCacheDirectory(std::string_view root_directory,std::string_view sub_directory={})573*795d594fSAndroid Build Coastguard Worker static std::string GetDalvikCacheDirectory(std::string_view root_directory,
574*795d594fSAndroid Build Coastguard Worker std::string_view sub_directory = {}) {
575*795d594fSAndroid Build Coastguard Worker std::stringstream oss;
576*795d594fSAndroid Build Coastguard Worker oss << root_directory << '/' << dalvik_cache_sub_dir;
577*795d594fSAndroid Build Coastguard Worker if (!sub_directory.empty()) {
578*795d594fSAndroid Build Coastguard Worker oss << '/' << sub_directory;
579*795d594fSAndroid Build Coastguard Worker }
580*795d594fSAndroid Build Coastguard Worker return oss.str();
581*795d594fSAndroid Build Coastguard Worker }
582*795d594fSAndroid Build Coastguard Worker
GetDalvikCache(const char * subdir,const bool create_if_absent,std::string * dalvik_cache,bool * have_android_data,bool * dalvik_cache_exists,bool * is_global_cache)583*795d594fSAndroid Build Coastguard Worker void GetDalvikCache(const char* subdir,
584*795d594fSAndroid Build Coastguard Worker const bool create_if_absent,
585*795d594fSAndroid Build Coastguard Worker std::string* dalvik_cache,
586*795d594fSAndroid Build Coastguard Worker bool* have_android_data,
587*795d594fSAndroid Build Coastguard Worker bool* dalvik_cache_exists,
588*795d594fSAndroid Build Coastguard Worker bool* is_global_cache) {
589*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
590*795d594fSAndroid Build Coastguard Worker UNUSED(subdir);
591*795d594fSAndroid Build Coastguard Worker UNUSED(create_if_absent);
592*795d594fSAndroid Build Coastguard Worker UNUSED(dalvik_cache);
593*795d594fSAndroid Build Coastguard Worker UNUSED(have_android_data);
594*795d594fSAndroid Build Coastguard Worker UNUSED(dalvik_cache_exists);
595*795d594fSAndroid Build Coastguard Worker UNUSED(is_global_cache);
596*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "GetDalvikCache unsupported on Windows.";
597*795d594fSAndroid Build Coastguard Worker #else
598*795d594fSAndroid Build Coastguard Worker CHECK(subdir != nullptr);
599*795d594fSAndroid Build Coastguard Worker std::string unused_error_msg;
600*795d594fSAndroid Build Coastguard Worker std::string android_data = GetAndroidDataSafe(&unused_error_msg);
601*795d594fSAndroid Build Coastguard Worker if (android_data.empty()) {
602*795d594fSAndroid Build Coastguard Worker *have_android_data = false;
603*795d594fSAndroid Build Coastguard Worker *dalvik_cache_exists = false;
604*795d594fSAndroid Build Coastguard Worker *is_global_cache = false;
605*795d594fSAndroid Build Coastguard Worker return;
606*795d594fSAndroid Build Coastguard Worker } else {
607*795d594fSAndroid Build Coastguard Worker *have_android_data = true;
608*795d594fSAndroid Build Coastguard Worker }
609*795d594fSAndroid Build Coastguard Worker const std::string dalvik_cache_root = GetDalvikCacheDirectory(android_data);
610*795d594fSAndroid Build Coastguard Worker *dalvik_cache = dalvik_cache_root + '/' + subdir;
611*795d594fSAndroid Build Coastguard Worker *dalvik_cache_exists = OS::DirectoryExists(dalvik_cache->c_str());
612*795d594fSAndroid Build Coastguard Worker *is_global_cache = (android_data == kAndroidDataDefaultPath);
613*795d594fSAndroid Build Coastguard Worker if (create_if_absent && !*dalvik_cache_exists && !*is_global_cache) {
614*795d594fSAndroid Build Coastguard Worker // Don't create the system's /data/dalvik-cache/... because it needs special permissions.
615*795d594fSAndroid Build Coastguard Worker *dalvik_cache_exists = ((mkdir(dalvik_cache_root.c_str(), 0700) == 0 || errno == EEXIST) &&
616*795d594fSAndroid Build Coastguard Worker (mkdir(dalvik_cache->c_str(), 0700) == 0 || errno == EEXIST));
617*795d594fSAndroid Build Coastguard Worker }
618*795d594fSAndroid Build Coastguard Worker #endif
619*795d594fSAndroid Build Coastguard Worker }
620*795d594fSAndroid Build Coastguard Worker
621*795d594fSAndroid Build Coastguard Worker // Returns a path formed by encoding the dex location into the filename. The path returned will be
622*795d594fSAndroid Build Coastguard Worker // rooted at `cache_location`.
GetLocationEncodedFilename(std::string_view location,std::string_view cache_location,std::string * filename,std::string * error_msg)623*795d594fSAndroid Build Coastguard Worker static bool GetLocationEncodedFilename(std::string_view location,
624*795d594fSAndroid Build Coastguard Worker std::string_view cache_location,
625*795d594fSAndroid Build Coastguard Worker std::string* filename,
626*795d594fSAndroid Build Coastguard Worker std::string* error_msg) {
627*795d594fSAndroid Build Coastguard Worker if (!location.starts_with('/')) {
628*795d594fSAndroid Build Coastguard Worker *error_msg = "Expected path in location to be absolute: " + std::string(location);
629*795d594fSAndroid Build Coastguard Worker return false;
630*795d594fSAndroid Build Coastguard Worker }
631*795d594fSAndroid Build Coastguard Worker *filename = cache_location;
632*795d594fSAndroid Build Coastguard Worker *filename += location; // Including the leading slash.
633*795d594fSAndroid Build Coastguard Worker size_t replace_start = cache_location.length() + /* skip the leading slash from `location` */ 1u;
634*795d594fSAndroid Build Coastguard Worker std::replace(filename->begin() + replace_start, filename->end(), '/', '@');
635*795d594fSAndroid Build Coastguard Worker if (!location.ends_with(".dex") && !location.ends_with(kArtExtension) &&
636*795d594fSAndroid Build Coastguard Worker !location.ends_with(kOatExtension)) {
637*795d594fSAndroid Build Coastguard Worker *filename += "@";
638*795d594fSAndroid Build Coastguard Worker *filename += kClassesDex;
639*795d594fSAndroid Build Coastguard Worker }
640*795d594fSAndroid Build Coastguard Worker return true;
641*795d594fSAndroid Build Coastguard Worker }
642*795d594fSAndroid Build Coastguard Worker
GetDalvikCacheFilename(std::string_view location,std::string_view cache_location,std::string * filename,std::string * error_msg)643*795d594fSAndroid Build Coastguard Worker bool GetDalvikCacheFilename(std::string_view location,
644*795d594fSAndroid Build Coastguard Worker std::string_view cache_location,
645*795d594fSAndroid Build Coastguard Worker std::string* filename,
646*795d594fSAndroid Build Coastguard Worker std::string* error_msg) {
647*795d594fSAndroid Build Coastguard Worker return GetLocationEncodedFilename(location, cache_location, filename, error_msg);
648*795d594fSAndroid Build Coastguard Worker }
649*795d594fSAndroid Build Coastguard Worker
GetApexDataDalvikCacheDirectory(InstructionSet isa)650*795d594fSAndroid Build Coastguard Worker std::string GetApexDataDalvikCacheDirectory(InstructionSet isa) {
651*795d594fSAndroid Build Coastguard Worker if (isa != InstructionSet::kNone) {
652*795d594fSAndroid Build Coastguard Worker return GetDalvikCacheDirectory(GetArtApexData(), GetInstructionSetString(isa));
653*795d594fSAndroid Build Coastguard Worker }
654*795d594fSAndroid Build Coastguard Worker return GetDalvikCacheDirectory(GetArtApexData());
655*795d594fSAndroid Build Coastguard Worker }
656*795d594fSAndroid Build Coastguard Worker
GetApexDataDalvikCacheFilename(std::string_view dex_location,InstructionSet isa,bool is_boot_classpath_location,std::string_view file_extension)657*795d594fSAndroid Build Coastguard Worker static std::string GetApexDataDalvikCacheFilename(std::string_view dex_location,
658*795d594fSAndroid Build Coastguard Worker InstructionSet isa,
659*795d594fSAndroid Build Coastguard Worker bool is_boot_classpath_location,
660*795d594fSAndroid Build Coastguard Worker std::string_view file_extension) {
661*795d594fSAndroid Build Coastguard Worker if (LocationIsOnApex(dex_location) && is_boot_classpath_location) {
662*795d594fSAndroid Build Coastguard Worker // We don't compile boot images for updatable APEXes.
663*795d594fSAndroid Build Coastguard Worker return {};
664*795d594fSAndroid Build Coastguard Worker }
665*795d594fSAndroid Build Coastguard Worker std::string apex_data_dalvik_cache = GetApexDataDalvikCacheDirectory(isa);
666*795d594fSAndroid Build Coastguard Worker if (!is_boot_classpath_location) {
667*795d594fSAndroid Build Coastguard Worker // Arguments: "/system/framework/xyz.jar", "arm", true, "odex"
668*795d594fSAndroid Build Coastguard Worker // Result:
669*795d594fSAndroid Build Coastguard Worker // "/data/misc/apexdata/com.android.art/dalvik-cache/arm/system@[email protected]@classes.odex"
670*795d594fSAndroid Build Coastguard Worker std::string result, unused_error_msg;
671*795d594fSAndroid Build Coastguard Worker GetDalvikCacheFilename(dex_location,
672*795d594fSAndroid Build Coastguard Worker apex_data_dalvik_cache,
673*795d594fSAndroid Build Coastguard Worker &result,
674*795d594fSAndroid Build Coastguard Worker &unused_error_msg);
675*795d594fSAndroid Build Coastguard Worker return ReplaceFileExtension(result, file_extension);
676*795d594fSAndroid Build Coastguard Worker } else {
677*795d594fSAndroid Build Coastguard Worker // Arguments: "/system/framework/xyz.jar", "x86_64", false, "art"
678*795d594fSAndroid Build Coastguard Worker // Results: "/data/misc/apexdata/com.android.art/dalvik-cache/x86_64/boot-xyz.jar@classes.art"
679*795d594fSAndroid Build Coastguard Worker std::string basename = android::base::Basename(std::string{dex_location});
680*795d594fSAndroid Build Coastguard Worker return apex_data_dalvik_cache + "/boot-" + ReplaceFileExtension(basename, file_extension);
681*795d594fSAndroid Build Coastguard Worker }
682*795d594fSAndroid Build Coastguard Worker }
683*795d594fSAndroid Build Coastguard Worker
GetApexDataOatFilename(std::string_view location,InstructionSet isa)684*795d594fSAndroid Build Coastguard Worker std::string GetApexDataOatFilename(std::string_view location, InstructionSet isa) {
685*795d594fSAndroid Build Coastguard Worker return GetApexDataDalvikCacheFilename(
686*795d594fSAndroid Build Coastguard Worker location, isa, /*is_boot_classpath_location=*/true, kOatExtension);
687*795d594fSAndroid Build Coastguard Worker }
688*795d594fSAndroid Build Coastguard Worker
GetApexDataOdexFilename(std::string_view location,InstructionSet isa)689*795d594fSAndroid Build Coastguard Worker std::string GetApexDataOdexFilename(std::string_view location, InstructionSet isa) {
690*795d594fSAndroid Build Coastguard Worker return GetApexDataDalvikCacheFilename(
691*795d594fSAndroid Build Coastguard Worker location, isa, /*is_boot_classpath_location=*/false, kOdexExtension);
692*795d594fSAndroid Build Coastguard Worker }
693*795d594fSAndroid Build Coastguard Worker
GetApexDataBootImage(std::string_view dex_location)694*795d594fSAndroid Build Coastguard Worker std::string GetApexDataBootImage(std::string_view dex_location) {
695*795d594fSAndroid Build Coastguard Worker return GetApexDataDalvikCacheFilename(
696*795d594fSAndroid Build Coastguard Worker dex_location, InstructionSet::kNone, /*is_boot_classpath_location=*/true, kArtExtension);
697*795d594fSAndroid Build Coastguard Worker }
698*795d594fSAndroid Build Coastguard Worker
GetApexDataImage(std::string_view dex_location)699*795d594fSAndroid Build Coastguard Worker std::string GetApexDataImage(std::string_view dex_location) {
700*795d594fSAndroid Build Coastguard Worker return GetApexDataDalvikCacheFilename(
701*795d594fSAndroid Build Coastguard Worker dex_location, InstructionSet::kNone, /*is_boot_classpath_location=*/false, kArtExtension);
702*795d594fSAndroid Build Coastguard Worker }
703*795d594fSAndroid Build Coastguard Worker
GetApexDataDalvikCacheFilename(std::string_view dex_location,InstructionSet isa,std::string_view file_extension)704*795d594fSAndroid Build Coastguard Worker std::string GetApexDataDalvikCacheFilename(std::string_view dex_location,
705*795d594fSAndroid Build Coastguard Worker InstructionSet isa,
706*795d594fSAndroid Build Coastguard Worker std::string_view file_extension) {
707*795d594fSAndroid Build Coastguard Worker return GetApexDataDalvikCacheFilename(
708*795d594fSAndroid Build Coastguard Worker dex_location, isa, /*is_boot_classpath_location=*/false, file_extension);
709*795d594fSAndroid Build Coastguard Worker }
710*795d594fSAndroid Build Coastguard Worker
GetVdexFilename(const std::string & oat_location)711*795d594fSAndroid Build Coastguard Worker std::string GetVdexFilename(const std::string& oat_location) {
712*795d594fSAndroid Build Coastguard Worker return ReplaceFileExtension(oat_location, kVdexExtension);
713*795d594fSAndroid Build Coastguard Worker }
714*795d594fSAndroid Build Coastguard Worker
GetDmFilename(const std::string & dex_location)715*795d594fSAndroid Build Coastguard Worker std::string GetDmFilename(const std::string& dex_location) {
716*795d594fSAndroid Build Coastguard Worker return ReplaceFileExtension(dex_location, kDmExtension);
717*795d594fSAndroid Build Coastguard Worker }
718*795d594fSAndroid Build Coastguard Worker
719*795d594fSAndroid Build Coastguard Worker // check for the file in /system, followed by /system_ext
GetSystemOdexFilenameForApex(std::string_view location,InstructionSet isa)720*795d594fSAndroid Build Coastguard Worker std::string GetSystemOdexFilenameForApex(std::string_view location, InstructionSet isa) {
721*795d594fSAndroid Build Coastguard Worker DCHECK(LocationIsOnApex(location));
722*795d594fSAndroid Build Coastguard Worker std::string dir = GetAndroidRoot() + "/framework/oat/" + GetInstructionSetString(isa);
723*795d594fSAndroid Build Coastguard Worker std::string result, error_msg;
724*795d594fSAndroid Build Coastguard Worker bool ret = GetLocationEncodedFilename(location, dir, &result, &error_msg);
725*795d594fSAndroid Build Coastguard Worker // This should never fail. The function fails only if the location is not absolute, and a location
726*795d594fSAndroid Build Coastguard Worker // on /apex is always absolute.
727*795d594fSAndroid Build Coastguard Worker DCHECK(ret) << error_msg;
728*795d594fSAndroid Build Coastguard Worker std::string path = ReplaceFileExtension(result, kOdexExtension);
729*795d594fSAndroid Build Coastguard Worker if (OS::FileExists(path.c_str(), /*check_file_type=*/true)) {
730*795d594fSAndroid Build Coastguard Worker return path;
731*795d594fSAndroid Build Coastguard Worker }
732*795d594fSAndroid Build Coastguard Worker // check in /system_ext
733*795d594fSAndroid Build Coastguard Worker dir = GetSystemExtRoot() + "/framework/oat/" + GetInstructionSetString(isa);
734*795d594fSAndroid Build Coastguard Worker ret = GetLocationEncodedFilename(location, dir, &result, &error_msg);
735*795d594fSAndroid Build Coastguard Worker // This should never fail. The function fails only if the location is not absolute, and a location
736*795d594fSAndroid Build Coastguard Worker // on /apex is always absolute.
737*795d594fSAndroid Build Coastguard Worker DCHECK(ret) << error_msg;
738*795d594fSAndroid Build Coastguard Worker return ReplaceFileExtension(result, kOdexExtension);
739*795d594fSAndroid Build Coastguard Worker }
740*795d594fSAndroid Build Coastguard Worker
InsertIsaDirectory(const InstructionSet isa,std::string * filename)741*795d594fSAndroid Build Coastguard Worker static void InsertIsaDirectory(const InstructionSet isa, std::string* filename) {
742*795d594fSAndroid Build Coastguard Worker // in = /foo/bar/baz
743*795d594fSAndroid Build Coastguard Worker // out = /foo/bar/<isa>/baz
744*795d594fSAndroid Build Coastguard Worker size_t pos = filename->rfind('/');
745*795d594fSAndroid Build Coastguard Worker CHECK_NE(pos, std::string::npos) << *filename << " " << isa;
746*795d594fSAndroid Build Coastguard Worker filename->insert(pos, "/", 1);
747*795d594fSAndroid Build Coastguard Worker filename->insert(pos + 1, GetInstructionSetString(isa));
748*795d594fSAndroid Build Coastguard Worker }
749*795d594fSAndroid Build Coastguard Worker
GetSystemImageFilename(const char * location,const InstructionSet isa)750*795d594fSAndroid Build Coastguard Worker std::string GetSystemImageFilename(const char* location, const InstructionSet isa) {
751*795d594fSAndroid Build Coastguard Worker // location = /system/framework/boot.art
752*795d594fSAndroid Build Coastguard Worker // filename = /system/framework/<isa>/boot.art
753*795d594fSAndroid Build Coastguard Worker std::string filename(location);
754*795d594fSAndroid Build Coastguard Worker InsertIsaDirectory(isa, &filename);
755*795d594fSAndroid Build Coastguard Worker return filename;
756*795d594fSAndroid Build Coastguard Worker }
757*795d594fSAndroid Build Coastguard Worker
ReplaceFileExtension(std::string_view filename,std::string_view new_extension)758*795d594fSAndroid Build Coastguard Worker std::string ReplaceFileExtension(std::string_view filename, std::string_view new_extension) {
759*795d594fSAndroid Build Coastguard Worker ConsumePrefix(&new_extension, ".");
760*795d594fSAndroid Build Coastguard Worker const size_t last_ext = filename.find_last_of("./");
761*795d594fSAndroid Build Coastguard Worker std::string result;
762*795d594fSAndroid Build Coastguard Worker if (last_ext == std::string::npos || filename[last_ext] != '.') {
763*795d594fSAndroid Build Coastguard Worker result.reserve(filename.size() + 1 + new_extension.size());
764*795d594fSAndroid Build Coastguard Worker result.append(filename).append(".").append(new_extension);
765*795d594fSAndroid Build Coastguard Worker } else {
766*795d594fSAndroid Build Coastguard Worker result.reserve(last_ext + 1 + new_extension.size());
767*795d594fSAndroid Build Coastguard Worker result.append(filename.substr(0, last_ext + 1)).append(new_extension);
768*795d594fSAndroid Build Coastguard Worker }
769*795d594fSAndroid Build Coastguard Worker return result;
770*795d594fSAndroid Build Coastguard Worker }
771*795d594fSAndroid Build Coastguard Worker
LocationIsOnArtApexData(std::string_view location)772*795d594fSAndroid Build Coastguard Worker bool LocationIsOnArtApexData(std::string_view location) {
773*795d594fSAndroid Build Coastguard Worker const std::string art_apex_data = GetArtApexData();
774*795d594fSAndroid Build Coastguard Worker return location.starts_with(art_apex_data);
775*795d594fSAndroid Build Coastguard Worker }
776*795d594fSAndroid Build Coastguard Worker
LocationIsOnArtModule(std::string_view full_path)777*795d594fSAndroid Build Coastguard Worker bool LocationIsOnArtModule(std::string_view full_path) {
778*795d594fSAndroid Build Coastguard Worker std::string unused_error_msg;
779*795d594fSAndroid Build Coastguard Worker std::string module_path = GetArtRootSafe(/* must_exist= */ kIsTargetBuild, &unused_error_msg);
780*795d594fSAndroid Build Coastguard Worker if (module_path.empty()) {
781*795d594fSAndroid Build Coastguard Worker return false;
782*795d594fSAndroid Build Coastguard Worker }
783*795d594fSAndroid Build Coastguard Worker return full_path.starts_with(module_path);
784*795d594fSAndroid Build Coastguard Worker }
785*795d594fSAndroid Build Coastguard Worker
StartsWithSlash(const char * str)786*795d594fSAndroid Build Coastguard Worker static bool StartsWithSlash(const char* str) {
787*795d594fSAndroid Build Coastguard Worker DCHECK(str != nullptr);
788*795d594fSAndroid Build Coastguard Worker return str[0] == '/';
789*795d594fSAndroid Build Coastguard Worker }
790*795d594fSAndroid Build Coastguard Worker
EndsWithSlash(const char * str)791*795d594fSAndroid Build Coastguard Worker static bool EndsWithSlash(const char* str) {
792*795d594fSAndroid Build Coastguard Worker DCHECK(str != nullptr);
793*795d594fSAndroid Build Coastguard Worker size_t len = strlen(str);
794*795d594fSAndroid Build Coastguard Worker return len > 0 && str[len - 1] == '/';
795*795d594fSAndroid Build Coastguard Worker }
796*795d594fSAndroid Build Coastguard Worker
797*795d594fSAndroid Build Coastguard Worker // Returns true if `full_path` is located in folder either provided with `env_var`
798*795d594fSAndroid Build Coastguard Worker // or in `default_path` otherwise. The caller may optionally provide a `subdir`
799*795d594fSAndroid Build Coastguard Worker // which will be appended to the tested prefix.
800*795d594fSAndroid Build Coastguard Worker // `default_path` and the value of environment variable `env_var`
801*795d594fSAndroid Build Coastguard Worker // are expected to begin with a slash and not end with one. If this ever changes,
802*795d594fSAndroid Build Coastguard Worker // the path-building logic should be updated.
IsLocationOn(std::string_view full_path,const char * env_var,const char * default_path,const char * subdir=nullptr)803*795d594fSAndroid Build Coastguard Worker static bool IsLocationOn(std::string_view full_path,
804*795d594fSAndroid Build Coastguard Worker const char* env_var,
805*795d594fSAndroid Build Coastguard Worker const char* default_path,
806*795d594fSAndroid Build Coastguard Worker const char* subdir = nullptr) {
807*795d594fSAndroid Build Coastguard Worker std::string unused_error_msg;
808*795d594fSAndroid Build Coastguard Worker const char* path = GetAndroidDirSafe(env_var,
809*795d594fSAndroid Build Coastguard Worker default_path,
810*795d594fSAndroid Build Coastguard Worker /* must_exist= */ kIsTargetBuild,
811*795d594fSAndroid Build Coastguard Worker &unused_error_msg);
812*795d594fSAndroid Build Coastguard Worker if (path == nullptr) {
813*795d594fSAndroid Build Coastguard Worker return false;
814*795d594fSAndroid Build Coastguard Worker }
815*795d594fSAndroid Build Coastguard Worker
816*795d594fSAndroid Build Coastguard Worker // Build the path which we will check is a prefix of `full_path`. The prefix must
817*795d594fSAndroid Build Coastguard Worker // end with a slash, so that "/foo/bar" does not match "/foo/barz".
818*795d594fSAndroid Build Coastguard Worker DCHECK(StartsWithSlash(path)) << path;
819*795d594fSAndroid Build Coastguard Worker std::string path_prefix(path);
820*795d594fSAndroid Build Coastguard Worker if (!EndsWithSlash(path_prefix.c_str())) {
821*795d594fSAndroid Build Coastguard Worker path_prefix.append("/");
822*795d594fSAndroid Build Coastguard Worker }
823*795d594fSAndroid Build Coastguard Worker if (subdir != nullptr) {
824*795d594fSAndroid Build Coastguard Worker // If `subdir` is provided, we assume it is provided without a starting slash
825*795d594fSAndroid Build Coastguard Worker // but ending with one, e.g. "sub/dir/". `path_prefix` ends with a slash at
826*795d594fSAndroid Build Coastguard Worker // this point, so we simply append `subdir`.
827*795d594fSAndroid Build Coastguard Worker DCHECK(!StartsWithSlash(subdir) && EndsWithSlash(subdir)) << subdir;
828*795d594fSAndroid Build Coastguard Worker path_prefix.append(subdir);
829*795d594fSAndroid Build Coastguard Worker }
830*795d594fSAndroid Build Coastguard Worker
831*795d594fSAndroid Build Coastguard Worker return full_path.starts_with(path_prefix);
832*795d594fSAndroid Build Coastguard Worker }
833*795d594fSAndroid Build Coastguard Worker
LocationIsOnSystemFramework(std::string_view full_path)834*795d594fSAndroid Build Coastguard Worker bool LocationIsOnSystemFramework(std::string_view full_path) {
835*795d594fSAndroid Build Coastguard Worker return IsLocationOn(full_path,
836*795d594fSAndroid Build Coastguard Worker kAndroidRootEnvVar,
837*795d594fSAndroid Build Coastguard Worker kAndroidRootDefaultPath,
838*795d594fSAndroid Build Coastguard Worker /* subdir= */ "framework/");
839*795d594fSAndroid Build Coastguard Worker }
840*795d594fSAndroid Build Coastguard Worker
LocationIsOnSystemExtFramework(std::string_view full_path)841*795d594fSAndroid Build Coastguard Worker bool LocationIsOnSystemExtFramework(std::string_view full_path) {
842*795d594fSAndroid Build Coastguard Worker return IsLocationOn(full_path,
843*795d594fSAndroid Build Coastguard Worker kAndroidSystemExtRootEnvVar,
844*795d594fSAndroid Build Coastguard Worker kAndroidSystemExtRootDefaultPath,
845*795d594fSAndroid Build Coastguard Worker /* subdir= */ "framework/") ||
846*795d594fSAndroid Build Coastguard Worker // When the 'system_ext' partition is not present, builds will create
847*795d594fSAndroid Build Coastguard Worker // '/system/system_ext' instead.
848*795d594fSAndroid Build Coastguard Worker IsLocationOn(full_path,
849*795d594fSAndroid Build Coastguard Worker kAndroidRootEnvVar,
850*795d594fSAndroid Build Coastguard Worker kAndroidRootDefaultPath,
851*795d594fSAndroid Build Coastguard Worker /* subdir= */ "system_ext/framework/");
852*795d594fSAndroid Build Coastguard Worker }
853*795d594fSAndroid Build Coastguard Worker
LocationIsOnConscryptModule(std::string_view full_path)854*795d594fSAndroid Build Coastguard Worker bool LocationIsOnConscryptModule(std::string_view full_path) {
855*795d594fSAndroid Build Coastguard Worker return IsLocationOn(full_path, kAndroidConscryptRootEnvVar, kAndroidConscryptApexDefaultPath);
856*795d594fSAndroid Build Coastguard Worker }
857*795d594fSAndroid Build Coastguard Worker
LocationIsOnI18nModule(std::string_view full_path)858*795d594fSAndroid Build Coastguard Worker bool LocationIsOnI18nModule(std::string_view full_path) {
859*795d594fSAndroid Build Coastguard Worker return IsLocationOn(full_path, kAndroidI18nRootEnvVar, kAndroidI18nApexDefaultPath);
860*795d594fSAndroid Build Coastguard Worker }
861*795d594fSAndroid Build Coastguard Worker
LocationIsOnApex(std::string_view full_path)862*795d594fSAndroid Build Coastguard Worker bool LocationIsOnApex(std::string_view full_path) {
863*795d594fSAndroid Build Coastguard Worker return full_path.starts_with(kApexDefaultPath);
864*795d594fSAndroid Build Coastguard Worker }
865*795d594fSAndroid Build Coastguard Worker
ApexNameFromLocation(std::string_view full_path)866*795d594fSAndroid Build Coastguard Worker std::string_view ApexNameFromLocation(std::string_view full_path) {
867*795d594fSAndroid Build Coastguard Worker if (!full_path.starts_with(kApexDefaultPath)) {
868*795d594fSAndroid Build Coastguard Worker return {};
869*795d594fSAndroid Build Coastguard Worker }
870*795d594fSAndroid Build Coastguard Worker size_t start = strlen(kApexDefaultPath);
871*795d594fSAndroid Build Coastguard Worker size_t end = full_path.find('/', start);
872*795d594fSAndroid Build Coastguard Worker if (end == std::string_view::npos) {
873*795d594fSAndroid Build Coastguard Worker return {};
874*795d594fSAndroid Build Coastguard Worker }
875*795d594fSAndroid Build Coastguard Worker return full_path.substr(start, end - start);
876*795d594fSAndroid Build Coastguard Worker }
877*795d594fSAndroid Build Coastguard Worker
LocationIsOnSystem(const std::string & location)878*795d594fSAndroid Build Coastguard Worker bool LocationIsOnSystem(const std::string& location) {
879*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
880*795d594fSAndroid Build Coastguard Worker UNUSED(location);
881*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "LocationIsOnSystem is unsupported on Windows.";
882*795d594fSAndroid Build Coastguard Worker return false;
883*795d594fSAndroid Build Coastguard Worker #else
884*795d594fSAndroid Build Coastguard Worker return location.starts_with(GetAndroidRoot());
885*795d594fSAndroid Build Coastguard Worker #endif
886*795d594fSAndroid Build Coastguard Worker }
887*795d594fSAndroid Build Coastguard Worker
LocationIsOnSystemExt(const std::string & location)888*795d594fSAndroid Build Coastguard Worker bool LocationIsOnSystemExt(const std::string& location) {
889*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
890*795d594fSAndroid Build Coastguard Worker UNUSED(location);
891*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "LocationIsOnSystemExt is unsupported on Windows.";
892*795d594fSAndroid Build Coastguard Worker return false;
893*795d594fSAndroid Build Coastguard Worker #else
894*795d594fSAndroid Build Coastguard Worker return IsLocationOn(location,
895*795d594fSAndroid Build Coastguard Worker kAndroidSystemExtRootEnvVar,
896*795d594fSAndroid Build Coastguard Worker kAndroidSystemExtRootDefaultPath) ||
897*795d594fSAndroid Build Coastguard Worker // When the 'system_ext' partition is not present, builds will create
898*795d594fSAndroid Build Coastguard Worker // '/system/system_ext' instead.
899*795d594fSAndroid Build Coastguard Worker IsLocationOn(location,
900*795d594fSAndroid Build Coastguard Worker kAndroidRootEnvVar,
901*795d594fSAndroid Build Coastguard Worker kAndroidRootDefaultPath,
902*795d594fSAndroid Build Coastguard Worker /* subdir= */ "system_ext/");
903*795d594fSAndroid Build Coastguard Worker #endif
904*795d594fSAndroid Build Coastguard Worker }
905*795d594fSAndroid Build Coastguard Worker
LocationIsTrusted(const std::string & location,bool trust_art_apex_data_files)906*795d594fSAndroid Build Coastguard Worker bool LocationIsTrusted(const std::string& location, bool trust_art_apex_data_files) {
907*795d594fSAndroid Build Coastguard Worker if (LocationIsOnSystem(location) || LocationIsOnSystemExt(location)
908*795d594fSAndroid Build Coastguard Worker || LocationIsOnArtModule(location)) {
909*795d594fSAndroid Build Coastguard Worker return true;
910*795d594fSAndroid Build Coastguard Worker }
911*795d594fSAndroid Build Coastguard Worker return LocationIsOnArtApexData(location) & trust_art_apex_data_files;
912*795d594fSAndroid Build Coastguard Worker }
913*795d594fSAndroid Build Coastguard Worker
ArtModuleRootDistinctFromAndroidRoot()914*795d594fSAndroid Build Coastguard Worker bool ArtModuleRootDistinctFromAndroidRoot() {
915*795d594fSAndroid Build Coastguard Worker std::string error_msg;
916*795d594fSAndroid Build Coastguard Worker const char* android_root = GetAndroidDirSafe(kAndroidRootEnvVar,
917*795d594fSAndroid Build Coastguard Worker kAndroidRootDefaultPath,
918*795d594fSAndroid Build Coastguard Worker /* must_exist= */ kIsTargetBuild,
919*795d594fSAndroid Build Coastguard Worker &error_msg);
920*795d594fSAndroid Build Coastguard Worker const char* art_root = GetAndroidDirSafe(kAndroidArtRootEnvVar,
921*795d594fSAndroid Build Coastguard Worker kAndroidArtApexDefaultPath,
922*795d594fSAndroid Build Coastguard Worker /* must_exist= */ kIsTargetBuild,
923*795d594fSAndroid Build Coastguard Worker &error_msg);
924*795d594fSAndroid Build Coastguard Worker return (android_root != nullptr) && (art_root != nullptr) &&
925*795d594fSAndroid Build Coastguard Worker (std::string_view(android_root) != std::string_view(art_root));
926*795d594fSAndroid Build Coastguard Worker }
927*795d594fSAndroid Build Coastguard Worker
DupCloexec(int fd)928*795d594fSAndroid Build Coastguard Worker int DupCloexec(int fd) {
929*795d594fSAndroid Build Coastguard Worker #if defined(__linux__)
930*795d594fSAndroid Build Coastguard Worker return fcntl(fd, F_DUPFD_CLOEXEC, 0);
931*795d594fSAndroid Build Coastguard Worker #else
932*795d594fSAndroid Build Coastguard Worker return dup(fd); // NOLINT
933*795d594fSAndroid Build Coastguard Worker #endif
934*795d594fSAndroid Build Coastguard Worker }
935*795d594fSAndroid Build Coastguard Worker
936*795d594fSAndroid Build Coastguard Worker } // namespace art
937