1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "file_utils.h"
18 
19 #include <inttypes.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #ifndef _WIN32
23 #include <sys/wait.h>
24 #endif
25 #include <unistd.h>
26 
27 // We need dladdr.
28 #if !defined(__APPLE__) && !defined(_WIN32)
29 #ifndef _GNU_SOURCE
30 #define _GNU_SOURCE
31 #define DEFINED_GNU_SOURCE
32 #endif
33 #include <dlfcn.h>
34 #include <libgen.h>
35 #ifdef DEFINED_GNU_SOURCE
36 #undef _GNU_SOURCE
37 #undef DEFINED_GNU_SOURCE
38 #endif
39 #endif
40 
41 
42 #include <memory>
43 
44 #include "android-base/stringprintf.h"
45 #include "android-base/strings.h"
46 
47 #include "base/bit_utils.h"
48 #include "base/globals.h"
49 #include "base/os.h"
50 #include "base/stl_util.h"
51 #include "base/unix_file/fd_file.h"
52 
53 #if defined(__APPLE__)
54 #include <crt_externs.h>
55 #include <sys/syscall.h>
56 #include "AvailabilityMacros.h"  // For MAC_OS_X_VERSION_MAX_ALLOWED
57 #endif
58 
59 #if defined(__linux__)
60 #include <linux/unistd.h>
61 #endif
62 
63 namespace art {
64 
65 using android::base::StringPrintf;
66 
67 static constexpr const char* kClassesDex = "classes.dex";
68 static constexpr const char* kApexDefaultPath = "/apex/";
69 static constexpr const char* kAndroidRootEnvVar = "ANDROID_ROOT";
70 static constexpr const char* kAndroidRootDefaultPath = "/system";
71 static constexpr const char* kAndroidSystemExtRootEnvVar = "ANDROID_SYSTEM_EXT";
72 static constexpr const char* kAndroidSystemExtRootDefaultPath = "/system_ext";
73 static constexpr const char* kAndroidDataEnvVar = "ANDROID_DATA";
74 static constexpr const char* kAndroidDataDefaultPath = "/data";
75 static constexpr const char* kAndroidArtRootEnvVar = "ANDROID_ART_ROOT";
76 static constexpr const char* kAndroidConscryptRootEnvVar = "ANDROID_CONSCRYPT_ROOT";
77 static constexpr const char* kAndroidI18nRootEnvVar = "ANDROID_I18N_ROOT";
78 
79 // Get the "root" directory containing the "lib" directory where this instance
80 // of the libartbase library (which contains `GetRootContainingLibartbase`) is
81 // located:
82 // - on host this "root" is normally the Android Root (e.g. something like
83 //   "$ANDROID_BUILD_TOP/out/host/linux-x86/");
84 // - on target this "root" is normally the ART Root ("/apex/com.android.art").
85 // Return the empty string if that directory cannot be found or if this code is
86 // run on Windows or macOS.
GetRootContainingLibartbase()87 static std::string GetRootContainingLibartbase() {
88 #if !defined( _WIN32) && !defined(__APPLE__)
89   // Check where libartbase is from, and derive from there.
90   Dl_info info;
91   if (dladdr(reinterpret_cast<const void*>(&GetRootContainingLibartbase), /* out */ &info) != 0) {
92     // Make a duplicate of the fname so dirname can modify it.
93     UniqueCPtr<char> fname(strdup(info.dli_fname));
94 
95     char* dir1 = dirname(fname.get());  // This is the lib directory.
96     char* dir2 = dirname(dir1);         // This is the "root" directory.
97     if (OS::DirectoryExists(dir2)) {
98       std::string tmp = dir2;  // Make a copy here so that fname can be released.
99       return tmp;
100     }
101   }
102 #endif
103   return "";
104 }
105 
GetAndroidRootSafe(std::string * error_msg)106 std::string GetAndroidRootSafe(std::string* error_msg) {
107 #ifdef _WIN32
108   UNUSED(kAndroidRootEnvVar, kAndroidRootDefaultPath, GetRootContainingLibartbase);
109   *error_msg = "GetAndroidRootSafe unsupported for Windows.";
110   return "";
111 #else
112   // Prefer ANDROID_ROOT if it's set.
113   const char* android_root_from_env = getenv(kAndroidRootEnvVar);
114   if (android_root_from_env != nullptr) {
115     if (!OS::DirectoryExists(android_root_from_env)) {
116       *error_msg =
117           StringPrintf("Failed to find %s directory %s", kAndroidRootEnvVar, android_root_from_env);
118       return "";
119     }
120     return android_root_from_env;
121   }
122 
123   // On host, libartbase is currently installed in "$ANDROID_ROOT/lib"
124   // (e.g. something like "$ANDROID_BUILD_TOP/out/host/linux-x86/lib". Use this
125   // information to infer the location of the Android Root (on host only).
126   //
127   // Note that this could change in the future, if we decided to install ART
128   // artifacts in a different location, e.g. within an "ART APEX" directory.
129   if (!kIsTargetBuild) {
130     std::string root_containing_libartbase = GetRootContainingLibartbase();
131     if (!root_containing_libartbase.empty()) {
132       return root_containing_libartbase;
133     }
134   }
135 
136   // Try the default path.
137   if (!OS::DirectoryExists(kAndroidRootDefaultPath)) {
138     *error_msg =
139         StringPrintf("Failed to find default Android Root directory %s", kAndroidRootDefaultPath);
140     return "";
141   }
142   return kAndroidRootDefaultPath;
143 #endif
144 }
145 
GetAndroidRoot()146 std::string GetAndroidRoot() {
147   std::string error_msg;
148   std::string ret = GetAndroidRootSafe(&error_msg);
149   if (ret.empty()) {
150     LOG(FATAL) << error_msg;
151     UNREACHABLE();
152   }
153   return ret;
154 }
155 
156 
GetAndroidDirSafe(const char * env_var,const char * default_dir,bool must_exist,std::string * error_msg)157 static const char* GetAndroidDirSafe(const char* env_var,
158                                      const char* default_dir,
159                                      bool must_exist,
160                                      std::string* error_msg) {
161   const char* android_dir = getenv(env_var);
162   if (android_dir == nullptr) {
163     if (!must_exist || OS::DirectoryExists(default_dir)) {
164       android_dir = default_dir;
165     } else {
166       *error_msg = StringPrintf("%s not set and %s does not exist", env_var, default_dir);
167       return nullptr;
168     }
169   }
170   if (must_exist && !OS::DirectoryExists(android_dir)) {
171     *error_msg = StringPrintf("Failed to find directory %s", android_dir);
172     return nullptr;
173   }
174   return android_dir;
175 }
176 
GetAndroidDir(const char * env_var,const char * default_dir)177 static const char* GetAndroidDir(const char* env_var, const char* default_dir) {
178   std::string error_msg;
179   const char* dir = GetAndroidDirSafe(env_var, default_dir, /* must_exist= */ true, &error_msg);
180   if (dir != nullptr) {
181     return dir;
182   } else {
183     LOG(FATAL) << error_msg;
184     UNREACHABLE();
185   }
186 }
187 
GetArtRootSafe(bool must_exist,std::string * error_msg)188 static std::string GetArtRootSafe(bool must_exist, /*out*/ std::string* error_msg) {
189 #ifdef _WIN32
190   UNUSED(kAndroidArtRootEnvVar, kAndroidArtApexDefaultPath, GetRootContainingLibartbase);
191   UNUSED(must_exist);
192   *error_msg = "GetArtRootSafe unsupported for Windows.";
193   return "";
194 #else
195   // Prefer ANDROID_ART_ROOT if it's set.
196   const char* android_art_root_from_env = getenv(kAndroidArtRootEnvVar);
197   if (android_art_root_from_env != nullptr) {
198     if (must_exist && !OS::DirectoryExists(android_art_root_from_env)) {
199       *error_msg = StringPrintf("Failed to find %s directory %s",
200                                 kAndroidArtRootEnvVar,
201                                 android_art_root_from_env);
202       return "";
203     }
204     return android_art_root_from_env;
205   }
206 
207   // On target, libartbase is normally installed in
208   // "$ANDROID_ART_ROOT/lib(64)" (e.g. something like
209   // "/apex/com.android.art/lib(64)". Use this information to infer the
210   // location of the ART Root (on target only).
211   if (kIsTargetBuild) {
212     // *However*, a copy of libartbase may still be installed outside the
213     // ART Root on some occasions, as ART target gtests install their binaries
214     // and their dependencies under the Android Root, i.e. "/system" (see
215     // b/129534335). For that reason, we cannot reliably use
216     // `GetRootContainingLibartbase` to find the ART Root. (Note that this is
217     // not really a problem in practice, as Android Q devices define
218     // ANDROID_ART_ROOT in their default environment, and will instead use
219     // the logic above anyway.)
220     //
221     // TODO(b/129534335): Re-enable this logic when the only instance of
222     // libartbase on target is the one from the ART APEX.
223     if ((false)) {
224       std::string root_containing_libartbase = GetRootContainingLibartbase();
225       if (!root_containing_libartbase.empty()) {
226         return root_containing_libartbase;
227       }
228     }
229   }
230 
231   // Try the default path.
232   if (must_exist && !OS::DirectoryExists(kAndroidArtApexDefaultPath)) {
233     *error_msg = StringPrintf("Failed to find default ART root directory %s",
234                               kAndroidArtApexDefaultPath);
235     return "";
236   }
237   return kAndroidArtApexDefaultPath;
238 #endif
239 }
240 
GetArtRootSafe(std::string * error_msg)241 std::string GetArtRootSafe(std::string* error_msg) {
242   return GetArtRootSafe(/* must_exist= */ true, error_msg);
243 }
244 
GetArtRoot()245 std::string GetArtRoot() {
246   std::string error_msg;
247   std::string ret = GetArtRootSafe(&error_msg);
248   if (ret.empty()) {
249     LOG(FATAL) << error_msg;
250     UNREACHABLE();
251   }
252   return ret;
253 }
254 
GetArtBinDir()255 std::string GetArtBinDir() {
256   // Environment variable `ANDROID_ART_ROOT` is defined as
257   // `$ANDROID_HOST_OUT/com.android.art` on host. However, host ART binaries are
258   // still installed in `$ANDROID_HOST_OUT/bin` (i.e. outside the ART Root). The
259   // situation is cleaner on target, where `ANDROID_ART_ROOT` is
260   // `$ANDROID_ROOT/apex/com.android.art` and ART binaries are installed in
261   // `$ANDROID_ROOT/apex/com.android.art/bin`.
262   std::string android_art_root = kIsTargetBuild ? GetArtRoot() : GetAndroidRoot();
263   return android_art_root + "/bin";
264 }
265 
GetAndroidDataSafe(std::string * error_msg)266 std::string GetAndroidDataSafe(std::string* error_msg) {
267   const char* android_dir = GetAndroidDirSafe(kAndroidDataEnvVar,
268                                               kAndroidDataDefaultPath,
269                                               /* must_exist= */ true,
270                                               error_msg);
271   return (android_dir != nullptr) ? android_dir : "";
272 }
273 
GetAndroidData()274 std::string GetAndroidData() {
275   return GetAndroidDir(kAndroidDataEnvVar, kAndroidDataDefaultPath);
276 }
277 
GetDefaultBootImageLocation(const std::string & android_root)278 std::string GetDefaultBootImageLocation(const std::string& android_root) {
279   // Boot image consists of two parts:
280   //  - the primary boot image in the ART apex (contains the Core Libraries)
281   //  - the boot image extension on the system partition (contains framework libraries)
282   return StringPrintf("%s/javalib/boot.art:%s/framework/boot-framework.art!%s/etc/boot-image.prof",
283                       kAndroidArtApexDefaultPath,
284                       android_root.c_str(),
285                       android_root.c_str());
286 }
287 
GetDefaultBootImageLocation(std::string * error_msg)288 std::string GetDefaultBootImageLocation(std::string* error_msg) {
289   std::string android_root = GetAndroidRootSafe(error_msg);
290   if (android_root.empty()) {
291     return "";
292   }
293   return GetDefaultBootImageLocation(android_root);
294 }
295 
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)296 void GetDalvikCache(const char* subdir, const bool create_if_absent, std::string* dalvik_cache,
297                     bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache) {
298 #ifdef _WIN32
299   UNUSED(subdir);
300   UNUSED(create_if_absent);
301   UNUSED(dalvik_cache);
302   UNUSED(have_android_data);
303   UNUSED(dalvik_cache_exists);
304   UNUSED(is_global_cache);
305   LOG(FATAL) << "GetDalvikCache unsupported on Windows.";
306 #else
307   CHECK(subdir != nullptr);
308   std::string unused_error_msg;
309   std::string android_data = GetAndroidDataSafe(&unused_error_msg);
310   if (android_data.empty()) {
311     *have_android_data = false;
312     *dalvik_cache_exists = false;
313     *is_global_cache = false;
314     return;
315   } else {
316     *have_android_data = true;
317   }
318   const std::string dalvik_cache_root = android_data + "/dalvik-cache";
319   *dalvik_cache = dalvik_cache_root + '/' + subdir;
320   *dalvik_cache_exists = OS::DirectoryExists(dalvik_cache->c_str());
321   *is_global_cache = (android_data == kAndroidDataDefaultPath);
322   if (create_if_absent && !*dalvik_cache_exists && !*is_global_cache) {
323     // Don't create the system's /data/dalvik-cache/... because it needs special permissions.
324     *dalvik_cache_exists = ((mkdir(dalvik_cache_root.c_str(), 0700) == 0 || errno == EEXIST) &&
325                             (mkdir(dalvik_cache->c_str(), 0700) == 0 || errno == EEXIST));
326   }
327 #endif
328 }
329 
GetDalvikCache(const char * subdir)330 std::string GetDalvikCache(const char* subdir) {
331   CHECK(subdir != nullptr);
332   std::string android_data = GetAndroidData();
333   const std::string dalvik_cache_root = android_data + "/dalvik-cache";
334   const std::string dalvik_cache = dalvik_cache_root + '/' + subdir;
335   if (!OS::DirectoryExists(dalvik_cache.c_str())) {
336     // TODO: Check callers. Traditional behavior is to not abort.
337     return "";
338   }
339   return dalvik_cache;
340 }
341 
GetDalvikCacheFilename(const char * location,const char * cache_location,std::string * filename,std::string * error_msg)342 bool GetDalvikCacheFilename(const char* location, const char* cache_location,
343                             std::string* filename, std::string* error_msg) {
344   if (location[0] != '/') {
345     *error_msg = StringPrintf("Expected path in location to be absolute: %s", location);
346     return false;
347   }
348   std::string cache_file(&location[1]);  // skip leading slash
349   if (!android::base::EndsWith(location, ".dex") &&
350       !android::base::EndsWith(location, ".art") &&
351       !android::base::EndsWith(location, ".oat")) {
352     cache_file += "/";
353     cache_file += kClassesDex;
354   }
355   std::replace(cache_file.begin(), cache_file.end(), '/', '@');
356   *filename = StringPrintf("%s/%s", cache_location, cache_file.c_str());
357   return true;
358 }
359 
GetVdexFilename(const std::string & oat_location)360 std::string GetVdexFilename(const std::string& oat_location) {
361   return ReplaceFileExtension(oat_location, "vdex");
362 }
363 
InsertIsaDirectory(const InstructionSet isa,std::string * filename)364 static void InsertIsaDirectory(const InstructionSet isa, std::string* filename) {
365   // in = /foo/bar/baz
366   // out = /foo/bar/<isa>/baz
367   size_t pos = filename->rfind('/');
368   CHECK_NE(pos, std::string::npos) << *filename << " " << isa;
369   filename->insert(pos, "/", 1);
370   filename->insert(pos + 1, GetInstructionSetString(isa));
371 }
372 
GetSystemImageFilename(const char * location,const InstructionSet isa)373 std::string GetSystemImageFilename(const char* location, const InstructionSet isa) {
374   // location = /system/framework/boot.art
375   // filename = /system/framework/<isa>/boot.art
376   std::string filename(location);
377   InsertIsaDirectory(isa, &filename);
378   return filename;
379 }
380 
ReplaceFileExtension(const std::string & filename,const std::string & new_extension)381 std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension) {
382   const size_t last_ext = filename.find_last_of("./");
383   if (last_ext == std::string::npos || filename[last_ext] != '.') {
384     return filename + "." + new_extension;
385   } else {
386     return filename.substr(0, last_ext + 1) + new_extension;
387   }
388 }
389 
LocationIsOnArtModule(const char * full_path)390 bool LocationIsOnArtModule(const char* full_path) {
391   std::string unused_error_msg;
392   std::string module_path = GetArtRootSafe(/* must_exist= */ kIsTargetBuild, &unused_error_msg);
393   if (module_path.empty()) {
394     return false;
395   }
396   return android::base::StartsWith(full_path, module_path);
397 }
398 
StartsWithSlash(const char * str)399 static bool StartsWithSlash(const char* str) {
400   DCHECK(str != nullptr);
401   return str[0] == '/';
402 }
403 
EndsWithSlash(const char * str)404 static bool EndsWithSlash(const char* str) {
405   DCHECK(str != nullptr);
406   size_t len = strlen(str);
407   return len > 0 && str[len - 1] == '/';
408 }
409 
410 // Returns true if `full_path` is located in folder either provided with `env_var`
411 // or in `default_path` otherwise. The caller may optionally provide a `subdir`
412 // which will be appended to the tested prefix.
413 // All of `default_path`, `subdir` and the value of environment variable `env_var`
414 // are expected to begin with a slash and not end with one. If this ever changes,
415 // the path-building logic should be updated.
IsLocationOnModule(const char * full_path,const char * env_var,const char * default_path,const char * subdir=nullptr)416 static bool IsLocationOnModule(const char* full_path,
417                                const char* env_var,
418                                const char* default_path,
419                                const char* subdir = nullptr) {
420   std::string unused_error_msg;
421   const char* module_path = GetAndroidDirSafe(env_var,
422                                               default_path,
423                                               /* must_exist= */ kIsTargetBuild,
424                                               &unused_error_msg);
425   if (module_path == nullptr) {
426     return false;
427   }
428 
429   // Build the path which we will check is a prefix of `full_path`. The prefix must
430   // end with a slash, so that "/foo/bar" does not match "/foo/barz".
431   DCHECK(StartsWithSlash(module_path)) << module_path;
432   std::string path_prefix(module_path);
433   if (!EndsWithSlash(path_prefix.c_str())) {
434     path_prefix.append("/");
435   }
436   if (subdir != nullptr) {
437     // If `subdir` is provided, we assume it is provided without a starting slash
438     // but ending with one, e.g. "sub/dir/". `path_prefix` ends with a slash at
439     // this point, so we simply append `subdir`.
440     DCHECK(!StartsWithSlash(subdir) && EndsWithSlash(subdir)) << subdir;
441     path_prefix.append(subdir);
442   }
443 
444   return android::base::StartsWith(full_path, path_prefix);
445 }
446 
LocationIsOnSystemFramework(const char * full_path)447 bool LocationIsOnSystemFramework(const char* full_path) {
448   return IsLocationOnModule(full_path,
449                             kAndroidRootEnvVar,
450                             kAndroidRootDefaultPath,
451                             /* subdir= */ "framework/");
452 }
453 
LocationIsOnSystemExtFramework(const char * full_path)454 bool LocationIsOnSystemExtFramework(const char* full_path) {
455   return IsLocationOnModule(full_path,
456                             kAndroidSystemExtRootEnvVar,
457                             kAndroidSystemExtRootDefaultPath,
458                             /* subdir= */ "framework/");
459 }
460 
LocationIsOnConscryptModule(const char * full_path)461 bool LocationIsOnConscryptModule(const char* full_path) {
462   return IsLocationOnModule(
463       full_path, kAndroidConscryptRootEnvVar, kAndroidConscryptApexDefaultPath);
464 }
465 
LocationIsOnI18nModule(const char * full_path)466 bool LocationIsOnI18nModule(const char* full_path) {
467   return IsLocationOnModule(
468       full_path, kAndroidI18nRootEnvVar, kAndroidI18nApexDefaultPath);
469 }
470 
LocationIsOnApex(const char * full_path)471 bool LocationIsOnApex(const char* full_path) {
472   return android::base::StartsWith(full_path, kApexDefaultPath);
473 }
474 
LocationIsOnSystem(const char * path)475 bool LocationIsOnSystem(const char* path) {
476 #ifdef _WIN32
477   UNUSED(path);
478   LOG(FATAL) << "LocationIsOnSystem is unsupported on Windows.";
479   return false;
480 #else
481   UniqueCPtr<const char[]> full_path(realpath(path, nullptr));
482   return full_path != nullptr &&
483       android::base::StartsWith(full_path.get(), GetAndroidRoot().c_str());
484 #endif
485 }
486 
ArtModuleRootDistinctFromAndroidRoot()487 bool ArtModuleRootDistinctFromAndroidRoot() {
488   std::string error_msg;
489   const char* android_root = GetAndroidDirSafe(kAndroidRootEnvVar,
490                                                kAndroidRootDefaultPath,
491                                                /* must_exist= */ kIsTargetBuild,
492                                                &error_msg);
493   const char* art_root = GetAndroidDirSafe(kAndroidArtRootEnvVar,
494                                            kAndroidArtApexDefaultPath,
495                                            /* must_exist= */ kIsTargetBuild,
496                                            &error_msg);
497   return (android_root != nullptr)
498       && (art_root != nullptr)
499       && (std::string_view(android_root) != std::string_view(art_root));
500 }
501 
DupCloexec(int fd)502 int DupCloexec(int fd) {
503 #if defined(__linux__)
504   return fcntl(fd, F_DUPFD_CLOEXEC, 0);
505 #else
506   return dup(fd);
507 #endif
508 }
509 
510 }  // namespace art
511