Ensure the oat/vdex file is erased if we fail to truncate
Also, transform fd_file.cc CHECKS into DCHECKS to make sure we don't crash
on user devices.
Test: m test-art-host-gtest
Bug: 66903292
Change-Id: I4de921a3d621d616241ca73f1797c113cf153698
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index d8caf42..a12c3e1 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -1353,7 +1353,7 @@
DCHECK(!oat_filenames_.empty());
for (const char* oat_filename : oat_filenames_) {
std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_filename));
- if (oat_file.get() == nullptr) {
+ if (oat_file == nullptr) {
PLOG(ERROR) << "Failed to create oat file: " << oat_filename;
return false;
}
@@ -1383,7 +1383,7 @@
vdex_files_.push_back(std::move(vdex_file));
} else {
std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_filename.c_str()));
- if (vdex_file.get() == nullptr) {
+ if (vdex_file == nullptr) {
PLOG(ERROR) << "Failed to open vdex file: " << vdex_filename;
return false;
}
@@ -1397,13 +1397,15 @@
}
} else {
std::unique_ptr<File> oat_file(new File(oat_fd_, oat_location_, /* check_usage */ true));
- if (oat_file.get() == nullptr) {
+ if (oat_file == nullptr) {
PLOG(ERROR) << "Failed to create oat file: " << oat_location_;
return false;
}
oat_file->DisableAutoClose();
if (oat_file->SetLength(0) != 0) {
PLOG(WARNING) << "Truncating oat file " << oat_location_ << " failed.";
+ oat_file->Erase();
+ return false;
}
oat_files_.push_back(std::move(oat_file));
@@ -1432,7 +1434,7 @@
DCHECK_NE(output_vdex_fd_, -1);
std::string vdex_location = ReplaceFileExtension(oat_location_, "vdex");
std::unique_ptr<File> vdex_file(new File(output_vdex_fd_, vdex_location, /* check_usage */ true));
- if (vdex_file.get() == nullptr) {
+ if (vdex_file == nullptr) {
PLOG(ERROR) << "Failed to create vdex file: " << vdex_location;
return false;
}
@@ -1442,6 +1444,7 @@
} else {
if (vdex_file->SetLength(0) != 0) {
PLOG(ERROR) << "Truncating vdex file " << vdex_location << " failed.";
+ vdex_file->Erase();
return false;
}
}
diff --git a/runtime/base/unix_file/fd_file.cc b/runtime/base/unix_file/fd_file.cc
index eb8ced0..6d1de00 100644
--- a/runtime/base/unix_file/fd_file.cc
+++ b/runtime/base/unix_file/fd_file.cc
@@ -70,7 +70,7 @@
if (guard_state_ < GuardState::kClosed) {
LOG(ERROR) << "File " << file_path_ << " wasn't explicitly closed before destruction.";
}
- CHECK_GE(guard_state_, GuardState::kClosed);
+ DCHECK_GE(guard_state_, GuardState::kClosed);
}
if (auto_close_ && fd_ != -1) {
if (Close() != 0) {
@@ -135,7 +135,7 @@
bool FdFile::Open(const std::string& path, int flags, mode_t mode) {
static_assert(O_RDONLY == 0, "Readonly flag has unexpected value.");
- CHECK_EQ(fd_, -1) << path;
+ DCHECK_EQ(fd_, -1) << path;
read_only_mode_ = ((flags & O_ACCMODE) == O_RDONLY);
fd_ = TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode));
if (fd_ == -1) {
@@ -158,7 +158,7 @@
// Test here, so the file is closed and not leaked.
if (kCheckSafeUsage) {
- CHECK_GE(guard_state_, GuardState::kFlushed) << "File " << file_path_
+ DCHECK_GE(guard_state_, GuardState::kFlushed) << "File " << file_path_
<< " has not been flushed before closing.";
moveUp(GuardState::kClosed, nullptr);
}