Set the location checksum in vdex.

The checksum we check against at runtime isn't the dex checksum
if we are compiling a jar/apk, but the crc32 of the entry.

This is to optimize the check at startup and avoid reading the
contents of the zip file.

Because with vdex we do not want to open the zip file, (the test
has been done already in getDexOptNeeded), and cannot rely
on the .oat file for fetching it, put the location checksum in
the vdex directly.

Note for later refactorings: the dex checksum location is now
in the vdex file, but also in the oat file. This can be revisited
after we eventually cleanup OatDexFile.

Test: 629-vdex
bug: 30937355
Change-Id: I7af8ca63b889370c660d694dd4eb95e78f566a1c
diff --git a/runtime/vdex_file.h b/runtime/vdex_file.h
index 75a0d5e..3b114a9 100644
--- a/runtime/vdex_file.h
+++ b/runtime/vdex_file.h
@@ -43,7 +43,10 @@
  public:
   struct Header {
    public:
-    Header(uint32_t dex_size, uint32_t verifier_deps_size, uint32_t quickening_info_size);
+    Header(uint32_t number_of_dex_files_,
+           uint32_t dex_size,
+           uint32_t verifier_deps_size,
+           uint32_t quickening_info_size);
 
     const char* GetMagic() const { return reinterpret_cast<const char*>(magic_); }
     const char* GetVersion() const { return reinterpret_cast<const char*>(version_); }
@@ -54,18 +57,22 @@
     uint32_t GetDexSize() const { return dex_size_; }
     uint32_t GetVerifierDepsSize() const { return verifier_deps_size_; }
     uint32_t GetQuickeningInfoSize() const { return quickening_info_size_; }
+    uint32_t GetNumberOfDexFiles() const { return number_of_dex_files_; }
 
    private:
     static constexpr uint8_t kVdexMagic[] = { 'v', 'd', 'e', 'x' };
-    static constexpr uint8_t kVdexVersion[] = { '0', '0', '0', '\0' };
+    static constexpr uint8_t kVdexVersion[] = { '0', '0', '1', '\0' };
 
     uint8_t magic_[4];
     uint8_t version_[4];
+    uint32_t number_of_dex_files_;
     uint32_t dex_size_;
     uint32_t verifier_deps_size_;
     uint32_t quickening_info_size_;
   };
 
+  typedef uint32_t VdexChecksum;
+
   static VdexFile* Open(const std::string& vdex_filename,
                         bool writable,
                         bool low_4gb,
@@ -88,7 +95,7 @@
 
   ArrayRef<const uint8_t> GetVerifierDepsData() const {
     return ArrayRef<const uint8_t>(
-        Begin() + sizeof(Header) + GetHeader().GetDexSize(), GetHeader().GetVerifierDepsSize());
+        DexBegin() + GetHeader().GetDexSize(), GetHeader().GetVerifierDepsSize());
   }
 
   ArrayRef<const uint8_t> GetQuickeningInfo() const {
@@ -107,6 +114,12 @@
   // is none.
   const uint8_t* GetNextDexFileData(const uint8_t* cursor) const;
 
+  // Get the location checksum of the dex file number `dex_file_index`.
+  uint32_t GetLocationChecksum(uint32_t dex_file_index) const {
+    DCHECK_LT(dex_file_index, GetHeader().GetNumberOfDexFiles());
+    return reinterpret_cast<const uint32_t*>(Begin() + sizeof(Header))[dex_file_index];
+  }
+
  private:
   explicit VdexFile(MemMap* mmap) : mmap_(mmap) {}
 
@@ -115,11 +128,15 @@
   }
 
   const uint8_t* DexBegin() const {
-    return Begin() + sizeof(Header);
+    return Begin() + sizeof(Header) + GetSizeOfChecksumsSection();
   }
 
   const uint8_t* DexEnd() const {
-    return Begin() + sizeof(Header) + GetHeader().GetDexSize();
+    return DexBegin() + GetHeader().GetDexSize();
+  }
+
+  size_t GetSizeOfChecksumsSection() const {
+    return sizeof(VdexChecksum) * GetHeader().GetNumberOfDexFiles();
   }
 
   std::unique_ptr<MemMap> mmap_;