1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_LIBDEXFILE_DEX_STANDARD_DEX_FILE_H_
18 #define ART_LIBDEXFILE_DEX_STANDARD_DEX_FILE_H_
19 
20 #include <iosfwd>
21 
22 #include "dex_file.h"
23 
24 namespace art {
25 
26 class OatDexFile;
27 
28 // Standard dex file. This is the format that is packaged in APKs and produced by tools.
29 class StandardDexFile : public DexFile {
30  public:
31   class Header : public DexFile::Header {
32     // Same for now.
33   };
34 
35   struct CodeItem : public DexFile::CodeItem {
36     static constexpr size_t kAlignment = 4;
37 
38    private:
39     CodeItem() = default;
40 
41     uint16_t registers_size_;            // the number of registers used by this code
42                                          //   (locals + parameters)
43     uint16_t ins_size_;                  // the number of words of incoming arguments to the method
44                                          //   that this code is for
45     uint16_t outs_size_;                 // the number of words of outgoing argument space required
46                                          //   by this code for method invocation
47     uint16_t tries_size_;                // the number of try_items for this instance. If non-zero,
48                                          //   then these appear as the tries array just after the
49                                          //   insns in this instance.
50     uint32_t debug_info_off_;            // Holds file offset to debug info stream.
51 
52     uint32_t insns_size_in_code_units_;  // size of the insns array, in 2 byte code units
53     uint16_t insns_[1];                  // actual array of bytecode.
54 
55     ART_FRIEND_TEST(CodeItemAccessorsTest, TestDexInstructionsAccessor);
56     friend class CodeItemDataAccessor;
57     friend class CodeItemDebugInfoAccessor;
58     friend class CodeItemInstructionAccessor;
59     friend class DexWriter;
60     friend class StandardDexFile;
61     DISALLOW_COPY_AND_ASSIGN(CodeItem);
62   };
63 
64   // Write the standard dex specific magic.
65   static void WriteMagic(uint8_t* magic);
66 
67   // Write the current version, note that the input is the address of the magic.
68   static void WriteCurrentVersion(uint8_t* magic);
69 
70   static const uint8_t kDexMagic[kDexMagicSize];
71   static constexpr size_t kNumDexVersions = 4;
72   static const uint8_t kDexMagicVersions[kNumDexVersions][kDexVersionLen];
73 
74   // Returns true if the byte string points to the magic value.
75   static bool IsMagicValid(const uint8_t* magic);
76   virtual bool IsMagicValid() const OVERRIDE;
77 
78   // Returns true if the byte string after the magic is the correct value.
79   static bool IsVersionValid(const uint8_t* magic);
80   virtual bool IsVersionValid() const OVERRIDE;
81 
82   virtual bool SupportsDefaultMethods() const OVERRIDE;
83 
84   uint32_t GetCodeItemSize(const DexFile::CodeItem& item) const OVERRIDE;
85 
GetDequickenedSize()86   virtual size_t GetDequickenedSize() const OVERRIDE {
87     return Size();
88   }
89 
90  private:
StandardDexFile(const uint8_t * base,size_t size,const std::string & location,uint32_t location_checksum,const OatDexFile * oat_dex_file,std::unique_ptr<DexFileContainer> container)91   StandardDexFile(const uint8_t* base,
92                   size_t size,
93                   const std::string& location,
94                   uint32_t location_checksum,
95                   const OatDexFile* oat_dex_file,
96                   std::unique_ptr<DexFileContainer> container)
97       : DexFile(base,
98                 size,
99                 /*data_begin*/ base,
100                 /*data_size*/ size,
101                 location,
102                 location_checksum,
103                 oat_dex_file,
104                 std::move(container),
105                 /*is_compact_dex*/ false) {}
106 
107   friend class DexFileLoader;
108   friend class DexFileVerifierTest;
109 
110   ART_FRIEND_TEST(ClassLinkerTest, RegisterDexFileName);  // for constructor
111   friend class OptimizingUnitTestHelper;  // for constructor
112 
113   DISALLOW_COPY_AND_ASSIGN(StandardDexFile);
114 };
115 
116 }  // namespace art
117 
118 #endif  // ART_LIBDEXFILE_DEX_STANDARD_DEX_FILE_H_
119