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

Commit 10b8a6e7 authored by Jeff Hao's avatar Jeff Hao
Browse files

Run dex2oat from the staged apk directory.

The frameworks/base change makes the PackageManager pass relative paths
of previous code paths as shared library paths. This change passes the
base directory for these relative paths to dex2oat using the new switch
--classpath-dir.

Part of a multi-project change.

Bug: 34169257
Test: cts-tradefed run singleCommand cts -d --module
CtsAppSecurityHostTestCases -t android.appsecurity.cts.SplitTests

(cherry-picked from commit cc5c4e32)

Change-Id: I8f941eb9982a051c1f49256d5489f2575af1d80f
parent 5330710a
Loading
Loading
Loading
Loading
+32 −17
Original line number Original line Diff line number Diff line
@@ -177,6 +177,17 @@ static int split(char *buf, const char **argv)
  return count;
  return count;
}
}


static const char* get_location_from_path(const char* path) {
    static constexpr char kLocationSeparator = '/';
    const char *location = strrchr(path, kLocationSeparator);
    if (location == NULL) {
        return path;
    } else {
        // Skip the separator character.
        return location + 1;
    }
}

static void run_dex2oat(int zip_fd, int oat_fd, int input_vdex_fd, int output_vdex_fd, int image_fd,
static void run_dex2oat(int zip_fd, int oat_fd, int input_vdex_fd, int output_vdex_fd, int image_fd,
        const char* input_file_name, const char* output_file_name, int swap_fd,
        const char* input_file_name, const char* output_file_name, int swap_fd,
        const char* instruction_set, const char* compiler_filter, bool vm_safe_mode,
        const char* instruction_set, const char* compiler_filter, bool vm_safe_mode,
@@ -189,6 +200,9 @@ static void run_dex2oat(int zip_fd, int oat_fd, int input_vdex_fd, int output_vd
        return;
        return;
    }
    }


    // Get the relative path to the input file.
    const char* relative_input_file_name = get_location_from_path(input_file_name);

    char dex2oat_Xms_flag[kPropertyValueMax];
    char dex2oat_Xms_flag[kPropertyValueMax];
    bool have_dex2oat_Xms_flag = get_property("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
    bool have_dex2oat_Xms_flag = get_property("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;


@@ -279,7 +293,7 @@ static void run_dex2oat(int zip_fd, int oat_fd, int input_vdex_fd, int output_vd
    char dex2oat_image_fd[arraysize("--app-image-fd=") + MAX_INT_LEN];
    char dex2oat_image_fd[arraysize("--app-image-fd=") + MAX_INT_LEN];


    sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
    sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
    sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
    sprintf(zip_location_arg, "--zip-location=%s", relative_input_file_name);
    sprintf(input_vdex_fd_arg, "--input-vdex-fd=%d", input_vdex_fd);
    sprintf(input_vdex_fd_arg, "--input-vdex-fd=%d", input_vdex_fd);
    sprintf(output_vdex_fd_arg, "--output-vdex-fd=%d", output_vdex_fd);
    sprintf(output_vdex_fd_arg, "--output-vdex-fd=%d", output_vdex_fd);
    sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
    sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
@@ -341,8 +355,18 @@ static void run_dex2oat(int zip_fd, int oat_fd, int input_vdex_fd, int output_vd
        sprintf(profile_arg, "--profile-file-fd=%d", profile_fd);
        sprintf(profile_arg, "--profile-file-fd=%d", profile_fd);
    }
    }


    // Get the directory of the apk to pass as a base classpath directory.
    char base_dir[arraysize("--classpath-dir=") + PKG_PATH_MAX];
    std::string apk_dir(input_file_name);
    unsigned long dir_index = apk_dir.rfind('/');
    bool has_base_dir = dir_index != std::string::npos;
    if (has_base_dir) {
        apk_dir = apk_dir.substr(0, dir_index);
        sprintf(base_dir, "--classpath-dir=%s", apk_dir.c_str());
    }



    ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
    ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, relative_input_file_name, output_file_name);


    const char* argv[9  // program name, mandatory arguments and the final NULL
    const char* argv[9  // program name, mandatory arguments and the final NULL
                     + (have_dex2oat_isa_variant ? 1 : 0)
                     + (have_dex2oat_isa_variant ? 1 : 0)
@@ -360,6 +384,7 @@ static void run_dex2oat(int zip_fd, int oat_fd, int input_vdex_fd, int output_vd
                     + dex2oat_flags_count
                     + dex2oat_flags_count
                     + (profile_fd == -1 ? 0 : 1)
                     + (profile_fd == -1 ? 0 : 1)
                     + (shared_libraries != nullptr ? 4 : 0)
                     + (shared_libraries != nullptr ? 4 : 0)
                     + (has_base_dir ? 1 : 0)
                     + (have_dex2oat_large_app_threshold ? 1 : 0)];
                     + (have_dex2oat_large_app_threshold ? 1 : 0)];
    int i = 0;
    int i = 0;
    argv[i++] = DEX2OAT_BIN;
    argv[i++] = DEX2OAT_BIN;
@@ -424,6 +449,9 @@ static void run_dex2oat(int zip_fd, int oat_fd, int input_vdex_fd, int output_vd
        argv[i++] = RUNTIME_ARG;
        argv[i++] = RUNTIME_ARG;
        argv[i++] = shared_libraries;
        argv[i++] = shared_libraries;
    }
    }
    if (has_base_dir) {
        argv[i++] = base_dir;
    }
    // Do not add after dex2oat_flags, they should override others for debugging.
    // Do not add after dex2oat_flags, they should override others for debugging.
    argv[i] = NULL;
    argv[i] = NULL;


@@ -761,17 +789,6 @@ static void run_profman_dump(const std::vector<unique_fd>& profile_fds,
    exit(68);   /* only get here on exec failure */
    exit(68);   /* only get here on exec failure */
}
}


static const char* get_location_from_path(const char* path) {
    static constexpr char kLocationSeparator = '/';
    const char *location = strrchr(path, kLocationSeparator);
    if (location == NULL) {
        return path;
    } else {
        // Skip the separator character.
        return location + 1;
    }
}

bool dump_profiles(int32_t uid, const std::string& pkgname, const char* code_paths) {
bool dump_profiles(int32_t uid, const std::string& pkgname, const char* code_paths) {
    std::vector<unique_fd> profile_fds;
    std::vector<unique_fd> profile_fds;
    unique_fd reference_profile_fd;
    unique_fd reference_profile_fd;
@@ -1548,14 +1565,12 @@ int dexopt(const char* dex_path, uid_t uid, const char* pkgname, const char* ins
            _exit(67);
            _exit(67);
        }
        }


        // Pass dex2oat the relative path to the input file.
        const char *input_file_name = get_location_from_path(dex_path);
        run_dex2oat(input_fd.get(),
        run_dex2oat(input_fd.get(),
                    out_oat_fd.get(),
                    out_oat_fd.get(),
                    in_vdex_fd.get(),
                    in_vdex_fd.get(),
                    out_vdex_fd.get(),
                    out_vdex_fd.get(),
                    image_fd.get(),
                    image_fd.get(),
                    input_file_name,
                    dex_path,
                    out_oat_path,
                    out_oat_path,
                    swap_fd.get(),
                    swap_fd.get(),
                    instruction_set,
                    instruction_set,