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