otapreopt.cpp revision 4d0f825dd76a1972a3d081e771cde28513a1c6ff
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        int ret = dexopt(package_parameters_[0],          // apk_path
361                atoi(package_parameters_[1]),             // uid
362                package_parameters_[2],                   // pkgname
363                package_parameters_[3],                   // instruction_set
364                atoi(package_parameters_[4]),             // dexopt_needed
365                package_parameters_[5],                   // oat_dir
366                atoi(package_parameters_[6]),             // dexopt_flags
367                package_parameters_[7],                   // compiler_filter
368                ParseNull(package_parameters_[8]));       // volume_uuid
369        return ret;
370    }
371
372    ////////////////////////////////////
373    // Helpers, mostly taken from ART //
374    ////////////////////////////////////
375
376    // Wrapper on fork/execv to run a command in a subprocess.
377    bool Exec(const std::vector<std::string>& arg_vector, std::string* error_msg) {
378        const std::string command_line(Join(arg_vector, ' '));
379
380        CHECK_GE(arg_vector.size(), 1U) << command_line;
381
382        // Convert the args to char pointers.
383        const char* program = arg_vector[0].c_str();
384        std::vector<char*> args;
385        for (size_t i = 0; i < arg_vector.size(); ++i) {
386            const std::string& arg = arg_vector[i];
387            char* arg_str = const_cast<char*>(arg.c_str());
388            CHECK(arg_str != nullptr) << i;
389            args.push_back(arg_str);
390        }
391        args.push_back(nullptr);
392
393        // Fork and exec.
394        pid_t pid = fork();
395        if (pid == 0) {
396            // No allocation allowed between fork and exec.
397
398            // Change process groups, so we don't get reaped by ProcessManager.
399            setpgid(0, 0);
400
401            execv(program, &args[0]);
402
403            PLOG(ERROR) << "Failed to execv(" << command_line << ")";
404            // _exit to avoid atexit handlers in child.
405            _exit(1);
406        } else {
407            if (pid == -1) {
408                *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s",
409                        command_line.c_str(), strerror(errno));
410                return false;
411            }
412
413            // wait for subprocess to finish
414            int status;
415            pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
416            if (got_pid != pid) {
417                *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: "
418                        "wanted %d, got %d: %s",
419                        command_line.c_str(), pid, got_pid, strerror(errno));
420                return false;
421            }
422            if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
423                *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status",
424                        command_line.c_str());
425                return false;
426            }
427        }
428        return true;
429    }
430
431    // Choose a random relocation offset. Taken from art/runtime/gc/image_space.cc.
432    static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
433        constexpr size_t kPageSize = PAGE_SIZE;
434        CHECK_EQ(min_delta % kPageSize, 0u);
435        CHECK_EQ(max_delta % kPageSize, 0u);
436        CHECK_LT(min_delta, max_delta);
437
438        std::default_random_engine generator;
439        generator.seed(GetSeed());
440        std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
441        int32_t r = distribution(generator);
442        if (r % 2 == 0) {
443            r = RoundUp(r, kPageSize);
444        } else {
445            r = RoundDown(r, kPageSize);
446        }
447        CHECK_LE(min_delta, r);
448        CHECK_GE(max_delta, r);
449        CHECK_EQ(r % kPageSize, 0u);
450        return r;
451    }
452
453    static uint64_t GetSeed() {
454#ifdef __BIONIC__
455        // Bionic exposes arc4random, use it.
456        uint64_t random_data;
457        arc4random_buf(&random_data, sizeof(random_data));
458        return random_data;
459#else
460#error "This is only supposed to run with bionic. Otherwise, implement..."
461#endif
462    }
463
464    void AddCompilerOptionFromSystemProperty(const char* system_property,
465            const char* prefix,
466            bool runtime,
467            std::vector<std::string>& out) {
468        const std::string* value =
469        system_properties_.GetProperty(system_property);
470        if (value != nullptr) {
471            if (runtime) {
472                out.push_back("--runtime-arg");
473            }
474            if (prefix != nullptr) {
475                out.push_back(StringPrintf("%s%s", prefix, value->c_str()));
476            } else {
477                out.push_back(*value);
478            }
479        }
480    }
481
482    // Stores the system properties read out of the B partition. We need to use these properties
483    // to compile, instead of the A properties we could get from init/get_property.
484    SystemProperties system_properties_;
485
486    const char* package_parameters_[9];
487
488    // Store environment values we need to set.
489    std::vector<std::string> environ_;
490};
491
492OTAPreoptService gOps;
493
494////////////////////////
495// Plug-in functions. //
496////////////////////////
497
498int get_property(const char *key, char *value, const char *default_value) {
499    // TODO: Replace with system-properties map.
500    return gOps.GetProperty(key, value, default_value);
501}
502
503// Compute the output path of
504bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir,
505                             const char *apk_path,
506                             const char *instruction_set) {
507    // TODO: Insert B directory.
508    char *file_name_start;
509    char *file_name_end;
510
511    file_name_start = strrchr(apk_path, '/');
512    if (file_name_start == nullptr) {
513        ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
514        return false;
515    }
516    file_name_end = strrchr(file_name_start, '.');
517    if (file_name_end == nullptr) {
518        ALOGE("apk_path '%s' has no extension\n", apk_path);
519        return false;
520    }
521
522    // Calculate file_name
523    file_name_start++;  // Move past '/', is valid as file_name_end is valid.
524    size_t file_name_len = file_name_end - file_name_start;
525    std::string file_name(file_name_start, file_name_len);
526
527    // <apk_parent_dir>/oat/<isa>/<file_name>.odex.b
528    snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex.b", oat_dir, instruction_set,
529             file_name.c_str());
530    return true;
531}
532
533/*
534 * Computes the odex file for the given apk_path and instruction_set.
535 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
536 *
537 * Returns false if it failed to determine the odex file path.
538 */
539bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path,
540                              const char *instruction_set) {
541    if (StringPrintf("%soat/%s/odex.b", apk_path, instruction_set).length() + 1 > PKG_PATH_MAX) {
542        ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
543        return false;
544    }
545
546    const char *path_end = strrchr(apk_path, '/');
547    if (path_end == nullptr) {
548        ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
549        return false;
550    }
551    std::string path_component(apk_path, path_end - apk_path);
552
553    const char *name_begin = path_end + 1;
554    const char *extension_start = strrchr(name_begin, '.');
555    if (extension_start == nullptr) {
556        ALOGE("apk_path '%s' has no extension.\n", apk_path);
557        return false;
558    }
559    std::string name_component(name_begin, extension_start - name_begin);
560
561    std::string new_path = StringPrintf("%s/oat/%s/%s.odex.b",
562                                        path_component.c_str(),
563                                        instruction_set,
564                                        name_component.c_str());
565    CHECK_LT(new_path.length(), PKG_PATH_MAX);
566    strcpy(path, new_path.c_str());
567    return true;
568}
569
570bool create_cache_path(char path[PKG_PATH_MAX],
571                       const char *src,
572                       const char *instruction_set) {
573    size_t srclen = strlen(src);
574
575        /* demand that we are an absolute path */
576    if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
577        return false;
578    }
579
580    if (srclen > PKG_PATH_MAX) {        // XXX: PKG_NAME_MAX?
581        return false;
582    }
583
584    std::string from_src = std::string(src + 1);
585    std::replace(from_src.begin(), from_src.end(), '/', '@');
586
587    std::string assembled_path = StringPrintf("%s/%s/%s/%s%s",
588                                              OTAPreoptService::kOTADataDirectory,
589                                              DALVIK_CACHE,
590                                              instruction_set,
591                                              from_src.c_str(),
592                                              DALVIK_CACHE_POSTFIX2);
593
594    if (assembled_path.length() + 1 > PKG_PATH_MAX) {
595        return false;
596    }
597    strcpy(path, assembled_path.c_str());
598
599    return true;
600}
601
602bool initialize_globals() {
603    const char* data_path = getenv("ANDROID_DATA");
604    if (data_path == nullptr) {
605        ALOGE("Could not find ANDROID_DATA");
606        return false;
607    }
608    return init_globals_from_data_and_root(data_path, kOTARootDirectory);
609}
610
611static bool initialize_directories() {
612    // This is different from the normal installd. We only do the base
613    // directory, the rest will be created on demand when each app is compiled.
614    mode_t old_umask = umask(0);
615    LOG(INFO) << "Old umask: " << old_umask;
616    if (access(OTAPreoptService::kOTADataDirectory, R_OK) < 0) {
617        ALOGE("Could not access %s\n", OTAPreoptService::kOTADataDirectory);
618        return false;
619    }
620    return true;
621}
622
623static int log_callback(int type, const char *fmt, ...) {
624    va_list ap;
625    int priority;
626
627    switch (type) {
628        case SELINUX_WARNING:
629            priority = ANDROID_LOG_WARN;
630            break;
631        case SELINUX_INFO:
632            priority = ANDROID_LOG_INFO;
633            break;
634        default:
635            priority = ANDROID_LOG_ERROR;
636            break;
637    }
638    va_start(ap, fmt);
639    LOG_PRI_VA(priority, "SELinux", fmt, ap);
640    va_end(ap);
641    return 0;
642}
643
644static int otapreopt_main(const int argc, char *argv[]) {
645    int selinux_enabled = (is_selinux_enabled() > 0);
646
647    setenv("ANDROID_LOG_TAGS", "*:v", 1);
648    android::base::InitLogging(argv);
649
650    ALOGI("otapreopt firing up\n");
651
652    if (argc < 2) {
653        ALOGE("Expecting parameters");
654        exit(1);
655    }
656
657    union selinux_callback cb;
658    cb.func_log = log_callback;
659    selinux_set_callback(SELINUX_CB_LOG, cb);
660
661    if (!initialize_globals()) {
662        ALOGE("Could not initialize globals; exiting.\n");
663        exit(1);
664    }
665
666    if (!initialize_directories()) {
667        ALOGE("Could not create directories; exiting.\n");
668        exit(1);
669    }
670
671    if (selinux_enabled && selinux_status_open(true) < 0) {
672        ALOGE("Could not open selinux status; exiting.\n");
673        exit(1);
674    }
675
676    int ret = android::installd::gOps.Main(argc, argv);
677
678    return ret;
679}
680
681}  // namespace installd
682}  // namespace android
683
684int main(const int argc, char *argv[]) {
685    return android::installd::otapreopt_main(argc, argv);
686}
687