Add DexFileContainer for all DexFiles.

Ensure that all DexFiles have a container for consistency.

Enforce that the container shall contain exactly one dex file,
as specified the by file_size entry in the de header
(that is, the memory range should not be truncated or too big).

Bug: 266950186
Test: test.py -b --host --optimizing --64
Change-Id: I73ff074a980f9a9d5a0f4a9b51ac1a496971374f
diff --git a/libdexfile/external/dex_file_ext.cc b/libdexfile/external/dex_file_ext.cc
index c7b1cbb..fc2bb85 100644
--- a/libdexfile/external/dex_file_ext.cc
+++ b/libdexfile/external/dex_file_ext.cc
@@ -154,7 +154,8 @@
   }
 
   const art::DexFile::Header* header = reinterpret_cast<const art::DexFile::Header*>(address);
-  uint32_t file_size = header->file_size_;
+  uint32_t dex_size = header->file_size_;  // Size of "one dex file" excluding any shared data.
+  uint32_t full_size = dex_size;           // Includes referenced shared data past the end of dex.
   if (art::CompactDexFile::IsMagicValid(header->magic_)) {
     // Compact dex files store the data section separately so that it can be shared.
     // Therefore we need to extend the read memory range to include it.
@@ -164,23 +165,23 @@
     if (__builtin_add_overflow(header->data_off_, header->data_size_, &computed_file_size)) {
       return ADEXFILE_ERROR_INVALID_HEADER;
     }
-    if (computed_file_size > file_size) {
-      file_size = computed_file_size;
+    if (computed_file_size > full_size) {
+      full_size = computed_file_size;
     }
   } else if (!art::StandardDexFile::IsMagicValid(header->magic_)) {
     return ADEXFILE_ERROR_INVALID_HEADER;
   }
 
-  if (size < file_size) {
+  if (size < full_size) {
     if (new_size != nullptr) {
-      *new_size = file_size;
+      *new_size = full_size;
     }
     return ADEXFILE_ERROR_NOT_ENOUGH_DATA;
   }
 
   std::string loc_str(location);
   std::string error_msg;
-  art::DexFileLoader loader(static_cast<const uint8_t*>(address), size, loc_str);
+  art::DexFileLoader loader(static_cast<const uint8_t*>(address), dex_size, loc_str);
   std::unique_ptr<const art::DexFile> dex_file = loader.Open(header->checksum_,
                                                              /*oat_dex_file=*/nullptr,
                                                              /*verify=*/false,