otapreopt.cpp revision 12375069e753adea6c123fca7706b1018d358c92
1/*
2 ** Copyright 2016, 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 <algorithm>
18#include <inttypes.h>
19#include <limits>
20#include <random>
21#include <regex>
22#include <selinux/android.h>
23#include <selinux/avc.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/capability.h>
27#include <sys/prctl.h>
28#include <sys/stat.h>
29#include <sys/wait.h>
30
31#include <android-base/logging.h>
32#include <android-base/macros.h>
33#include <android-base/stringprintf.h>
34#include <android-base/strings.h>
35#include <cutils/fs.h>
36#include <cutils/properties.h>
37#include <dex2oat_return_codes.h>
38#include <log/log.h>
39#include <private/android_filesystem_config.h>
40
41#include "dexopt.h"
42#include "file_parsing.h"
43#include "globals.h"
44#include "installd_constants.h"
45#include "installd_deps.h"  // Need to fill in requirements of commands.
46#include "otapreopt_utils.h"
47#include "system_properties.h"
48#include "utils.h"
49
50#ifndef LOG_TAG
51#define LOG_TAG "otapreopt"
52#endif
53
54#define BUFFER_MAX    1024  /* input buffer for commands */
55#define TOKEN_MAX     16    /* max number of arguments in buffer */
56#define REPLY_MAX     256   /* largest reply allowed */
57
58using android::base::EndsWith;
59using android::base::Join;
60using android::base::Split;
61using android::base::StartsWith;
62using android::base::StringPrintf;
63
64namespace android {
65namespace installd {
66
67template<typename T>
68static constexpr T RoundDown(T x, typename std::decay<T>::type n) {
69    return DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))(x & -n);
70}
71
72template<typename T>
73static constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) {
74    return RoundDown(x + n - 1, n);
75}
76
77class OTAPreoptService {
78 public:
79    // Main driver. Performs the following steps.
80    //
81    // 1) Parse options (read system properties etc from B partition).
82    //
83    // 2) Read in package data.
84    //
85    // 3) Prepare environment variables.
86    //
87    // 4) Prepare(compile) boot image, if necessary.
88    //
89    // 5) Run update.
90    int Main(int argc, char** argv) {
91        if (!ReadArguments(argc, argv)) {
92            LOG(ERROR) << "Failed reading command line.";
93            return 1;
94        }
95
96        if (!ReadSystemProperties()) {
97            LOG(ERROR)<< "Failed reading system properties.";
98            return 2;
99        }
100
101        if (!ReadEnvironment()) {
102            LOG(ERROR) << "Failed reading environment properties.";
103            return 3;
104        }
105
106        if (!CheckAndInitializeInstalldGlobals()) {
107            LOG(ERROR) << "Failed initializing globals.";
108            return 4;
109        }
110
111        PrepareEnvironment();
112
113        if (!PrepareBootImage(/* force */ false)) {
114            LOG(ERROR) << "Failed preparing boot image.";
115            return 5;
116        }
117
118        int dexopt_retcode = RunPreopt();
119
120        return dexopt_retcode;
121    }
122
123    int GetProperty(const char* key, char* value, const char* default_value) const {
124        const std::string* prop_value = system_properties_.GetProperty(key);
125        if (prop_value == nullptr) {
126            if (default_value == nullptr) {
127                return 0;
128            }
129            // Copy in the default value.
130            strncpy(value, default_value, kPropertyValueMax - 1);
131            value[kPropertyValueMax - 1] = 0;
132            return strlen(default_value);// TODO: Need to truncate?
133        }
134        size_t size = std::min(kPropertyValueMax - 1, prop_value->length());
135        strncpy(value, prop_value->data(), size);
136        value[size] = 0;
137        return static_cast<int>(size);
138    }
139
140    std::string GetOTADataDirectory() const {
141        return StringPrintf("%s/%s", GetOtaDirectoryPrefix().c_str(), target_slot_.c_str());
142    }
143
144    const std::string& GetTargetSlot() const {
145        return target_slot_;
146    }
147
148private:
149
150    struct Parameters {
151        const char *apk_path;
152        uid_t uid;
153        const char *pkgName;
154        const char *instruction_set;
155        int dexopt_needed;
156        const char* oat_dir;
157        int dexopt_flags;
158        const char* compiler_filter;
159        const char* volume_uuid;
160        const char* shared_libraries;
161        const char* se_info;
162    };
163
164    bool ReadSystemProperties() {
165        static constexpr const char* kPropertyFiles[] = {
166                "/default.prop", "/system/build.prop"
167        };
168
169        for (size_t i = 0; i < arraysize(kPropertyFiles); ++i) {
170            if (!system_properties_.Load(kPropertyFiles[i])) {
171                return false;
172            }
173        }
174
175        return true;
176    }
177
178    bool ReadEnvironment() {
179        // Parse the environment variables from init.environ.rc, which have the form
180        //   export NAME VALUE
181        // For simplicity, don't respect string quotation. The values we are interested in can be
182        // encoded without them.
183        std::regex export_regex("\\s*export\\s+(\\S+)\\s+(\\S+)");
184        bool parse_result = ParseFile("/init.environ.rc", [&](const std::string& line) {
185            std::smatch export_match;
186            if (!std::regex_match(line, export_match, export_regex)) {
187                return true;
188            }
189
190            if (export_match.size() != 3) {
191                return true;
192            }
193
194            std::string name = export_match[1].str();
195            std::string value = export_match[2].str();
196
197            system_properties_.SetProperty(name, value);
198
199            return true;
200        });
201        if (!parse_result) {
202            return false;
203        }
204
205        if (system_properties_.GetProperty(kAndroidDataPathPropertyName) == nullptr) {
206            return false;
207        }
208        android_data_ = *system_properties_.GetProperty(kAndroidDataPathPropertyName);
209
210        if (system_properties_.GetProperty(kAndroidRootPathPropertyName) == nullptr) {
211            return false;
212        }
213        android_root_ = *system_properties_.GetProperty(kAndroidRootPathPropertyName);
214
215        if (system_properties_.GetProperty(kBootClassPathPropertyName) == nullptr) {
216            return false;
217        }
218        boot_classpath_ = *system_properties_.GetProperty(kBootClassPathPropertyName);
219
220        if (system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME) == nullptr) {
221            return false;
222        }
223        asec_mountpoint_ = *system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME);
224
225        return true;
226    }
227
228    const std::string& GetAndroidData() const {
229        return android_data_;
230    }
231
232    const std::string& GetAndroidRoot() const {
233        return android_root_;
234    }
235
236    const std::string GetOtaDirectoryPrefix() const {
237        return GetAndroidData() + "/ota";
238    }
239
240    bool CheckAndInitializeInstalldGlobals() {
241        // init_globals_from_data_and_root requires "ASEC_MOUNTPOINT" in the environment. We
242        // do not use any datapath that includes this, but we'll still have to set it.
243        CHECK(system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME) != nullptr);
244        int result = setenv(ASEC_MOUNTPOINT_ENV_NAME, asec_mountpoint_.c_str(), 0);
245        if (result != 0) {
246            LOG(ERROR) << "Could not set ASEC_MOUNTPOINT environment variable";
247            return false;
248        }
249
250        if (!init_globals_from_data_and_root(GetAndroidData().c_str(), GetAndroidRoot().c_str())) {
251            LOG(ERROR) << "Could not initialize globals; exiting.";
252            return false;
253        }
254
255        // This is different from the normal installd. We only do the base
256        // directory, the rest will be created on demand when each app is compiled.
257        if (access(GetOtaDirectoryPrefix().c_str(), R_OK) < 0) {
258            LOG(ERROR) << "Could not access " << GetOtaDirectoryPrefix();
259            return false;
260        }
261
262        return true;
263    }
264
265    bool ParseUInt(const char* in, uint32_t* out) {
266        char* end;
267        long long int result = strtoll(in, &end, 0);
268        if (in == end || *end != '\0') {
269            return false;
270        }
271        if (result < std::numeric_limits<uint32_t>::min() ||
272                std::numeric_limits<uint32_t>::max() < result) {
273            return false;
274        }
275        *out = static_cast<uint32_t>(result);
276        return true;
277    }
278
279    bool ReadArguments(int argc, char** argv) {
280        // Expected command line:
281        //   target-slot [version] dexopt {DEXOPT_PARAMETERS}
282
283        const char* target_slot_arg = argv[1];
284        if (target_slot_arg == nullptr) {
285            LOG(ERROR) << "Missing parameters";
286            return false;
287        }
288        // Sanitize value. Only allow (a-zA-Z0-9_)+.
289        target_slot_ = target_slot_arg;
290        if (!ValidateTargetSlotSuffix(target_slot_)) {
291            LOG(ERROR) << "Target slot suffix not legal: " << target_slot_;
292            return false;
293        }
294
295        // Check for version or "dexopt" next.
296        if (argv[2] == nullptr) {
297            LOG(ERROR) << "Missing parameters";
298            return false;
299        }
300
301        if (std::string("dexopt").compare(argv[2]) == 0) {
302            // This is version 1 (N) or pre-versioning version 2.
303            constexpr int kV2ArgCount =   1   // "otapreopt"
304                                        + 1   // slot
305                                        + 1   // "dexopt"
306                                        + 1   // apk_path
307                                        + 1   // uid
308                                        + 1   // pkg
309                                        + 1   // isa
310                                        + 1   // dexopt_needed
311                                        + 1   // oat_dir
312                                        + 1   // dexopt_flags
313                                        + 1   // filter
314                                        + 1   // volume
315                                        + 1   // libs
316                                        + 1;  // seinfo
317            if (argc == kV2ArgCount) {
318                return ReadArgumentsV2(argc, argv, false);
319            } else {
320                return ReadArgumentsV1(argc, argv);
321            }
322        }
323
324        uint32_t version;
325        if (!ParseUInt(argv[2], &version)) {
326            LOG(ERROR) << "Could not parse version: " << argv[2];
327            return false;
328        }
329
330        switch (version) {
331            case 2:
332                return ReadArgumentsV2(argc, argv, true);
333
334            default:
335                LOG(ERROR) << "Unsupported version " << version;
336                return false;
337        }
338    }
339
340    bool ReadArgumentsV2(int argc ATTRIBUTE_UNUSED, char** argv, bool versioned) {
341        size_t dexopt_index = versioned ? 3 : 2;
342
343        // Check for "dexopt".
344        if (argv[dexopt_index] == nullptr) {
345            LOG(ERROR) << "Missing parameters";
346            return false;
347        }
348        if (std::string("dexopt").compare(argv[dexopt_index]) != 0) {
349            LOG(ERROR) << "Expected \"dexopt\"";
350            return false;
351        }
352
353        size_t param_index = 0;
354        for (;; ++param_index) {
355            const char* param = argv[dexopt_index + 1 + param_index];
356            if (param == nullptr) {
357                break;
358            }
359
360            switch (param_index) {
361                case 0:
362                    package_parameters_.apk_path = param;
363                    break;
364
365                case 1:
366                    package_parameters_.uid = atoi(param);
367                    break;
368
369                case 2:
370                    package_parameters_.pkgName = param;
371                    break;
372
373                case 3:
374                    package_parameters_.instruction_set = param;
375                    break;
376
377                case 4:
378                    package_parameters_.dexopt_needed = atoi(param);
379                    break;
380
381                case 5:
382                    package_parameters_.oat_dir = param;
383                    break;
384
385                case 6:
386                    package_parameters_.dexopt_flags = atoi(param);
387                    break;
388
389                case 7:
390                    package_parameters_.compiler_filter = param;
391                    break;
392
393                case 8:
394                    package_parameters_.volume_uuid = ParseNull(param);
395                    break;
396
397                case 9:
398                    package_parameters_.shared_libraries = ParseNull(param);
399                    break;
400
401                case 10:
402                    package_parameters_.se_info = ParseNull(param);
403                    break;
404
405                default:
406                    LOG(ERROR) << "Too many arguments, got " << param;
407                    return false;
408            }
409        }
410
411        if (param_index != 11) {
412            LOG(ERROR) << "Not enough parameters";
413            return false;
414        }
415
416        return true;
417    }
418
419    static int ReplaceMask(int input, int old_mask, int new_mask) {
420        return (input & old_mask) != 0 ? new_mask : 0;
421    }
422
423    bool ReadArgumentsV1(int argc ATTRIBUTE_UNUSED, char** argv) {
424        // Check for "dexopt".
425        if (argv[2] == nullptr) {
426            LOG(ERROR) << "Missing parameters";
427            return false;
428        }
429        if (std::string("dexopt").compare(argv[2]) != 0) {
430            LOG(ERROR) << "Expected \"dexopt\"";
431            return false;
432        }
433
434        size_t param_index = 0;
435        for (;; ++param_index) {
436            const char* param = argv[3 + param_index];
437            if (param == nullptr) {
438                break;
439            }
440
441            switch (param_index) {
442                case 0:
443                    package_parameters_.apk_path = param;
444                    break;
445
446                case 1:
447                    package_parameters_.uid = atoi(param);
448                    break;
449
450                case 2:
451                    package_parameters_.pkgName = param;
452                    break;
453
454                case 3:
455                    package_parameters_.instruction_set = param;
456                    break;
457
458                case 4: {
459                    // Version 1 had:
460                    //   DEXOPT_DEX2OAT_NEEDED       = 1
461                    //   DEXOPT_PATCHOAT_NEEDED      = 2
462                    //   DEXOPT_SELF_PATCHOAT_NEEDED = 3
463                    // We will simply use DEX2OAT_FROM_SCRATCH.
464                    package_parameters_.dexopt_needed = DEX2OAT_FROM_SCRATCH;
465                    break;
466                }
467
468                case 5:
469                    package_parameters_.oat_dir = param;
470                    break;
471
472                case 6: {
473                    // Version 1 had:
474                    constexpr int OLD_DEXOPT_PUBLIC         = 1 << 1;
475                    constexpr int OLD_DEXOPT_SAFEMODE       = 1 << 2;
476                    constexpr int OLD_DEXOPT_DEBUGGABLE     = 1 << 3;
477                    constexpr int OLD_DEXOPT_BOOTCOMPLETE   = 1 << 4;
478                    constexpr int OLD_DEXOPT_PROFILE_GUIDED = 1 << 5;
479                    constexpr int OLD_DEXOPT_OTA            = 1 << 6;
480                    int input = atoi(param);
481                    package_parameters_.dexopt_flags =
482                            ReplaceMask(input, OLD_DEXOPT_PUBLIC, DEXOPT_PUBLIC) |
483                            ReplaceMask(input, OLD_DEXOPT_SAFEMODE, DEXOPT_SAFEMODE) |
484                            ReplaceMask(input, OLD_DEXOPT_DEBUGGABLE, DEXOPT_DEBUGGABLE) |
485                            ReplaceMask(input, OLD_DEXOPT_BOOTCOMPLETE, DEXOPT_BOOTCOMPLETE) |
486                            ReplaceMask(input, OLD_DEXOPT_PROFILE_GUIDED, DEXOPT_PROFILE_GUIDED) |
487                            ReplaceMask(input, OLD_DEXOPT_OTA, 0);
488                    break;
489                }
490
491                case 7:
492                    package_parameters_.compiler_filter = param;
493                    break;
494
495                case 8:
496                    package_parameters_.volume_uuid = ParseNull(param);
497                    break;
498
499                case 9:
500                    package_parameters_.shared_libraries = ParseNull(param);
501                    break;
502
503                default:
504                    LOG(ERROR) << "Too many arguments, got " << param;
505                    return false;
506            }
507        }
508
509        if (param_index != 10) {
510            LOG(ERROR) << "Not enough parameters";
511            return false;
512        }
513
514        // Set se_info to null. It is only relevant for secondary dex files, which we won't
515        // receive from a v1 A side.
516        package_parameters_.se_info = nullptr;
517
518        return true;
519    }
520
521    void PrepareEnvironment() {
522        environ_.push_back(StringPrintf("BOOTCLASSPATH=%s", boot_classpath_.c_str()));
523        environ_.push_back(StringPrintf("ANDROID_DATA=%s", GetOTADataDirectory().c_str()));
524        environ_.push_back(StringPrintf("ANDROID_ROOT=%s", android_root_.c_str()));
525
526        for (const std::string& e : environ_) {
527            putenv(const_cast<char*>(e.c_str()));
528        }
529    }
530
531    // Ensure that we have the right boot image. The first time any app is
532    // compiled, we'll try to generate it.
533    bool PrepareBootImage(bool force) const {
534        if (package_parameters_.instruction_set == nullptr) {
535            LOG(ERROR) << "Instruction set missing.";
536            return false;
537        }
538        const char* isa = package_parameters_.instruction_set;
539
540        // Check whether the file exists where expected.
541        std::string dalvik_cache = GetOTADataDirectory() + "/" + DALVIK_CACHE;
542        std::string isa_path = dalvik_cache + "/" + isa;
543        std::string art_path = isa_path + "/system@framework@boot.art";
544        std::string oat_path = isa_path + "/system@framework@boot.oat";
545        bool cleared = false;
546        if (access(art_path.c_str(), F_OK) == 0 && access(oat_path.c_str(), F_OK) == 0) {
547            // Files exist, assume everything is alright if not forced. Otherwise clean up.
548            if (!force) {
549                return true;
550            }
551            ClearDirectory(isa_path);
552            cleared = true;
553        }
554
555        // Reset umask in otapreopt, so that we control the the access for the files we create.
556        umask(0);
557
558        // Create the directories, if necessary.
559        if (access(dalvik_cache.c_str(), F_OK) != 0) {
560            if (!CreatePath(dalvik_cache)) {
561                PLOG(ERROR) << "Could not create dalvik-cache dir " << dalvik_cache;
562                return false;
563            }
564        }
565        if (access(isa_path.c_str(), F_OK) != 0) {
566            if (!CreatePath(isa_path)) {
567                PLOG(ERROR) << "Could not create dalvik-cache isa dir";
568                return false;
569            }
570        }
571
572        // Prepare to create.
573        if (!cleared) {
574            ClearDirectory(isa_path);
575        }
576
577        std::string preopted_boot_art_path = StringPrintf("/system/framework/%s/boot.art", isa);
578        if (access(preopted_boot_art_path.c_str(), F_OK) == 0) {
579          return PatchoatBootImage(art_path, isa);
580        } else {
581          // No preopted boot image. Try to compile.
582          return Dex2oatBootImage(boot_classpath_, art_path, oat_path, isa);
583        }
584    }
585
586    static bool CreatePath(const std::string& path) {
587        // Create the given path. Use string processing instead of dirname, as dirname's need for
588        // a writable char buffer is painful.
589
590        // First, try to use the full path.
591        if (mkdir(path.c_str(), 0711) == 0) {
592            return true;
593        }
594        if (errno != ENOENT) {
595            PLOG(ERROR) << "Could not create path " << path;
596            return false;
597        }
598
599        // Now find the parent and try that first.
600        size_t last_slash = path.find_last_of('/');
601        if (last_slash == std::string::npos || last_slash == 0) {
602            PLOG(ERROR) << "Could not create " << path;
603            return false;
604        }
605
606        if (!CreatePath(path.substr(0, last_slash))) {
607            return false;
608        }
609
610        if (mkdir(path.c_str(), 0711) == 0) {
611            return true;
612        }
613        PLOG(ERROR) << "Could not create " << path;
614        return false;
615    }
616
617    static void ClearDirectory(const std::string& dir) {
618        DIR* c_dir = opendir(dir.c_str());
619        if (c_dir == nullptr) {
620            PLOG(WARNING) << "Unable to open " << dir << " to delete it's contents";
621            return;
622        }
623
624        for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) {
625            const char* name = de->d_name;
626            if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
627                continue;
628            }
629            // We only want to delete regular files and symbolic links.
630            std::string file = StringPrintf("%s/%s", dir.c_str(), name);
631            if (de->d_type != DT_REG && de->d_type != DT_LNK) {
632                LOG(WARNING) << "Unexpected file "
633                             << file
634                             << " of type "
635                             << std::hex
636                             << de->d_type
637                             << " encountered.";
638            } else {
639                // Try to unlink the file.
640                if (unlink(file.c_str()) != 0) {
641                    PLOG(ERROR) << "Unable to unlink " << file;
642                }
643            }
644        }
645        CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory.";
646    }
647
648    bool PatchoatBootImage(const std::string& art_path, const char* isa) const {
649        // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc.
650
651        std::vector<std::string> cmd;
652        cmd.push_back("/system/bin/patchoat");
653
654        cmd.push_back("--input-image-location=/system/framework/boot.art");
655        cmd.push_back(StringPrintf("--output-image-file=%s", art_path.c_str()));
656
657        cmd.push_back(StringPrintf("--instruction-set=%s", isa));
658
659        int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
660                                                          ART_BASE_ADDRESS_MAX_DELTA);
661        cmd.push_back(StringPrintf("--base-offset-delta=%d", base_offset));
662
663        std::string error_msg;
664        bool result = Exec(cmd, &error_msg);
665        if (!result) {
666            LOG(ERROR) << "Could not generate boot image: " << error_msg;
667        }
668        return result;
669    }
670
671    bool Dex2oatBootImage(const std::string& boot_cp,
672                          const std::string& art_path,
673                          const std::string& oat_path,
674                          const char* isa) const {
675        // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc.
676        std::vector<std::string> cmd;
677        cmd.push_back("/system/bin/dex2oat");
678        cmd.push_back(StringPrintf("--image=%s", art_path.c_str()));
679        for (const std::string& boot_part : Split(boot_cp, ":")) {
680            cmd.push_back(StringPrintf("--dex-file=%s", boot_part.c_str()));
681        }
682        cmd.push_back(StringPrintf("--oat-file=%s", oat_path.c_str()));
683
684        int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
685                ART_BASE_ADDRESS_MAX_DELTA);
686        cmd.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
687
688        cmd.push_back(StringPrintf("--instruction-set=%s", isa));
689
690        // These things are pushed by AndroidRuntime, see frameworks/base/core/jni/AndroidRuntime.cpp.
691        AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xms",
692                "-Xms",
693                true,
694                cmd);
695        AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xmx",
696                "-Xmx",
697                true,
698                cmd);
699        AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-filter",
700                "--compiler-filter=",
701                false,
702                cmd);
703        cmd.push_back("--image-classes=/system/etc/preloaded-classes");
704        // TODO: Compiled-classes.
705        const std::string* extra_opts =
706                system_properties_.GetProperty("dalvik.vm.image-dex2oat-flags");
707        if (extra_opts != nullptr) {
708            std::vector<std::string> extra_vals = Split(*extra_opts, " ");
709            cmd.insert(cmd.end(), extra_vals.begin(), extra_vals.end());
710        }
711        // TODO: Should we lower this? It's usually set close to max, because
712        //       normally there's not much else going on at boot.
713        AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-threads",
714                "-j",
715                false,
716                cmd);
717        AddCompilerOptionFromSystemProperty(
718                StringPrintf("dalvik.vm.isa.%s.variant", isa).c_str(),
719                "--instruction-set-variant=",
720                false,
721                cmd);
722        AddCompilerOptionFromSystemProperty(
723                StringPrintf("dalvik.vm.isa.%s.features", isa).c_str(),
724                "--instruction-set-features=",
725                false,
726                cmd);
727
728        std::string error_msg;
729        bool result = Exec(cmd, &error_msg);
730        if (!result) {
731            LOG(ERROR) << "Could not generate boot image: " << error_msg;
732        }
733        return result;
734    }
735
736    static const char* ParseNull(const char* arg) {
737        // b/38186355. Revert soon.
738        if (strcmp(arg, "!null") == 0) {
739            return nullptr;
740        }
741        return (strcmp(arg, "!") == 0) ? nullptr : arg;
742    }
743
744    bool ShouldSkipPreopt() const {
745        // There's one thing we have to be careful about: we may/will be asked to compile an app
746        // living in the system image. This may be a valid request - if the app wasn't compiled,
747        // e.g., if the system image wasn't large enough to include preopted files. However, the
748        // data we have is from the old system, so the driver (the OTA service) can't actually
749        // know. Thus, we will get requests for apps that have preopted components. To avoid
750        // duplication (we'd generate files that are not used and are *not* cleaned up), do two
751        // simple checks:
752        //
753        // 1) Does the apk_path start with the value of ANDROID_ROOT? (~in the system image)
754        //    (For simplicity, assume the value of ANDROID_ROOT does not contain a symlink.)
755        //
756        // 2) If you replace the name in the apk_path with "oat," does the path exist?
757        //    (=have a subdirectory for preopted files)
758        //
759        // If the answer to both is yes, skip the dexopt.
760        //
761        // Note: while one may think it's OK to call dexopt and it will fail (because APKs should
762        //       be stripped), that's not true for APKs signed outside the build system (so the
763        //       jar content must be exactly the same).
764
765        //       (This is ugly as it's the only thing where we need to understand the contents
766        //        of package_parameters_, but it beats postponing the decision or using the call-
767        //        backs to do weird things.)
768        const char* apk_path = package_parameters_.apk_path;
769        CHECK(apk_path != nullptr);
770        if (StartsWith(apk_path, android_root_.c_str())) {
771            const char* last_slash = strrchr(apk_path, '/');
772            if (last_slash != nullptr) {
773                std::string path(apk_path, last_slash - apk_path + 1);
774                CHECK(EndsWith(path, "/"));
775                path = path + "oat";
776                if (access(path.c_str(), F_OK) == 0) {
777                    return true;
778                }
779            }
780        }
781
782        // Another issue is unavailability of files in the new system. If the partition
783        // layout changes, otapreopt_chroot may not know about this. Then files from that
784        // partition will not be available and fail to build. This is problematic, as
785        // this tool will wipe the OTA artifact cache and try again (for robustness after
786        // a failed OTA with remaining cache artifacts).
787        if (access(apk_path, F_OK) != 0) {
788            LOG(WARNING) << "Skipping preopt of non-existing package " << apk_path;
789            return true;
790        }
791
792        return false;
793    }
794
795    // Run dexopt with the parameters of package_parameters_.
796    int Dexopt() {
797        return dexopt(package_parameters_.apk_path,
798                      package_parameters_.uid,
799                      package_parameters_.pkgName,
800                      package_parameters_.instruction_set,
801                      package_parameters_.dexopt_needed,
802                      package_parameters_.oat_dir,
803                      package_parameters_.dexopt_flags,
804                      package_parameters_.compiler_filter,
805                      package_parameters_.volume_uuid,
806                      package_parameters_.shared_libraries,
807                      package_parameters_.se_info);
808    }
809
810    int RunPreopt() {
811        if (ShouldSkipPreopt()) {
812            return 0;
813        }
814
815        int dexopt_result = Dexopt();
816        if (dexopt_result == 0) {
817            return 0;
818        }
819
820        // If the dexopt failed, we may have a stale boot image from a previous OTA run.
821        // Then regenerate and retry.
822        if (WEXITSTATUS(dexopt_result) ==
823                static_cast<int>(art::dex2oat::ReturnCode::kCreateRuntime)) {
824            if (!PrepareBootImage(/* force */ true)) {
825                LOG(ERROR) << "Forced boot image creating failed. Original error return was "
826                        << dexopt_result;
827                return dexopt_result;
828            }
829
830            int dexopt_result_boot_image_retry = Dexopt();
831            if (dexopt_result_boot_image_retry == 0) {
832                return 0;
833            }
834        }
835
836        // If this was a profile-guided run, we may have profile version issues. Try to downgrade,
837        // if possible.
838        if ((package_parameters_.dexopt_flags & DEXOPT_PROFILE_GUIDED) == 0) {
839            return dexopt_result;
840        }
841
842        LOG(WARNING) << "Downgrading compiler filter in an attempt to progress compilation";
843        package_parameters_.dexopt_flags &= ~DEXOPT_PROFILE_GUIDED;
844        return Dexopt();
845    }
846
847    ////////////////////////////////////
848    // Helpers, mostly taken from ART //
849    ////////////////////////////////////
850
851    // Wrapper on fork/execv to run a command in a subprocess.
852    static bool Exec(const std::vector<std::string>& arg_vector, std::string* error_msg) {
853        const std::string command_line = Join(arg_vector, ' ');
854
855        CHECK_GE(arg_vector.size(), 1U) << command_line;
856
857        // Convert the args to char pointers.
858        const char* program = arg_vector[0].c_str();
859        std::vector<char*> args;
860        for (size_t i = 0; i < arg_vector.size(); ++i) {
861            const std::string& arg = arg_vector[i];
862            char* arg_str = const_cast<char*>(arg.c_str());
863            CHECK(arg_str != nullptr) << i;
864            args.push_back(arg_str);
865        }
866        args.push_back(nullptr);
867
868        // Fork and exec.
869        pid_t pid = fork();
870        if (pid == 0) {
871            // No allocation allowed between fork and exec.
872
873            // Change process groups, so we don't get reaped by ProcessManager.
874            setpgid(0, 0);
875
876            execv(program, &args[0]);
877
878            PLOG(ERROR) << "Failed to execv(" << command_line << ")";
879            // _exit to avoid atexit handlers in child.
880            _exit(1);
881        } else {
882            if (pid == -1) {
883                *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s",
884                        command_line.c_str(), strerror(errno));
885                return false;
886            }
887
888            // wait for subprocess to finish
889            int status;
890            pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
891            if (got_pid != pid) {
892                *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: "
893                        "wanted %d, got %d: %s",
894                        command_line.c_str(), pid, got_pid, strerror(errno));
895                return false;
896            }
897            if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
898                *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status",
899                        command_line.c_str());
900                return false;
901            }
902        }
903        return true;
904    }
905
906    // Choose a random relocation offset. Taken from art/runtime/gc/image_space.cc.
907    static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
908        constexpr size_t kPageSize = PAGE_SIZE;
909        CHECK_EQ(min_delta % kPageSize, 0u);
910        CHECK_EQ(max_delta % kPageSize, 0u);
911        CHECK_LT(min_delta, max_delta);
912
913        std::default_random_engine generator;
914        generator.seed(GetSeed());
915        std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
916        int32_t r = distribution(generator);
917        if (r % 2 == 0) {
918            r = RoundUp(r, kPageSize);
919        } else {
920            r = RoundDown(r, kPageSize);
921        }
922        CHECK_LE(min_delta, r);
923        CHECK_GE(max_delta, r);
924        CHECK_EQ(r % kPageSize, 0u);
925        return r;
926    }
927
928    static uint64_t GetSeed() {
929#ifdef __BIONIC__
930        // Bionic exposes arc4random, use it.
931        uint64_t random_data;
932        arc4random_buf(&random_data, sizeof(random_data));
933        return random_data;
934#else
935#error "This is only supposed to run with bionic. Otherwise, implement..."
936#endif
937    }
938
939    void AddCompilerOptionFromSystemProperty(const char* system_property,
940            const char* prefix,
941            bool runtime,
942            std::vector<std::string>& out) const {
943        const std::string* value = system_properties_.GetProperty(system_property);
944        if (value != nullptr) {
945            if (runtime) {
946                out.push_back("--runtime-arg");
947            }
948            if (prefix != nullptr) {
949                out.push_back(StringPrintf("%s%s", prefix, value->c_str()));
950            } else {
951                out.push_back(*value);
952            }
953        }
954    }
955
956    static constexpr const char* kBootClassPathPropertyName = "BOOTCLASSPATH";
957    static constexpr const char* kAndroidRootPathPropertyName = "ANDROID_ROOT";
958    static constexpr const char* kAndroidDataPathPropertyName = "ANDROID_DATA";
959    // The index of the instruction-set string inside the package parameters. Needed for
960    // some special-casing that requires knowledge of the instruction-set.
961    static constexpr size_t kISAIndex = 3;
962
963    // Stores the system properties read out of the B partition. We need to use these properties
964    // to compile, instead of the A properties we could get from init/get_property.
965    SystemProperties system_properties_;
966
967    // Some select properties that are always needed.
968    std::string target_slot_;
969    std::string android_root_;
970    std::string android_data_;
971    std::string boot_classpath_;
972    std::string asec_mountpoint_;
973
974    Parameters package_parameters_;
975
976    // Store environment values we need to set.
977    std::vector<std::string> environ_;
978};
979
980OTAPreoptService gOps;
981
982////////////////////////
983// Plug-in functions. //
984////////////////////////
985
986int get_property(const char *key, char *value, const char *default_value) {
987    return gOps.GetProperty(key, value, default_value);
988}
989
990// Compute the output path of
991bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir,
992                             const char *apk_path,
993                             const char *instruction_set) {
994    const char *file_name_start;
995    const char *file_name_end;
996
997    file_name_start = strrchr(apk_path, '/');
998    if (file_name_start == nullptr) {
999        ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
1000        return false;
1001    }
1002    file_name_end = strrchr(file_name_start, '.');
1003    if (file_name_end == nullptr) {
1004        ALOGE("apk_path '%s' has no extension\n", apk_path);
1005        return false;
1006    }
1007
1008    // Calculate file_name
1009    file_name_start++;  // Move past '/', is valid as file_name_end is valid.
1010    size_t file_name_len = file_name_end - file_name_start;
1011    std::string file_name(file_name_start, file_name_len);
1012
1013    // <apk_parent_dir>/oat/<isa>/<file_name>.odex.b
1014    snprintf(path,
1015             PKG_PATH_MAX,
1016             "%s/%s/%s.odex.%s",
1017             oat_dir,
1018             instruction_set,
1019             file_name.c_str(),
1020             gOps.GetTargetSlot().c_str());
1021    return true;
1022}
1023
1024/*
1025 * Computes the odex file for the given apk_path and instruction_set.
1026 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
1027 *
1028 * Returns false if it failed to determine the odex file path.
1029 */
1030bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path,
1031                              const char *instruction_set) {
1032    const char *path_end = strrchr(apk_path, '/');
1033    if (path_end == nullptr) {
1034        ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
1035        return false;
1036    }
1037    std::string path_component(apk_path, path_end - apk_path);
1038
1039    const char *name_begin = path_end + 1;
1040    const char *extension_start = strrchr(name_begin, '.');
1041    if (extension_start == nullptr) {
1042        ALOGE("apk_path '%s' has no extension.\n", apk_path);
1043        return false;
1044    }
1045    std::string name_component(name_begin, extension_start - name_begin);
1046
1047    std::string new_path = StringPrintf("%s/oat/%s/%s.odex.%s",
1048                                        path_component.c_str(),
1049                                        instruction_set,
1050                                        name_component.c_str(),
1051                                        gOps.GetTargetSlot().c_str());
1052    if (new_path.length() >= PKG_PATH_MAX) {
1053        LOG(ERROR) << "apk_path of " << apk_path << " is too long: " << new_path;
1054        return false;
1055    }
1056    strcpy(path, new_path.c_str());
1057    return true;
1058}
1059
1060bool create_cache_path(char path[PKG_PATH_MAX],
1061                       const char *src,
1062                       const char *instruction_set) {
1063    size_t srclen = strlen(src);
1064
1065        /* demand that we are an absolute path */
1066    if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
1067        return false;
1068    }
1069
1070    if (srclen > PKG_PATH_MAX) {        // XXX: PKG_NAME_MAX?
1071        return false;
1072    }
1073
1074    std::string from_src = std::string(src + 1);
1075    std::replace(from_src.begin(), from_src.end(), '/', '@');
1076
1077    std::string assembled_path = StringPrintf("%s/%s/%s/%s%s",
1078                                              gOps.GetOTADataDirectory().c_str(),
1079                                              DALVIK_CACHE,
1080                                              instruction_set,
1081                                              from_src.c_str(),
1082                                              DALVIK_CACHE_POSTFIX);
1083
1084    if (assembled_path.length() + 1 > PKG_PATH_MAX) {
1085        return false;
1086    }
1087    strcpy(path, assembled_path.c_str());
1088
1089    return true;
1090}
1091
1092static int log_callback(int type, const char *fmt, ...) {
1093    va_list ap;
1094    int priority;
1095
1096    switch (type) {
1097        case SELINUX_WARNING:
1098            priority = ANDROID_LOG_WARN;
1099            break;
1100        case SELINUX_INFO:
1101            priority = ANDROID_LOG_INFO;
1102            break;
1103        default:
1104            priority = ANDROID_LOG_ERROR;
1105            break;
1106    }
1107    va_start(ap, fmt);
1108    LOG_PRI_VA(priority, "SELinux", fmt, ap);
1109    va_end(ap);
1110    return 0;
1111}
1112
1113static int otapreopt_main(const int argc, char *argv[]) {
1114    int selinux_enabled = (is_selinux_enabled() > 0);
1115
1116    setenv("ANDROID_LOG_TAGS", "*:v", 1);
1117    android::base::InitLogging(argv);
1118
1119    if (argc < 2) {
1120        ALOGE("Expecting parameters");
1121        exit(1);
1122    }
1123
1124    union selinux_callback cb;
1125    cb.func_log = log_callback;
1126    selinux_set_callback(SELINUX_CB_LOG, cb);
1127
1128    if (selinux_enabled && selinux_status_open(true) < 0) {
1129        ALOGE("Could not open selinux status; exiting.\n");
1130        exit(1);
1131    }
1132
1133    int ret = android::installd::gOps.Main(argc, argv);
1134
1135    return ret;
1136}
1137
1138}  // namespace installd
1139}  // namespace android
1140
1141int main(const int argc, char *argv[]) {
1142    return android::installd::otapreopt_main(argc, argv);
1143}
1144