Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2773ab4d authored by Jiakai Zhang's avatar Jiakai Zhang Committed by Automerger Merge Worker
Browse files

Merge "Installd: don't generate app image with empty reference profile" am:...

Merge "Installd: don't generate app image with empty reference profile" am: ddae51e1 am: d6e3363f am: f0bccca4 am: 8fb761c1

Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/2455516



Change-Id: Ic5591c52cc5a86f170e2ddd25892d66ed4d2facb
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 32cdb606 8fb761c1
Loading
Loading
Loading
Loading
+5 −4
Original line number Original line Diff line number Diff line
@@ -1917,10 +1917,11 @@ int dexopt(const char* dex_path, uid_t uid, const char* pkgname, const char* ins
    // Open the reference profile if needed.
    // Open the reference profile if needed.
    UniqueFile reference_profile = maybe_open_reference_profile(
    UniqueFile reference_profile = maybe_open_reference_profile(
            pkgname, dex_path, profile_name, profile_guided, is_public, uid, is_secondary_dex);
            pkgname, dex_path, profile_name, profile_guided, is_public, uid, is_secondary_dex);

    struct stat sbuf;
    if (reference_profile.fd() == -1) {
    if (reference_profile.fd() == -1 ||
        // We don't create an app image without reference profile since there is no speedup from
        (fstat(reference_profile.fd(), &sbuf) != -1 && sbuf.st_size == 0)) {
        // loading it in that case and instead will be a small overhead.
        // We don't create an app image with empty or non existing reference profile since there
        // is no speedup from loading it in that case and instead will be a small overhead.
        generate_app_image = false;
        generate_app_image = false;
    }
    }


+52 −32
Original line number Original line Diff line number Diff line
@@ -185,7 +185,7 @@ protected:
    std::optional<std::string> volume_uuid_;
    std::optional<std::string> volume_uuid_;
    std::string package_name_;
    std::string package_name_;
    std::string apk_path_;
    std::string apk_path_;
    std::string empty_dm_file_;
    std::string dm_file_;
    std::string app_apk_dir_;
    std::string app_apk_dir_;
    std::string app_private_dir_ce_;
    std::string app_private_dir_ce_;
    std::string app_private_dir_de_;
    std::string app_private_dir_de_;
@@ -248,26 +248,6 @@ protected:
                                                 << " : " << error_msg;
                                                 << " : " << error_msg;
        }
        }


        // Create an empty dm file.
        empty_dm_file_ = apk_path_ + ".dm";
        {
            int fd = open(empty_dm_file_.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
            if (fd < 0) {
                return ::testing::AssertionFailure() << "Could not open " << empty_dm_file_;
            }
            FILE* file = fdopen(fd, "wb");
            if (file == nullptr) {
                return ::testing::AssertionFailure() << "Null file for " << empty_dm_file_
                         << " fd=" << fd;
            }
            ZipWriter writer(file);
            // Add vdex to zip.
            writer.StartEntry("primary.prof", ZipWriter::kCompress);
            writer.FinishEntry();
            writer.Finish();
            fclose(file);
          }

        // Create the app user data.
        // Create the app user data.
        binder::Status status = service_->createAppData(
        binder::Status status = service_->createAppData(
                volume_uuid_,
                volume_uuid_,
@@ -316,6 +296,46 @@ protected:
                                                 << secondary_dex_de_ << " : " << error_msg;
                                                 << secondary_dex_de_ << " : " << error_msg;
        }
        }


        // Create a non-empty dm file.
        dm_file_ = apk_path_ + ".dm";
        {
            android::base::unique_fd fd(open(dm_file_.c_str(),
                                          O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR));
            if (fd.get() < 0) {
                return ::testing::AssertionFailure() << "Could not open " << dm_file_;
            }
            FILE* file = fdopen(fd.release(), "wb");
            if (file == nullptr) {
                return ::testing::AssertionFailure() << "Null file for " << dm_file_
                                  << " fd=" << fd.get();
            }

            // Create a profile file.
            std::string profile_file = app_private_dir_ce_ + "/primary.prof";
            run_cmd("profman --generate-test-profile=" + profile_file);

            // Add profile to zip.
            ZipWriter writer(file);
            writer.StartEntry("primary.prof", ZipWriter::kCompress);
            android::base::unique_fd profile_fd(open(profile_file.c_str(), O_RDONLY));
            if (profile_fd.get() < 0) {
                return ::testing::AssertionFailure() << "Failed to open profile '"
                                  << profile_file << "'";
            }
            std::string profile_content;
            if (!android::base::ReadFdToString(profile_fd, &profile_content)) {
                return ::testing::AssertionFailure() << "Failed to read profile "
                                  << profile_file << "'";
            }
            writer.WriteBytes(profile_content.c_str(), profile_content.length());
            writer.FinishEntry();
            writer.Finish();
            fclose(file);

            // Delete the temp file.
            unlink(profile_file.c_str());
        }

        // Fix app data uid.
        // Fix app data uid.
        status = service_->fixupAppData(volume_uuid_, kTestUserId);
        status = service_->fixupAppData(volume_uuid_, kTestUserId);
        if (!status.isOk()) {
        if (!status.isOk()) {
@@ -608,7 +628,7 @@ protected:
                kTestAppGid,
                kTestAppGid,
                DEX2OAT_FROM_SCRATCH,
                DEX2OAT_FROM_SCRATCH,
                /*binder_result=*/nullptr,
                /*binder_result=*/nullptr,
                empty_dm_file_.c_str());
                dm_file_.c_str());




        int64_t odex_size = GetSize(GetPrimaryDexArtifact(oat_dir, apk_path_,
        int64_t odex_size = GetSize(GetPrimaryDexArtifact(oat_dir, apk_path_,
@@ -657,13 +677,13 @@ protected:
                            DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_PUBLIC |
                            DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_PUBLIC |
                                    DEXOPT_GENERATE_APP_IMAGE,
                                    DEXOPT_GENERATE_APP_IMAGE,
                            oat_dir, kTestAppGid, DEX2OAT_FROM_SCRATCH,
                            oat_dir, kTestAppGid, DEX2OAT_FROM_SCRATCH,
                            /*binder_result=*/nullptr, empty_dm_file_.c_str());
                            /*binder_result=*/nullptr, dm_file_.c_str());
        checkVisibility(in_dalvik_cache, ODEX_IS_PUBLIC);
        checkVisibility(in_dalvik_cache, ODEX_IS_PUBLIC);


        CompilePrimaryDexOk("speed-profile",
        CompilePrimaryDexOk("speed-profile",
                            DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_GENERATE_APP_IMAGE,
                            DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_GENERATE_APP_IMAGE,
                            oat_dir, kTestAppGid, DEX2OAT_FROM_SCRATCH,
                            oat_dir, kTestAppGid, DEX2OAT_FROM_SCRATCH,
                            /*binder_result=*/nullptr, empty_dm_file_.c_str());
                            /*binder_result=*/nullptr, dm_file_.c_str());
        checkVisibility(in_dalvik_cache, ODEX_IS_PRIVATE);
        checkVisibility(in_dalvik_cache, ODEX_IS_PRIVATE);
    }
    }
};
};
@@ -787,7 +807,7 @@ TEST_F(DexoptTest, DexoptPrimaryProfileNonPublic) {
                        kTestAppGid,
                        kTestAppGid,
                        DEX2OAT_FROM_SCRATCH,
                        DEX2OAT_FROM_SCRATCH,
                        /*binder_result=*/nullptr,
                        /*binder_result=*/nullptr,
                        empty_dm_file_.c_str());
                        dm_file_.c_str());
}
}


TEST_F(DexoptTest, DexoptPrimaryProfilePublic) {
TEST_F(DexoptTest, DexoptPrimaryProfilePublic) {
@@ -799,7 +819,7 @@ TEST_F(DexoptTest, DexoptPrimaryProfilePublic) {
                        kTestAppGid,
                        kTestAppGid,
                        DEX2OAT_FROM_SCRATCH,
                        DEX2OAT_FROM_SCRATCH,
                        /*binder_result=*/nullptr,
                        /*binder_result=*/nullptr,
                        empty_dm_file_.c_str());
                        dm_file_.c_str());
}
}


TEST_F(DexoptTest, DexoptPrimaryBackgroundOk) {
TEST_F(DexoptTest, DexoptPrimaryBackgroundOk) {
@@ -811,7 +831,7 @@ TEST_F(DexoptTest, DexoptPrimaryBackgroundOk) {
                        kTestAppGid,
                        kTestAppGid,
                        DEX2OAT_FROM_SCRATCH,
                        DEX2OAT_FROM_SCRATCH,
                        /*binder_result=*/nullptr,
                        /*binder_result=*/nullptr,
                        empty_dm_file_.c_str());
                        dm_file_.c_str());
}
}


TEST_F(DexoptTest, DexoptBlockPrimary) {
TEST_F(DexoptTest, DexoptBlockPrimary) {
@@ -874,7 +894,7 @@ TEST_F(DexoptTest, ResolveStartupConstStrings) {
                        kTestAppGid,
                        kTestAppGid,
                        DEX2OAT_FROM_SCRATCH,
                        DEX2OAT_FROM_SCRATCH,
                        /*binder_result=*/nullptr,
                        /*binder_result=*/nullptr,
                        empty_dm_file_.c_str());
                        dm_file_.c_str());
    run_cmd_and_process_output(
    run_cmd_and_process_output(
            "oatdump --header-only --oat-file=" + odex,
            "oatdump --header-only --oat-file=" + odex,
            [&](const std::string& line) {
            [&](const std::string& line) {
@@ -893,7 +913,7 @@ TEST_F(DexoptTest, ResolveStartupConstStrings) {
                        kTestAppGid,
                        kTestAppGid,
                        DEX2OAT_FROM_SCRATCH,
                        DEX2OAT_FROM_SCRATCH,
                        /*binder_result=*/nullptr,
                        /*binder_result=*/nullptr,
                        empty_dm_file_.c_str());
                        dm_file_.c_str());
    run_cmd_and_process_output(
    run_cmd_and_process_output(
            "oatdump --header-only --oat-file=" + odex,
            "oatdump --header-only --oat-file=" + odex,
            [&](const std::string& line) {
            [&](const std::string& line) {
@@ -926,7 +946,7 @@ TEST_F(DexoptTest, DexoptDex2oat64Enabled) {
                        kTestAppGid,
                        kTestAppGid,
                        DEX2OAT_FROM_SCRATCH,
                        DEX2OAT_FROM_SCRATCH,
                        /*binder_result=*/nullptr,
                        /*binder_result=*/nullptr,
                        empty_dm_file_.c_str());
                        dm_file_.c_str());
    // Enable the property and use dex2oat64.
    // Enable the property and use dex2oat64.
    ASSERT_TRUE(android::base::SetProperty(property, "true")) << property;
    ASSERT_TRUE(android::base::SetProperty(property, "true")) << property;
    CompilePrimaryDexOk("speed-profile",
    CompilePrimaryDexOk("speed-profile",
@@ -936,7 +956,7 @@ TEST_F(DexoptTest, DexoptDex2oat64Enabled) {
                        kTestAppGid,
                        kTestAppGid,
                        DEX2OAT_FROM_SCRATCH,
                        DEX2OAT_FROM_SCRATCH,
                        /*binder_result=*/nullptr,
                        /*binder_result=*/nullptr,
                        empty_dm_file_.c_str());
                        dm_file_.c_str());
}
}


class PrimaryDexReCompilationTest : public DexoptTest {
class PrimaryDexReCompilationTest : public DexoptTest {
@@ -1143,7 +1163,7 @@ class ProfileTest : public DexoptTest {
                service_->prepareAppProfile(package_name, has_user_id ? kTestUserId : USER_NULL,
                service_->prepareAppProfile(package_name, has_user_id ? kTestUserId : USER_NULL,
                                            kTestAppId, profile_name, apk_path_,
                                            kTestAppId, profile_name, apk_path_,
                                            has_dex_metadata ? std::make_optional<std::string>(
                                            has_dex_metadata ? std::make_optional<std::string>(
                                                                       empty_dm_file_)
                                                                       dm_file_)
                                                             : std::nullopt,
                                                             : std::nullopt,
                                            &result));
                                            &result));
        ASSERT_EQ(expected_result, result);
        ASSERT_EQ(expected_result, result);