From fbef44de596d298dc6430f482dffc933a046dd28 Mon Sep 17 00:00:00 2001 From: Richard Uhler Date: Tue, 23 Dec 2014 09:48:51 -0800 Subject: Use unique_ptr to track ownership of dex files. Bug: 18809837 Change-Id: Ie571eae8fc19ee9207390cff5c7e2a38071b126a --- runtime/common_runtime_test.cc | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'runtime/common_runtime_test.cc') diff --git a/runtime/common_runtime_test.cc b/runtime/common_runtime_test.cc index 75ba9dd..e017699 100644 --- a/runtime/common_runtime_test.cc +++ b/runtime/common_runtime_test.cc @@ -102,7 +102,11 @@ void ScratchFile::Unlink() { } CommonRuntimeTest::CommonRuntimeTest() {} -CommonRuntimeTest::~CommonRuntimeTest() {} +CommonRuntimeTest::~CommonRuntimeTest() { + // Ensure the dex files are cleaned up before the runtime. + loaded_dex_files_.clear(); + runtime_.reset(); +} void CommonRuntimeTest::SetUpAndroidRoot() { if (IsHost()) { @@ -181,15 +185,15 @@ std::string CommonRuntimeTest::GetCoreOatLocation() { return GetCoreFileLocation("oat"); } -const DexFile* CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) { - std::vector dex_files; +std::unique_ptr CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) { + std::vector> dex_files; std::string error_msg; if (!DexFile::Open(location, location, &error_msg, &dex_files)) { LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n"; - return nullptr; + UNREACHABLE(); } else { CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location; - return dex_files[0]; + return std::move(dex_files[0]); } } @@ -222,6 +226,9 @@ void CommonRuntimeTest::SetUp() { class_linker_ = runtime_->GetClassLinker(); class_linker_->FixupDexCaches(runtime_->GetResolutionMethod()); class_linker_->RunRootClinits(); + boot_class_path_ = class_linker_->GetBootClassPath(); + java_lang_dex_file_ = boot_class_path_[0]; + // Runtime::Create acquired the mutator_lock_ that is normally given away when we // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess. @@ -285,8 +292,6 @@ void CommonRuntimeTest::TearDown() { IcuCleanupFn icu_cleanup_fn = reinterpret_cast(sym); (*icu_cleanup_fn)(); - STLDeleteElements(&opened_dex_files_); - Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption after the test } @@ -323,7 +328,7 @@ std::string CommonRuntimeTest::GetTestAndroidRoot() { #define ART_TARGET_NATIVETEST_DIR_STRING "" #endif -std::vector CommonRuntimeTest::OpenTestDexFiles(const char* name) { +std::vector> CommonRuntimeTest::OpenTestDexFiles(const char* name) { CHECK(name != nullptr); std::string filename; if (IsHost()) { @@ -336,28 +341,30 @@ std::vector CommonRuntimeTest::OpenTestDexFiles(const char* name filename += name; filename += ".jar"; std::string error_msg; - std::vector dex_files; + std::vector> dex_files; bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files); CHECK(success) << "Failed to open '" << filename << "': " << error_msg; - for (const DexFile* dex_file : dex_files) { + for (auto& dex_file : dex_files) { CHECK_EQ(PROT_READ, dex_file->GetPermissions()); CHECK(dex_file->IsReadOnly()); } - opened_dex_files_.insert(opened_dex_files_.end(), dex_files.begin(), dex_files.end()); return dex_files; } -const DexFile* CommonRuntimeTest::OpenTestDexFile(const char* name) { - std::vector vector = OpenTestDexFiles(name); +std::unique_ptr CommonRuntimeTest::OpenTestDexFile(const char* name) { + std::vector> vector = OpenTestDexFiles(name); EXPECT_EQ(1U, vector.size()); - return vector[0]; + return std::move(vector[0]); } jobject CommonRuntimeTest::LoadDex(const char* dex_name) { - std::vector dex_files = OpenTestDexFiles(dex_name); + std::vector> dex_files = OpenTestDexFiles(dex_name); + std::vector class_path; CHECK_NE(0U, dex_files.size()); - for (const DexFile* dex_file : dex_files) { + for (auto& dex_file : dex_files) { + class_path.push_back(dex_file.get()); class_linker_->RegisterDexFile(*dex_file); + loaded_dex_files_.push_back(std::move(dex_file)); } Thread* self = Thread::Current(); JNIEnvExt* env = self->GetJniEnv(); @@ -365,7 +372,7 @@ jobject CommonRuntimeTest::LoadDex(const char* dex_name) { env->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); jobject class_loader = env->NewGlobalRef(class_loader_local.get()); self->SetClassLoaderOverride(class_loader_local.get()); - Runtime::Current()->SetCompileTimeClassPath(class_loader, dex_files); + Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path); return class_loader; } -- cgit v1.1