1/*
2 * Copyright (C) 2011 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_RUNTIME_DEX_FILE_VERIFIER_H_
18#define ART_RUNTIME_DEX_FILE_VERIFIER_H_
19
20#include "dex_file.h"
21#include "safe_map.h"
22
23namespace art {
24
25class DexFileVerifier {
26 public:
27  static bool Verify(const DexFile* dex_file, const byte* begin, size_t size);
28
29 private:
30  DexFileVerifier(const DexFile* dex_file, const byte* begin, size_t size)
31      : dex_file_(dex_file), begin_(begin), size_(size),
32        header_(&dex_file->GetHeader()), ptr_(NULL), previous_item_(NULL)  {
33  }
34
35  bool Verify();
36
37  bool CheckPointerRange(const void* start, const void* end, const char* label) const;
38  bool CheckListSize(const void* start, uint32_t count, uint32_t element_size, const char* label) const;
39  bool CheckIndex(uint32_t field, uint32_t limit, const char* label) const;
40
41  bool CheckHeader() const;
42  bool CheckMap() const;
43
44  uint32_t ReadUnsignedLittleEndian(uint32_t size);
45  bool CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item,
46      uint32_t* handler_offsets, uint32_t handlers_size);
47  bool CheckClassDataItemField(uint32_t idx, uint32_t access_flags, bool expect_static) const;
48  bool CheckClassDataItemMethod(uint32_t idx, uint32_t access_flags, uint32_t code_offset,
49      bool expect_direct) const;
50  bool CheckPadding(uint32_t offset, uint32_t aligned_offset);
51  bool CheckEncodedValue();
52  bool CheckEncodedArray();
53  bool CheckEncodedAnnotation();
54
55  bool CheckIntraClassDataItem();
56  bool CheckIntraCodeItem();
57  bool CheckIntraStringDataItem();
58  bool CheckIntraDebugInfoItem();
59  bool CheckIntraAnnotationItem();
60  bool CheckIntraAnnotationsDirectoryItem();
61
62  bool CheckIntraSectionIterate(uint32_t offset, uint32_t count, uint16_t type);
63  bool CheckIntraIdSection(uint32_t offset, uint32_t count, uint16_t type);
64  bool CheckIntraDataSection(uint32_t offset, uint32_t count, uint16_t type);
65  bool CheckIntraSection();
66
67  bool CheckOffsetToTypeMap(uint32_t offset, uint16_t type);
68  uint16_t FindFirstClassDataDefiner(const byte* ptr) const;
69  uint16_t FindFirstAnnotationsDirectoryDefiner(const byte* ptr) const;
70
71  bool CheckInterStringIdItem();
72  bool CheckInterTypeIdItem();
73  bool CheckInterProtoIdItem();
74  bool CheckInterFieldIdItem();
75  bool CheckInterMethodIdItem();
76  bool CheckInterClassDefItem();
77  bool CheckInterAnnotationSetRefList();
78  bool CheckInterAnnotationSetItem();
79  bool CheckInterClassDataItem();
80  bool CheckInterAnnotationsDirectoryItem();
81
82  bool CheckInterSectionIterate(uint32_t offset, uint32_t count, uint16_t type);
83  bool CheckInterSection();
84
85  const DexFile* dex_file_;
86  const byte* begin_;
87  size_t size_;
88  const DexFile::Header* header_;
89
90  SafeMap<uint32_t, uint16_t> offset_to_type_map_;
91  const byte* ptr_;
92  const void* previous_item_;
93};
94
95}  // namespace art
96
97#endif  // ART_RUNTIME_DEX_FILE_VERIFIER_H_
98