summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Jiakai Zhang <jiakaiz@google.com> 2024-08-21 14:10:46 +0100
committer Jiakai Zhang <jiakaiz@google.com> 2024-08-22 14:34:49 +0000
commit2b0a73bc7dd8d79da1dd8362a22e3a02caa3fd6d (patch)
treec9e87e00b53075cbae0e630ae1654d1e0aca86bd
parent01e082af26edc8a26ffdbbac8bdec0dcf55f07ed (diff)
Remove the dependency on libselinux from odrefresh_test.
odrefresh_test is supposed to be a self-contained unit test, so it shouldn't call into the real libselinux. Bug: 357802879 Test: art/tools/run-gtests.sh /apex/com.android.art/bin/art/arm64/art_odrefresh_tests Change-Id: I23197aeb65e554fbaa2a543b5e8bc8b45ff60c1d
-rw-r--r--odrefresh/Android.bp2
-rw-r--r--odrefresh/odrefresh.cc27
-rw-r--r--odrefresh/odrefresh.h18
-rw-r--r--odrefresh/odrefresh_main.cc4
-rw-r--r--odrefresh/odrefresh_test.cc7
5 files changed, 37 insertions, 21 deletions
diff --git a/odrefresh/Android.bp b/odrefresh/Android.bp
index e06b5a8685..53e5f90fa6 100644
--- a/odrefresh/Android.bp
+++ b/odrefresh/Android.bp
@@ -40,7 +40,6 @@ cc_defaults {
],
shared_libs: [
"liblog",
- "libselinux",
],
static_libs: [
"libmodules-utils-build",
@@ -76,6 +75,7 @@ cc_defaults {
"libarttools",
"libbase",
"libdexfile",
+ "libselinux",
],
}
diff --git a/odrefresh/odrefresh.cc b/odrefresh/odrefresh.cc
index db0c808375..0110a6ec45 100644
--- a/odrefresh/odrefresh.cc
+++ b/odrefresh/odrefresh.cc
@@ -85,8 +85,6 @@
#include "odr_fs_utils.h"
#include "odr_metrics.h"
#include "odrefresh/odrefresh.h"
-#include "selinux/android.h"
-#include "selinux/selinux.h"
#include "tools/cmdline_builder.h"
namespace art {
@@ -145,7 +143,8 @@ void EraseFiles(const std::vector<std::unique_ptr<File>>& files) {
//
// Returns true if all files are moved, false otherwise.
bool MoveOrEraseFiles(const std::vector<std::unique_ptr<File>>& files,
- std::string_view output_directory_path) {
+ std::string_view output_directory_path,
+ android::base::function_ref<int(const char*, unsigned int)> restorecon) {
std::vector<std::unique_ptr<File>> output_files;
for (auto& file : files) {
std::string file_basename(Basename(file->GetPath()));
@@ -168,7 +167,7 @@ bool MoveOrEraseFiles(const std::vector<std::unique_ptr<File>>& files,
return false;
}
- if (selinux_android_restorecon(output_file_path.c_str(), 0) < 0) {
+ if (restorecon(output_file_path.c_str(), 0) < 0) {
LOG(ERROR) << "Failed to set security context for file " << QuotePath(output_file_path);
EraseFiles(files);
return false;
@@ -683,25 +682,31 @@ int CompilationOptions::CompilationUnitCount() const {
return count;
}
-OnDeviceRefresh::OnDeviceRefresh(const OdrConfig& config)
+OnDeviceRefresh::OnDeviceRefresh(
+ const OdrConfig& config,
+ android::base::function_ref<int(const char*, const char*)> setfilecon,
+ android::base::function_ref<int(const char*, unsigned int)> restorecon)
: OnDeviceRefresh(config,
+ setfilecon,
+ restorecon,
config.GetArtifactDirectory() + "/" + kCacheInfoFile,
std::make_unique<ExecUtils>(),
- CheckCompilationSpace,
- setfilecon) {}
+ CheckCompilationSpace) {}
OnDeviceRefresh::OnDeviceRefresh(
const OdrConfig& config,
+ android::base::function_ref<int(const char*, const char*)> setfilecon,
+ android::base::function_ref<int(const char*, unsigned int)> restorecon,
const std::string& cache_info_filename,
std::unique_ptr<ExecUtils> exec_utils,
- android::base::function_ref<bool()> check_compilation_space,
- android::base::function_ref<int(const char*, const char*)> setfilecon)
+ android::base::function_ref<bool()> check_compilation_space)
: config_(config),
cache_info_filename_(cache_info_filename),
start_time_(time(nullptr)),
exec_utils_(std::move(exec_utils)),
check_compilation_space_(check_compilation_space),
- setfilecon_(setfilecon) {
+ setfilecon_(setfilecon),
+ restorecon_(restorecon) {
// Updatable APEXes should not have DEX files in the DEX2OATBOOTCLASSPATH. At the time of
// writing i18n is a non-updatable APEX and so does appear in the DEX2OATBOOTCLASSPATH.
dex2oat_boot_classpath_jars_ = Split(config_.GetDex2oatBootClasspath(), ":");
@@ -1825,7 +1830,7 @@ WARN_UNUSED CompilationResult OnDeviceRefresh::RunDex2oat(
ART_FORMAT("Failed to flush file '{}'", file->GetPath()));
}
}
- if (!MoveOrEraseFiles(output_files, install_location)) {
+ if (!MoveOrEraseFiles(output_files, install_location, restorecon_)) {
return CompilationResult::Error(
OdrMetrics::Status::kIoError,
ART_FORMAT("Failed to commit artifacts to '{}'", install_location));
diff --git a/odrefresh/odrefresh.h b/odrefresh/odrefresh.h
index 3031b1b554..301ef64157 100644
--- a/odrefresh/odrefresh.h
+++ b/odrefresh/odrefresh.h
@@ -158,14 +158,19 @@ class PreconditionCheckResult {
class OnDeviceRefresh final {
public:
- explicit OnDeviceRefresh(const OdrConfig& config);
+ explicit OnDeviceRefresh(
+ const OdrConfig& config,
+ android::base::function_ref<int(const char*, const char*)> setfilecon,
+ android::base::function_ref<int(const char*, unsigned int)> restorecon);
// Constructor with injections. For testing and internal use only.
- OnDeviceRefresh(const OdrConfig& config,
- const std::string& cache_info_filename,
- std::unique_ptr<ExecUtils> exec_utils,
- android::base::function_ref<bool()> check_compilation_space,
- android::base::function_ref<int(const char*, const char*)> setfilecon);
+ OnDeviceRefresh(
+ const OdrConfig& config,
+ android::base::function_ref<int(const char*, const char*)> setfilecon,
+ android::base::function_ref<int(const char*, unsigned int)> restorecon,
+ const std::string& cache_info_filename,
+ std::unique_ptr<ExecUtils> exec_utils,
+ android::base::function_ref<bool()> check_compilation_space);
// Returns the exit code and specifies what should be compiled in `compilation_options`.
WARN_UNUSED ExitCode
@@ -388,6 +393,7 @@ class OnDeviceRefresh final {
android::base::function_ref<bool()> check_compilation_space_;
android::base::function_ref<int(const char*, const char*)> setfilecon_;
+ android::base::function_ref<int(const char*, unsigned int)> restorecon_;
DISALLOW_COPY_AND_ASSIGN(OnDeviceRefresh);
};
diff --git a/odrefresh/odrefresh_main.cc b/odrefresh/odrefresh_main.cc
index 2d3009e25c..22551f96f1 100644
--- a/odrefresh/odrefresh_main.cc
+++ b/odrefresh/odrefresh_main.cc
@@ -35,6 +35,8 @@
#include "odr_metrics.h"
#include "odrefresh.h"
#include "odrefresh/odrefresh.h"
+#include "selinux/android.h"
+#include "selinux/selinux.h"
namespace {
@@ -278,7 +280,7 @@ int main(int argc, char** argv) {
GetSystemProperties(config.MutableSystemProperties());
OdrMetrics metrics(config.GetArtifactDirectory());
- OnDeviceRefresh odr(config);
+ OnDeviceRefresh odr(config, setfilecon, selinux_android_restorecon);
std::string_view action(argv[0]);
CompilationOptions compilation_options;
diff --git a/odrefresh/odrefresh_test.cc b/odrefresh/odrefresh_test.cc
index f44c3211c3..b19a4225b4 100644
--- a/odrefresh/odrefresh_test.cc
+++ b/odrefresh/odrefresh_test.cc
@@ -233,11 +233,13 @@ class OdRefreshTest : public CommonArtTest {
cache_info_xml_ = dalvik_cache_dir_ + "/cache-info.xml";
check_compilation_space_ = [] { return true; };
setfilecon_ = [](auto, auto) { return 0; };
+ restorecon_ = [](auto, auto) { return 0; };
odrefresh_ = std::make_unique<OnDeviceRefresh>(config_,
+ setfilecon_,
+ restorecon_,
cache_info_xml_,
std::move(mock_exec_utils),
- check_compilation_space_,
- setfilecon_);
+ check_compilation_space_);
}
void TearDown() override {
@@ -277,6 +279,7 @@ class OdRefreshTest : public CommonArtTest {
std::string cache_info_xml_;
std::function<bool()> check_compilation_space_;
std::function<int(const char*, const char*)> setfilecon_;
+ std::function<int(const char*, unsigned int)> restorecon_;
};
TEST_F(OdRefreshTest, PrimaryBootImage) {