otapreopt.cpp revision 1842af3a4a8fccf71f5c569071518de14b3698ac
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 <random>
20#include <regex>
21#include <selinux/android.h>
22#include <selinux/avc.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/capability.h>
26#include <sys/prctl.h>
27#include <sys/stat.h>
28#include <sys/wait.h>
29
30#include <android-base/logging.h>
31#include <android-base/macros.h>
32#include <android-base/stringprintf.h>
33#include <cutils/fs.h>
34#include <cutils/log.h>
35#include <cutils/properties.h>
36#include <private/android_filesystem_config.h>
37
38#include <commands.h>
39#include <file_parsing.h>
40#include <globals.h>
41#include <installd_deps.h>  // Need to fill in requirements of commands.
42#include <string_helpers.h>
43#include <system_properties.h>
44#include <utils.h>
45
46#ifndef LOG_TAG
47#define LOG_TAG "otapreopt"
48#endif
49
50#define BUFFER_MAX    1024  /* input buffer for commands */
51#define TOKEN_MAX     16    /* max number of arguments in buffer */
52#define REPLY_MAX     256   /* largest reply allowed */
53
54using android::base::StringPrintf;
55
56namespace android {
57namespace installd {
58
59static constexpr const char* kBootClassPathPropertyName = "BOOTCLASSPATH";
60static constexpr const char* kAndroidRootPathPropertyName = "ANDROID_ROOT";
61static constexpr const char* kOTARootDirectory = "/system-b";
62static constexpr size_t kISAIndex = 3;
63
64template<typename T>
65static constexpr T RoundDown(T x, typename std::decay<T>::type n) {
66    return DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))(x & -n);
67}
68
69template<typename T>
70static constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) {
71    return RoundDown(x + n - 1, n);
72}
73
74class OTAPreoptService {
75 public:
76    static constexpr const char* kOTADataDirectory = "/data/ota";
77
78    // Main driver. Performs the following steps.
79    //
80    // 1) Parse options (read system properties etc from B partition).
81    //
82    // 2) Read in package data.
83    //
84    // 3) Prepare environment variables.
85    //
86    // 4) Prepare(compile) boot image, if necessary.
87    //
88    // 5) Run update.
89    int Main(int argc, char** argv) {
90        if (!ReadSystemProperties()) {
91            LOG(ERROR)<< "Failed reading system properties.";
92            return 1;
93        }
94
95        if (!ReadEnvironment()) {
96            LOG(ERROR) << "Failed reading environment properties.";
97            return 2;
98        }
99
100        if (!ReadPackage(argc, argv)) {
101            LOG(ERROR) << "Failed reading command line file.";
102            return 3;
103        }
104
105        PrepareEnvironment();
106
107        if (!PrepareBootImage()) {
108            LOG(ERROR) << "Failed preparing boot image.";
109            return 4;
110        }
111
112        int dexopt_retcode = RunPreopt();
113
114        return dexopt_retcode;
115    }
116
117    int GetProperty(const char* key, char* value, const char* default_value) {
118        const std::string* prop_value = system_properties_.GetProperty(key);
119        if (prop_value == nullptr) {
120            if (default_value == nullptr) {
121                return 0;
122            }
123            // Copy in the default value.
124            strncpy(value, default_value, kPropertyValueMax - 1);
125            value[kPropertyValueMax - 1] = 0;
126            return strlen(default_value);// TODO: Need to truncate?
127        }
128        size_t size = std::min(kPropertyValueMax - 1, prop_value->length());
129        strncpy(value, prop_value->data(), size);
130        value[size] = 0;
131        return static_cast<int>(size);
132    }
133
134private:
135    bool ReadSystemProperties() {
136        static constexpr const char* kPropertyFiles[] = {
137                "/default.prop", "/system/build.prop"
138        };
139
140        for (size_t i = 0; i < arraysize(kPropertyFiles); ++i) {
141            if (!system_properties_.Load(kPropertyFiles[i])) {
142                return false;
143            }
144        }
145
146        return true;
147    }
148
149    bool ReadEnvironment() {
150        // Parse the environment variables from init.environ.rc, which have the form
151        //   export NAME VALUE
152        // For simplicity, don't respect string quotation. The values we are interested in can be
153        // encoded without them.
154        std::regex export_regex("\\s*export\\s+(\\S+)\\s+(\\S+)");
155        bool parse_result = ParseFile("/init.environ.rc", [&](const std::string& line) {
156            std::smatch export_match;
157            if (!std::regex_match(line, export_match, export_regex)) {
158                return true;
159            }
160
161            if (export_match.size() != 3) {
162                return true;
163            }
164
165            std::string name = export_match[1].str();
166            std::string value = export_match[2].str();
167
168            system_properties_.SetProperty(name, value);
169
170            return true;
171        });
172        if (!parse_result) {
173            return false;
174        }
175
176        // Check that we found important properties.
177        constexpr const char* kRequiredProperties[] = {
178                kBootClassPathPropertyName, kAndroidRootPathPropertyName
179        };
180        for (size_t i = 0; i < arraysize(kRequiredProperties); ++i) {
181            if (system_properties_.GetProperty(kRequiredProperties[i]) == nullptr) {
182                return false;
183            }
184        }
185
186        return true;
187    }
188
189    bool ReadPackage(int argc ATTRIBUTE_UNUSED, char** argv) {
190        size_t index = 0;
191        while (index < ARRAY_SIZE(package_parameters_) &&
192                argv[index + 1] != nullptr) {
193            package_parameters_[index] = argv[index + 1];
194            index++;
195        }
196        if (index != ARRAY_SIZE(package_parameters_)) {
197            LOG(ERROR) << "Wrong number of parameters";
198            return false;
199        }
200
201        return true;
202    }
203
204    void PrepareEnvironment() {
205        CHECK(system_properties_.GetProperty(kBootClassPathPropertyName) != nullptr);
206        const std::string& boot_cp =
207                *system_properties_.GetProperty(kBootClassPathPropertyName);
208        environ_.push_back(StringPrintf("BOOTCLASSPATH=%s", boot_cp.c_str()));
209        environ_.push_back(StringPrintf("ANDROID_DATA=%s", kOTADataDirectory));
210        CHECK(system_properties_.GetProperty(kAndroidRootPathPropertyName) != nullptr);
211        const std::string& android_root =
212                *system_properties_.GetProperty(kAndroidRootPathPropertyName);
213        environ_.push_back(StringPrintf("ANDROID_ROOT=%s", android_root.c_str()));
214
215        for (const std::string& e : environ_) {
216            putenv(const_cast<char*>(e.c_str()));
217        }
218    }
219
220    // Ensure that we have the right boot image. The first time any app is
221    // compiled, we'll try to generate it.
222    bool PrepareBootImage() {
223        if (package_parameters_[kISAIndex] == nullptr) {
224            LOG(ERROR) << "Instruction set missing.";
225            return false;
226        }
227        const char* isa = package_parameters_[kISAIndex];
228
229        // Check whether the file exists where expected.
230        std::string dalvik_cache = std::string(kOTADataDirectory) + "/" + DALVIK_CACHE;
231        std::string isa_path = dalvik_cache + "/" + isa;
232        std::string art_path = isa_path + "/system@framework@boot.art";
233        std::string oat_path = isa_path + "/system@framework@boot.oat";
234        if (access(art_path.c_str(), F_OK) == 0 &&
235                access(oat_path.c_str(), F_OK) == 0) {
236            // Files exist, assume everything is alright.
237            return true;
238        }
239
240        // Create the directories, if necessary.
241        if (access(dalvik_cache.c_str(), F_OK) != 0) {
242            if (mkdir(dalvik_cache.c_str(), 0711) != 0) {
243                PLOG(ERROR) << "Could not create dalvik-cache dir";
244                return false;
245            }
246        }
247        if (access(isa_path.c_str(), F_OK) != 0) {
248            if (mkdir(isa_path.c_str(), 0711) != 0) {
249                PLOG(ERROR) << "Could not create dalvik-cache isa dir";
250                return false;
251            }
252        }
253
254        // Prepare to create.
255        // TODO: Delete files, just for a blank slate.
256        const std::string& boot_cp = *system_properties_.GetProperty(kBootClassPathPropertyName);
257
258        std::string preopted_boot_art_path = StringPrintf("/system/framework/%s/boot.art", isa);
259        if (access(preopted_boot_art_path.c_str(), F_OK) == 0) {
260          return PatchoatBootImage(art_path, isa);
261        } else {
262          // No preopted boot image. Try to compile.
263          return Dex2oatBootImage(boot_cp, art_path, oat_path, isa);
264        }
265    }
266
267    bool PatchoatBootImage(const std::string& art_path, const char* isa) {
268        // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc.
269
270        std::vector<std::string> cmd;
271        cmd.push_back("/system/bin/patchoat");
272
273        cmd.push_back("--input-image-location=/system/framework/boot.art");
274        cmd.push_back(StringPrintf("--output-image-file=%s", art_path.c_str()));
275
276        cmd.push_back(StringPrintf("--instruction-set=%s", isa));
277
278        int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
279                                                          ART_BASE_ADDRESS_MAX_DELTA);
280        cmd.push_back(StringPrintf("--base-offset-delta=%d", base_offset));
281
282        std::string error_msg;
283        bool result = Exec(cmd, &error_msg);
284        if (!result) {
285            LOG(ERROR) << "Could not generate boot image: " << error_msg;
286        }
287        return result;
288    }
289
290    bool Dex2oatBootImage(const std::string& boot_cp,
291                          const std::string& art_path,
292                          const std::string& oat_path,
293                          const char* isa) {
294        // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc.
295        std::vector<std::string> cmd;
296        cmd.push_back("/system/bin/dex2oat");
297        cmd.push_back(StringPrintf("--image=%s", art_path.c_str()));
298        for (const std::string& boot_part : Split(boot_cp, ':')) {
299            cmd.push_back(StringPrintf("--dex-file=%s", boot_part.c_str()));
300        }
301        cmd.push_back(StringPrintf("--oat-file=%s", oat_path.c_str()));
302
303        int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
304                ART_BASE_ADDRESS_MAX_DELTA);
305        cmd.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
306
307        cmd.push_back(StringPrintf("--instruction-set=%s", isa));
308
309        // These things are pushed by AndroidRuntime, see frameworks/base/core/jni/AndroidRuntime.cpp.
310        AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xms",
311                "-Xms",
312                true,
313                cmd);
314        AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xmx",
315                "-Xmx",
316                true,
317                cmd);
318        AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-filter",
319                "--compiler-filter=",
320                false,
321                cmd);
322        cmd.push_back("--image-classes=/system/etc/preloaded-classes");
323        // TODO: Compiled-classes.
324        const std::string* extra_opts =
325                system_properties_.GetProperty("dalvik.vm.image-dex2oat-flags");
326        if (extra_opts != nullptr) {
327            std::vector<std::string> extra_vals = Split(*extra_opts, ' ');
328            cmd.insert(cmd.end(), extra_vals.begin(), extra_vals.end());
329        }
330        // TODO: Should we lower this? It's usually set close to max, because
331        //       normally there's not much else going on at boot.
332        AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-threads",
333                "-j",
334                false,
335                cmd);
336        AddCompilerOptionFromSystemProperty(
337                StringPrintf("dalvik.vm.isa.%s.variant", isa).c_str(),
338                "--instruction-set-variant=",
339                false,
340                cmd);
341        AddCompilerOptionFromSystemProperty(
342                StringPrintf("dalvik.vm.isa.%s.features", isa).c_str(),
343                "--instruction-set-features=",
344                false,
345                cmd);
346
347        std::string error_msg;
348        bool result = Exec(cmd, &error_msg);
349        if (!result) {
350            LOG(ERROR) << "Could not generate boot image: " << error_msg;
351        }
352        return result;
353    }
354
355    static const char* ParseNull(const char* arg) {
356        return (strcmp(arg, "!") == 0) ? nullptr : arg;
357    }
358
359    int RunPreopt() {
360        /* apk_path, uid, pkgname, instruction_set, dexopt_needed, oat_dir, dexopt_flags,
361           volume_uuid, use_profiles */
362        int ret = dexopt(package_parameters_[0],
363                atoi(package_parameters_[1]),
364                package_parameters_[2],
365                package_parameters_[3],
366                atoi(package_parameters_[4]),
367                package_parameters_[5],
368                atoi(package_parameters_[6]),
369                ParseNull(package_parameters_[7]),
370                (atoi(package_parameters_[8]) == 0 ? false : true));
371        return ret;
372    }
373
374    ////////////////////////////////////
375    // Helpers, mostly taken from ART //
376    ////////////////////////////////////
377
378    // Wrapper on fork/execv to run a command in a subprocess.
379    bool Exec(const std::vector<std::string>& arg_vector, std::string* error_msg) {
380        const std::string command_line(Join(arg_vector, ' '));
381
382        CHECK_GE(arg_vector.size(), 1U) << command_line;
383
384        // Convert the args to char pointers.
385        const char* program = arg_vector[0].c_str();
386        std::vector<char*> args;
387        for (size_t i = 0; i < arg_vector.size(); ++i) {
388            const std::string& arg = arg_vector[i];
389            char* arg_str = const_cast<char*>(arg.c_str());
390            CHECK(arg_str != nullptr) << i;
391            args.push_back(arg_str);
392        }
393        args.push_back(nullptr);
394
395        // Fork and exec.
396        pid_t pid = fork();
397        if (pid == 0) {
398            // No allocation allowed between fork and exec.
399
400            // Change process groups, so we don't get reaped by ProcessManager.
401            setpgid(0, 0);
402
403            execv(program, &args[0]);
404
405            PLOG(ERROR) << "Failed to execv(" << command_line << ")";
406            // _exit to avoid atexit handlers in child.
407            _exit(1);
408        } else {
409            if (pid == -1) {
410                *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s",
411                        command_line.c_str(), strerror(errno));
412                return false;
413            }
414
415            // wait for subprocess to finish
416            int status;
417            pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
418            if (got_pid != pid) {
419                *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: "
420                        "wanted %d, got %d: %s",
421                        command_line.c_str(), pid, got_pid, strerror(errno));
422                return false;
423            }
424            if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
425                *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status",
426                        command_line.c_str());
427                return false;
428            }
429        }
430        return true;
431    }
432
433    // Choose a random relocation offset. Taken from art/runtime/gc/image_space.cc.
434    static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
435        constexpr size_t kPageSize = PAGE_SIZE;
436        CHECK_EQ(min_delta % kPageSize, 0u);
437        CHECK_EQ(max_delta % kPageSize, 0u);
438        CHECK_LT(min_delta, max_delta);
439
440        std::default_random_engine generator;
441        generator.seed(GetSeed());
442        std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
443        int32_t r = distribution(generator);
444        if (r % 2 == 0) {
445            r = RoundUp(r, kPageSize);
446        } else {
447            r = RoundDown(r, kPageSize);
448        }
449        CHECK_LE(min_delta, r);
450        CHECK_GE(max_delta, r);
451        CHECK_EQ(r % kPageSize, 0u);
452        return r;
453    }
454
455    static uint64_t GetSeed() {
456#ifdef __BIONIC__
457        // Bionic exposes arc4random, use it.
458        uint64_t random_data;
459        arc4random_buf(&random_data, sizeof(random_data));
460        return random_data;
461#else
462#error "This is only supposed to run with bionic. Otherwise, implement..."
463#endif
464    }
465
466    void AddCompilerOptionFromSystemProperty(const char* system_property,
467            const char* prefix,
468            bool runtime,
469            std::vector<std::string>& out) {
470        const std::string* value =
471        system_properties_.GetProperty(system_property);
472        if (value != nullptr) {
473            if (runtime) {
474                out.push_back("--runtime-arg");
475            }
476            if (prefix != nullptr) {
477                out.push_back(StringPrintf("%s%s", prefix, value->c_str()));
478            } else {
479                out.push_back(*value);
480            }
481        }
482    }
483
484    // Stores the system properties read out of the B partition. We need to use these properties
485    // to compile, instead of the A properties we could get from init/get_property.
486    SystemProperties system_properties_;
487
488    const char* package_parameters_[9];
489
490    // Store environment values we need to set.
491    std::vector<std::string> environ_;
492};
493
494OTAPreoptService gOps;
495
496////////////////////////
497// Plug-in functions. //
498////////////////////////
499
500int get_property(const char *key, char *value, const char *default_value) {
501    // TODO: Replace with system-properties map.
502    return gOps.GetProperty(key, value, default_value);
503}
504
505// Compute the output path of
506bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir,
507                             const char *apk_path,
508                             const char *instruction_set) {
509    // TODO: Insert B directory.
510    char *file_name_start;
511    char *file_name_end;
512
513    file_name_start = strrchr(apk_path, '/');
514    if (file_name_start == nullptr) {
515        ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
516        return false;
517    }
518    file_name_end = strrchr(file_name_start, '.');
519    if (file_name_end == nullptr) {
520        ALOGE("apk_path '%s' has no extension\n", apk_path);
521        return false;
522    }
523
524    // Calculate file_name
525    file_name_start++;  // Move past '/', is valid as file_name_end is valid.
526    size_t file_name_len = file_name_end - file_name_start;
527    std::string file_name(file_name_start, file_name_len);
528
529    // <apk_parent_dir>/oat/<isa>/<file_name>.odex.b
530    snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex.b", oat_dir, instruction_set,
531             file_name.c_str());
532    return true;
533}
534
535/*
536 * Computes the odex file for the given apk_path and instruction_set.
537 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
538 *
539 * Returns false if it failed to determine the odex file path.
540 */
541bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path,
542                              const char *instruction_set) {
543    if (StringPrintf("%soat/%s/odex.b", apk_path, instruction_set).length() + 1 > PKG_PATH_MAX) {
544        ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
545        return false;
546    }
547
548    const char *path_end = strrchr(apk_path, '/');
549    if (path_end == nullptr) {
550        ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
551        return false;
552    }
553    std::string path_component(apk_path, path_end - apk_path);
554
555    const char *name_begin = path_end + 1;
556    const char *extension_start = strrchr(name_begin, '.');
557    if (extension_start == nullptr) {
558        ALOGE("apk_path '%s' has no extension.\n", apk_path);
559        return false;
560    }
561    std::string name_component(name_begin, extension_start - name_begin);
562
563    std::string new_path = StringPrintf("%s/oat/%s/%s.odex.b",
564                                        path_component.c_str(),
565                                        instruction_set,
566                                        name_component.c_str());
567    CHECK_LT(new_path.length(), PKG_PATH_MAX);
568    strcpy(path, new_path.c_str());
569    return true;
570}
571
572bool create_cache_path(char path[PKG_PATH_MAX],
573                       const char *src,
574                       const char *instruction_set) {
575    size_t srclen = strlen(src);
576
577        /* demand that we are an absolute path */
578    if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
579        return false;
580    }
581
582    if (srclen > PKG_PATH_MAX) {        // XXX: PKG_NAME_MAX?
583        return false;
584    }
585
586    std::string from_src = std::string(src + 1);
587    std::replace(from_src.begin(), from_src.end(), '/', '@');
588
589    std::string assembled_path = StringPrintf("%s/%s/%s/%s%s",
590                                              OTAPreoptService::kOTADataDirectory,
591                                              DALVIK_CACHE,
592                                              instruction_set,
593                                              from_src.c_str(),
594                                              DALVIK_CACHE_POSTFIX2);
595
596    if (assembled_path.length() + 1 > PKG_PATH_MAX) {
597        return false;
598    }
599    strcpy(path, assembled_path.c_str());
600
601    return true;
602}
603
604bool initialize_globals() {
605    const char* data_path = getenv("ANDROID_DATA");
606    if (data_path == nullptr) {
607        ALOGE("Could not find ANDROID_DATA");
608        return false;
609    }
610    return init_globals_from_data_and_root(data_path, kOTARootDirectory);
611}
612
613static bool initialize_directories() {
614    // This is different from the normal installd. We only do the base
615    // directory, the rest will be created on demand when each app is compiled.
616    mode_t old_umask = umask(0);
617    LOG(INFO) << "Old umask: " << old_umask;
618    if (access(OTAPreoptService::kOTADataDirectory, R_OK) < 0) {
619        ALOGE("Could not access %s\n", OTAPreoptService::kOTADataDirectory);
620        return false;
621    }
622    return true;
623}
624
625static int log_callback(int type, const char *fmt, ...) {
626    va_list ap;
627    int priority;
628
629    switch (type) {
630        case SELINUX_WARNING:
631            priority = ANDROID_LOG_WARN;
632            break;
633        case SELINUX_INFO:
634            priority = ANDROID_LOG_INFO;
635            break;
636        default:
637            priority = ANDROID_LOG_ERROR;
638            break;
639    }
640    va_start(ap, fmt);
641    LOG_PRI_VA(priority, "SELinux", fmt, ap);
642    va_end(ap);
643    return 0;
644}
645
646static int otapreopt_main(const int argc, char *argv[]) {
647    int selinux_enabled = (is_selinux_enabled() > 0);
648
649    setenv("ANDROID_LOG_TAGS", "*:v", 1);
650    android::base::InitLogging(argv);
651
652    ALOGI("otapreopt firing up\n");
653
654    if (argc < 2) {
655        ALOGE("Expecting parameters");
656        exit(1);
657    }
658
659    union selinux_callback cb;
660    cb.func_log = log_callback;
661    selinux_set_callback(SELINUX_CB_LOG, cb);
662
663    if (!initialize_globals()) {
664        ALOGE("Could not initialize globals; exiting.\n");
665        exit(1);
666    }
667
668    if (!initialize_directories()) {
669        ALOGE("Could not create directories; exiting.\n");
670        exit(1);
671    }
672
673    if (selinux_enabled && selinux_status_open(true) < 0) {
674        ALOGE("Could not open selinux status; exiting.\n");
675        exit(1);
676    }
677
678    int ret = android::installd::gOps.Main(argc, argv);
679
680    return ret;
681}
682
683}  // namespace installd
684}  // namespace android
685
686int main(const int argc, char *argv[]) {
687    return android::installd::otapreopt_main(argc, argv);
688}
689