Merge "Fix system_weak_test"
diff --git a/adbconnection/adbconnection.cc b/adbconnection/adbconnection.cc
index 2050133..ba25393 100644
--- a/adbconnection/adbconnection.cc
+++ b/adbconnection/adbconnection.cc
@@ -20,6 +20,7 @@
#include "android-base/endian.h"
#include "android-base/stringprintf.h"
+#include "base/file_utils.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/mutex.h"
@@ -428,11 +429,11 @@
cmsg->cmsg_type = SCM_RIGHTS;
// Duplicate the fds before sending them.
- android::base::unique_fd read_fd(dup(adb_connection_socket_));
+ android::base::unique_fd read_fd(art::DupCloexec(adb_connection_socket_));
CHECK_NE(read_fd.get(), -1) << "Failed to dup read_fd_: " << strerror(errno);
- android::base::unique_fd write_fd(dup(adb_connection_socket_));
+ android::base::unique_fd write_fd(art::DupCloexec(adb_connection_socket_));
CHECK_NE(write_fd.get(), -1) << "Failed to dup write_fd: " << strerror(errno);
- android::base::unique_fd write_lock_fd(dup(adb_write_event_fd_));
+ android::base::unique_fd write_lock_fd(art::DupCloexec(adb_write_event_fd_));
CHECK_NE(write_lock_fd.get(), -1) << "Failed to dup write_lock_fd: " << strerror(errno);
dt_fd_forward::FdSet {
diff --git a/build/Android.bp b/build/Android.bp
index 47a540d..09d3a18 100644
--- a/build/Android.bp
+++ b/build/Android.bp
@@ -18,6 +18,8 @@
}
art_clang_tidy_errors = [
+ "android-cloexec-dup",
+ "android-cloexec-open",
"bugprone-argument-comment",
"bugprone-lambda-function-name",
"bugprone-unused-raii", // Protect scoped things like MutexLock.
@@ -35,7 +37,9 @@
"misc-unused-using-decls",
]
// Should be: strings.Join(art_clang_tidy_errors, ",").
-art_clang_tidy_errors_str = "bugprone-argument-comment"
+art_clang_tidy_errors_str = "android-cloexec-dup"
+ + ",android-cloexec-open"
+ + ",bugprone-argument-comment"
+ ",bugprone-lambda-function-name"
+ ",bugprone-unused-raii"
+ ",bugprone-unused-return-value"
diff --git a/dex2oat/dex2oat_test.cc b/dex2oat/dex2oat_test.cc
index 6f53861..fbad1af 100644
--- a/dex2oat/dex2oat_test.cc
+++ b/dex2oat/dex2oat_test.cc
@@ -634,7 +634,9 @@
const std::string& dex_location,
size_t num_classes,
uint32_t checksum) {
- int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
+ int profile_test_fd = open(test_profile.c_str(),
+ O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC,
+ 0644);
CHECK_GE(profile_test_fd, 0);
ProfileCompilationInfo info;
@@ -1698,7 +1700,7 @@
// Create a multidex file with only one dex that gets rejected for cdex conversion.
ScratchFile apk_file;
{
- FILE* file = fdopen(dup(apk_file.GetFd()), "w+b");
+ FILE* file = fdopen(DupCloexec(apk_file.GetFd()), "w+b");
ZipWriter writer(file);
// Add vdex to zip.
writer.StartEntry("classes.dex", ZipWriter::kCompress);
@@ -1837,7 +1839,7 @@
std::unique_ptr<File> vdex_file(OS::OpenFileForReading(vdex_location.c_str()));
ASSERT_TRUE(vdex_file != nullptr);
ASSERT_GT(vdex_file->GetLength(), 0u);
- FILE* file = fdopen(dup(dm_file.GetFd()), "w+b");
+ FILE* file = fdopen(DupCloexec(dm_file.GetFd()), "w+b");
ZipWriter writer(file);
auto write_all_bytes = [&](File* file) {
std::unique_ptr<uint8_t[]> bytes(new uint8_t[file->GetLength()]);
@@ -1963,7 +1965,7 @@
TEST_F(Dex2oatTest, CompactDexInvalidSource) {
ScratchFile invalid_dex;
{
- FILE* file = fdopen(dup(invalid_dex.GetFd()), "w+b");
+ FILE* file = fdopen(DupCloexec(invalid_dex.GetFd()), "w+b");
ZipWriter writer(file);
writer.StartEntry("classes.dex", ZipWriter::kAlign32);
DexFile::Header header = {};
@@ -2005,7 +2007,7 @@
// Create a zip containing the invalid dex.
ScratchFile invalid_dex_zip;
{
- FILE* file = fdopen(dup(invalid_dex_zip.GetFd()), "w+b");
+ FILE* file = fdopen(DupCloexec(invalid_dex_zip.GetFd()), "w+b");
ZipWriter writer(file);
writer.StartEntry("classes.dex", ZipWriter::kCompress);
ASSERT_GE(writer.WriteBytes(&header, sizeof(header)), 0);
diff --git a/dex2oat/linker/oat_writer.cc b/dex2oat/linker/oat_writer.cc
index acd49d5..23c486d 100644
--- a/dex2oat/linker/oat_writer.cc
+++ b/dex2oat/linker/oat_writer.cc
@@ -26,6 +26,7 @@
#include "base/bit_vector-inl.h"
#include "base/enums.h"
#include "base/file_magic.h"
+#include "base/file_utils.h"
#include "base/indenter.h"
#include "base/logging.h" // For VLOG
#include "base/os.h"
@@ -3430,7 +3431,7 @@
&error_msg);
} else if (oat_dex_file->source_.IsRawFile()) {
File* raw_file = oat_dex_file->source_.GetRawFile();
- int dup_fd = dup(raw_file->Fd());
+ int dup_fd = DupCloexec(raw_file->Fd());
if (dup_fd < 0) {
PLOG(ERROR) << "Failed to dup dex file descriptor (" << raw_file->Fd() << ") at " << location;
return false;
diff --git a/dex2oat/linker/oat_writer_test.cc b/dex2oat/linker/oat_writer_test.cc
index 7382208..83fb17c 100644
--- a/dex2oat/linker/oat_writer_test.cc
+++ b/dex2oat/linker/oat_writer_test.cc
@@ -19,6 +19,7 @@
#include "arch/instruction_set_features.h"
#include "art_method-inl.h"
#include "base/enums.h"
+#include "base/file_utils.h"
#include "base/stl_util.h"
#include "base/unix_file/fd_file.h"
#include "class_linker.h"
@@ -765,7 +766,7 @@
{
// Test using the AddZipDexFileSource() interface with the zip file handle.
- File zip_fd(dup(zip_file.GetFd()), /*check_usage=*/ false);
+ File zip_fd(DupCloexec(zip_file.GetFd()), /*check_usage=*/ false);
ASSERT_NE(-1, zip_fd.Fd());
ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
diff --git a/dexlayout/dex_visualize.cc b/dexlayout/dex_visualize.cc
index 4a36744..27cec8d9 100644
--- a/dexlayout/dex_visualize.cc
+++ b/dexlayout/dex_visualize.cc
@@ -53,7 +53,7 @@
bool OpenAndPrintHeader(size_t dex_index) {
// Open the file and emit the gnuplot prologue.
- out_file_ = fopen(MultidexName("layout", dex_index, ".gnuplot").c_str(), "w");
+ out_file_ = fopen(MultidexName("layout", dex_index, ".gnuplot").c_str(), "we");
if (out_file_ == nullptr) {
return false;
}
diff --git a/dexlayout/dexlayout_main.cc b/dexlayout/dexlayout_main.cc
index d212e71..41b60da 100644
--- a/dexlayout/dexlayout_main.cc
+++ b/dexlayout/dexlayout_main.cc
@@ -190,7 +190,7 @@
// Open profile file.
std::unique_ptr<ProfileCompilationInfo> profile_info;
if (options.profile_file_name_) {
- int profile_fd = open(options.profile_file_name_, O_RDONLY);
+ int profile_fd = open(options.profile_file_name_, O_RDONLY | O_CLOEXEC);
if (profile_fd < 0) {
PLOG(ERROR) << "Can't open " << options.profile_file_name_;
return 1;
diff --git a/dexlist/dexlist.cc b/dexlist/dexlist.cc
index adb6a54..7d3ae71 100644
--- a/dexlist/dexlist.cc
+++ b/dexlist/dexlist.cc
@@ -257,7 +257,7 @@
// Open alternative output file.
if (gOptions.outputFileName) {
- gOutFile = fopen(gOptions.outputFileName, "w");
+ gOutFile = fopen(gOptions.outputFileName, "we");
if (!gOutFile) {
PLOG(ERROR) << "Can't open " << gOptions.outputFileName;
free(gOptions.argCopy);
diff --git a/dt_fd_forward/dt_fd_forward.cc b/dt_fd_forward/dt_fd_forward.cc
index 116cdf8..a99f785 100644
--- a/dt_fd_forward/dt_fd_forward.cc
+++ b/dt_fd_forward/dt_fd_forward.cc
@@ -105,12 +105,21 @@
TEMP_FAILURE_RETRY(send(fd, kListenStartMessage, sizeof(kListenStartMessage), MSG_EOR));
}
+// Copy from file_utils, so we do not need to depend on libartbase.
+static int DupCloexec(int fd) {
+#if defined(__linux__)
+ return fcntl(fd, F_DUPFD_CLOEXEC, 0);
+#else
+ return dup(fd);
+#endif
+}
+
jdwpTransportError FdForwardTransport::SetupListen(int listen_fd) {
std::lock_guard<std::mutex> lk(state_mutex_);
if (!ChangeState(TransportState::kClosed, TransportState::kListenSetup)) {
return ERR(ILLEGAL_STATE);
} else {
- listen_fd_.reset(dup(listen_fd));
+ listen_fd_.reset(DupCloexec(listen_fd));
SendListenMessage(listen_fd_);
CHECK(ChangeState(TransportState::kListenSetup, TransportState::kListening));
return OK;
@@ -339,7 +348,7 @@
write_lock_fd_.reset(out_fds.write_lock_fd_);
// We got the fds. Send ack.
- close_notify_fd_.reset(dup(listen_fd_));
+ close_notify_fd_.reset(DupCloexec(listen_fd_));
SendAcceptMessage(close_notify_fd_);
return IOResult::kOk;
diff --git a/libartbase/base/common_art_test.cc b/libartbase/base/common_art_test.cc
index 9485fca..987ceb6 100644
--- a/libartbase/base/common_art_test.cc
+++ b/libartbase/base/common_art_test.cc
@@ -62,7 +62,7 @@
: ScratchFile(other.GetFilename() + suffix) {}
ScratchFile::ScratchFile(const std::string& filename) : filename_(filename) {
- int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666);
+ int fd = open(filename_.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0666);
CHECK_NE(-1, fd);
file_.reset(new File(fd, GetFilename(), true));
}
diff --git a/libartbase/base/scoped_flock.cc b/libartbase/base/scoped_flock.cc
index beee501..2f16fb2 100644
--- a/libartbase/base/scoped_flock.cc
+++ b/libartbase/base/scoped_flock.cc
@@ -22,6 +22,7 @@
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
+#include "file_utils.h"
#include "unix_file/fd_file.h"
namespace art {
@@ -98,7 +99,7 @@
// destructor. Callers should explicitly flush files they're writing to if
// that is the desired behaviour.
ScopedFlock locked_file(
- new LockedFile(dup(fd), path, /* check_usage= */ false, read_only_mode));
+ new LockedFile(DupCloexec(fd), path, /* check_usage= */ false, read_only_mode));
if (locked_file->Fd() == -1) {
*error_msg = StringPrintf("Failed to duplicate open file '%s': %s",
locked_file->GetPath().c_str(), strerror(errno));
diff --git a/libartbase/base/unix_file/fd_file.cc b/libartbase/base/unix_file/fd_file.cc
index de60277..76894c6 100644
--- a/libartbase/base/unix_file/fd_file.cc
+++ b/libartbase/base/unix_file/fd_file.cc
@@ -431,7 +431,7 @@
bool is_current = false;
{
struct stat this_stat, current_stat;
- int cur_fd = TEMP_FAILURE_RETRY(open(file_path_.c_str(), O_RDONLY));
+ int cur_fd = TEMP_FAILURE_RETRY(open(file_path_.c_str(), O_RDONLY | O_CLOEXEC));
if (cur_fd > 0) {
// File still exists.
if (fstat(fd_, &this_stat) == 0 && fstat(cur_fd, ¤t_stat) == 0) {
diff --git a/libartbase/base/unix_file/fd_file_test.cc b/libartbase/base/unix_file/fd_file_test.cc
index 9c39bb5..3a9cf59 100644
--- a/libartbase/base/unix_file/fd_file_test.cc
+++ b/libartbase/base/unix_file/fd_file_test.cc
@@ -15,6 +15,7 @@
*/
#include "base/common_art_test.h" // For ScratchFile
+#include "base/file_utils.h"
#include "gtest/gtest.h"
#include "fd_file.h"
#include "random_access_file_test.h"
@@ -25,7 +26,7 @@
protected:
RandomAccessFile* MakeTestFile() override {
FILE* tmp = tmpfile();
- int fd = dup(fileno(tmp));
+ int fd = art::DupCloexec(fileno(tmp));
fclose(tmp);
return new FdFile(fd, false);
}
diff --git a/libartbase/base/zip_archive_test.cc b/libartbase/base/zip_archive_test.cc
index b923881..969cf12 100644
--- a/libartbase/base/zip_archive_test.cc
+++ b/libartbase/base/zip_archive_test.cc
@@ -23,6 +23,7 @@
#include <memory>
#include "base/common_art_test.h"
+#include "file_utils.h"
#include "os.h"
#include "unix_file/fd_file.h"
@@ -41,7 +42,7 @@
ScratchFile tmp;
ASSERT_NE(-1, tmp.GetFd());
- std::unique_ptr<File> file(new File(dup(tmp.GetFd()), tmp.GetFilename(), false));
+ std::unique_ptr<File> file(new File(DupCloexec(tmp.GetFd()), tmp.GetFilename(), false));
ASSERT_TRUE(file.get() != nullptr);
bool success = zip_entry->ExtractToFile(*file, &error_msg);
ASSERT_TRUE(success) << error_msg;
@@ -49,7 +50,7 @@
file.reset(nullptr);
uint32_t computed_crc = crc32(0L, Z_NULL, 0);
- int fd = open(tmp.GetFilename().c_str(), O_RDONLY);
+ int fd = open(tmp.GetFilename().c_str(), O_RDONLY | O_CLOEXEC);
ASSERT_NE(-1, fd);
const size_t kBufSize = 32768;
uint8_t buf[kBufSize];
diff --git a/profman/profile_assistant_test.cc b/profman/profile_assistant_test.cc
index 31dfbc0..e9d3290 100644
--- a/profman/profile_assistant_test.cc
+++ b/profman/profile_assistant_test.cc
@@ -1192,7 +1192,7 @@
// Run profman and pass the dex file with --apk-fd.
android::base::unique_fd apk_fd(
- open(GetTestDexFileName("ProfileTestMultiDex").c_str(), O_RDONLY));
+ open(GetTestDexFileName("ProfileTestMultiDex").c_str(), O_RDONLY)); // NOLINT
ASSERT_GE(apk_fd.get(), 0);
std::string profman_cmd = GetProfmanCmd();
@@ -1270,7 +1270,7 @@
// Run profman and pass the dex file with --apk-fd.
android::base::unique_fd apk_fd(
- open(GetTestDexFileName("ProfileTestMultiDex").c_str(), O_RDONLY));
+ open(GetTestDexFileName("ProfileTestMultiDex").c_str(), O_RDONLY)); // NOLINT
ASSERT_GE(apk_fd.get(), 0);
std::string profman_cmd = GetProfmanCmd();
diff --git a/profman/profman.cc b/profman/profman.cc
index d989c8c..734cdf4 100644
--- a/profman/profman.cc
+++ b/profman/profman.cc
@@ -477,7 +477,7 @@
std::unique_ptr<const ProfileCompilationInfo> LoadProfile(const std::string& filename, int fd) {
if (!filename.empty()) {
- fd = open(filename.c_str(), O_RDWR);
+ fd = open(filename.c_str(), O_RDWR | O_CLOEXEC);
if (fd < 0) {
LOG(ERROR) << "Cannot open " << filename << strerror(errno);
return nullptr;
@@ -641,7 +641,7 @@
bool GetClassNamesAndMethods(const std::string& profile_file,
std::vector<std::unique_ptr<const DexFile>>* dex_files,
std::set<std::string>* out_lines) {
- int fd = open(profile_file.c_str(), O_RDONLY);
+ int fd = open(profile_file.c_str(), O_RDONLY | O_CLOEXEC);
if (!FdIsValid(fd)) {
LOG(ERROR) << "Cannot open " << profile_file << strerror(errno);
return false;
@@ -1022,7 +1022,7 @@
int fd = reference_profile_file_fd_;
if (!FdIsValid(fd)) {
CHECK(!reference_profile_file_.empty());
- fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
+ fd = open(reference_profile_file_.c_str(), O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
if (fd < 0) {
LOG(ERROR) << "Cannot open " << reference_profile_file_ << strerror(errno);
return kInvalidFd;
@@ -1155,7 +1155,9 @@
}
}
// ShouldGenerateTestProfile confirms !test_profile_.empty().
- int profile_test_fd = open(test_profile_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
+ int profile_test_fd = open(test_profile_.c_str(),
+ O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC,
+ 0644);
if (profile_test_fd < 0) {
LOG(ERROR) << "Cannot open " << test_profile_ << strerror(errno);
return -1;
diff --git a/runtime/hprof/hprof.cc b/runtime/hprof/hprof.cc
index 438af12..832bacb 100644
--- a/runtime/hprof/hprof.cc
+++ b/runtime/hprof/hprof.cc
@@ -41,6 +41,7 @@
#include "art_field-inl.h"
#include "art_method-inl.h"
#include "base/array_ref.h"
+#include "base/file_utils.h"
#include "base/globals.h"
#include "base/macros.h"
#include "base/mutex.h"
@@ -761,13 +762,13 @@
// Where exactly are we writing to?
int out_fd;
if (fd_ >= 0) {
- out_fd = dup(fd_);
+ out_fd = DupCloexec(fd_);
if (out_fd < 0) {
ThrowRuntimeException("Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno));
return false;
}
} else {
- out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
+ out_fd = open(filename_.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (out_fd < 0) {
ThrowRuntimeException("Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(),
strerror(errno));
diff --git a/runtime/monitor_android.cc b/runtime/monitor_android.cc
index 74623da..19e1f3d 100644
--- a/runtime/monitor_android.cc
+++ b/runtime/monitor_android.cc
@@ -43,7 +43,7 @@
// Emit the process name, <= 37 bytes.
{
- int fd = open("/proc/self/cmdline", O_RDONLY);
+ int fd = open("/proc/self/cmdline", O_RDONLY | O_CLOEXEC);
char procName[33];
memset(procName, 0, sizeof(procName));
read(fd, procName, sizeof(procName) - 1);
diff --git a/runtime/native/dalvik_system_VMDebug.cc b/runtime/native/dalvik_system_VMDebug.cc
index 6f98a6d..96f15de 100644
--- a/runtime/native/dalvik_system_VMDebug.cc
+++ b/runtime/native/dalvik_system_VMDebug.cc
@@ -23,6 +23,7 @@
#include "nativehelper/jni_macros.h"
+#include "base/file_utils.h"
#include "base/histogram-inl.h"
#include "base/time_utils.h"
#include "class_linker.h"
@@ -113,7 +114,7 @@
return;
}
- int fd = dup(originalFd);
+ int fd = DupCloexec(originalFd);
if (fd < 0) {
ScopedObjectAccess soa(env);
soa.Self()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
diff --git a/runtime/oat_file_assistant_test.cc b/runtime/oat_file_assistant_test.cc
index 3a974df..aba2eae 100644
--- a/runtime/oat_file_assistant_test.cc
+++ b/runtime/oat_file_assistant_test.cc
@@ -338,9 +338,9 @@
CompilerFilter::kSpeed,
/* with_alternate_image */ false);
- android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY));
- android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY));
- android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
+ android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY | O_CLOEXEC));
+ android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY | O_CLOEXEC));
+ android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
OatFileAssistant oat_file_assistant(dex_location.c_str(),
kRuntimeISA,
@@ -377,8 +377,8 @@
CompilerFilter::kSpeed,
/* with_alternate_image */ false);
- android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY));
- android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
+ android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY | O_CLOEXEC));
+ android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
OatFileAssistant oat_file_assistant(dex_location.c_str(),
kRuntimeISA,
@@ -410,8 +410,8 @@
CompilerFilter::kSpeed,
/* with_alternate_image */ false);
- android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY));
- android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
+ android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY | O_CLOEXEC));
+ android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
OatFileAssistant oat_file_assistant(dex_location.c_str(),
kRuntimeISA,
@@ -436,7 +436,7 @@
Copy(GetDexSrc1(), dex_location);
- android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
+ android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
OatFileAssistant oat_file_assistant(dex_location.c_str(),
kRuntimeISA,
false,