From 0f6af5e3b51a7f5905d09a98ec8d531541666015 Mon Sep 17 00:00:00 2001 From: Nicolas Geoffray Date: Mon, 30 Jan 2023 14:29:11 +0000 Subject: Reland "Write classes in runtime-generated app image." This reverts commit 24b3d648ff6c2c200003f55ac63fc910d7bfd40f. Bug: 260557058 Reason for revert: - Encode class loader context in image, and check it at load time. - Set nterp entrypoint to methods that can. Test: test.py Test: atest com.android.bluetooth.opp.BluetoothOppObexServerSessionTest#onPut_withUnsupportedMimeTypeInHeader_returnsHttpBadRequest Change-Id: Ibf4a8604c4a226d1acc021103668e211446bb53c --- runtime/class_linker.cc | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'runtime/class_linker.cc') diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc index d5efb685c8..c772a9c29a 100644 --- a/runtime/class_linker.cc +++ b/runtime/class_linker.cc @@ -1400,6 +1400,7 @@ bool ClassLinker::InitFromBootImage(std::string* error_msg) { std::vector> dex_files; if (!AddImageSpace(spaces[i], ScopedNullHandle(), + /* class_loader_context= */ nullptr, /*out*/&dex_files, error_msg)) { return false; @@ -1971,6 +1972,7 @@ static void VerifyAppImage(const ImageHeader& header, bool ClassLinker::AddImageSpace( gc::space::ImageSpace* space, Handle class_loader, + ClassLoaderContext* context, std::vector>* out_dex_files, std::string* error_msg) { DCHECK(out_dex_files != nullptr); @@ -2053,21 +2055,32 @@ bool ClassLinker::AddImageSpace( if (special_root == nullptr) { *error_msg = "Unexpected null special root in app image"; return false; - } else if (special_root->IsIntArray()) { - size_t count = special_root->AsIntArray()->GetLength(); + } else if (special_root->IsObjectArray()) { + ObjPtr checksums = + special_root->AsObjectArray()->Get(1)->AsIntArray(); + size_t count = checksums->GetLength(); if (oat_file->GetVdexFile()->GetNumberOfDexFiles() != count) { *error_msg = "Checksums count does not match"; return false; } static_assert(sizeof(VdexFile::VdexChecksum) == sizeof(int32_t)); const VdexFile::VdexChecksum* art_checksums = - reinterpret_cast(special_root->AsIntArray()->GetData()); + reinterpret_cast(checksums->GetData()); const VdexFile::VdexChecksum* vdex_checksums = oat_file->GetVdexFile()->GetDexChecksumsArray(); if (memcmp(art_checksums, vdex_checksums, sizeof(VdexFile::VdexChecksum) * count) != 0) { *error_msg = "Image and vdex checksums did not match"; return false; } + ObjPtr encoded_context = + special_root->AsObjectArray()->Get(0)->AsString(); + std::string encoded_context_str = encoded_context->ToModifiedUtf8(); + if (context->VerifyClassLoaderContextMatch(encoded_context_str.c_str()) == + ClassLoaderContext::VerificationResult::kMismatch) { + *error_msg = + StringPrintf("Class loader contexts don't match: %s", encoded_context_str.c_str()); + return false; + } } else if (IsBootClassLoader(special_root.Get())) { *error_msg = "Unexpected BootClassLoader in app image"; return false; @@ -2180,9 +2193,11 @@ bool ClassLinker::AddImageSpace( ObjPtr klass(root.Read()); // Do not update class loader for boot image classes where the app image // class loader is only the initiating loader but not the defining loader. - // Avoid read barrier since we are comparing against null. - if (klass->GetClassLoader() != nullptr) { + if (space->HasAddress(klass.Ptr())) { klass->SetClassLoader(loader); + } else { + DCHECK(klass->IsBootStrapClassLoaded()); + DCHECK(Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass.Ptr())); } } } -- cgit v1.2.3-59-g8ed1b