blob: e6aa32997c48e7910e37982572080a2f862fa9d8 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
jeffhao10037c82012-01-23 15:06:23 -080016
17#ifndef ART_SRC_DEX_FILE_VERIFIER_H_
18#define ART_SRC_DEX_FILE_VERIFIER_H_
19
20#include <map>
21
22#include "dex_file.h"
23
24namespace art {
25
26class DexFileVerifier {
27 public:
Ian Rogers30fab402012-01-23 15:43:46 -080028 static bool Verify(DexFile* dex_file, const byte* begin, size_t length);
jeffhao10037c82012-01-23 15:06:23 -080029
30 private:
Ian Rogers30fab402012-01-23 15:43:46 -080031 DexFileVerifier(DexFile* dex_file, const byte* begin, size_t length)
32 : dex_file_(dex_file), begin_(begin), length_(length),
jeffhao10037c82012-01-23 15:06:23 -080033 header_(&dex_file->GetHeader()), ptr_(NULL), previous_item_(NULL) {
34 }
35
36 bool Verify();
37
38 bool CheckPointerRange(const void* start, const void* end, const char* label) const;
39 bool CheckListSize(const void* start, uint32_t count, uint32_t element_size, const char* label) const;
40 bool CheckIndex(uint32_t field, uint32_t limit, const char* label) const;
41
42 bool CheckHeader() const;
43 bool CheckMap() const;
44
45 uint32_t ReadUnsignedLittleEndian(uint32_t size);
46 bool CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item,
47 uint32_t* handler_offsets, uint32_t handlers_size);
48 bool CheckClassDataItemField(uint32_t idx, uint32_t access_flags, bool expect_static) const;
49 bool CheckClassDataItemMethod(uint32_t idx, uint32_t access_flags, uint32_t code_offset,
50 bool expect_direct) const;
51 bool CheckPadding(uint32_t offset, uint32_t aligned_offset);
52 bool CheckEncodedValue();
53 bool CheckEncodedArray();
54 bool CheckEncodedAnnotation();
55
56 bool CheckIntraClassDataItem();
57 bool CheckIntraCodeItem();
58 bool CheckIntraStringDataItem();
59 bool CheckIntraDebugInfoItem();
60 bool CheckIntraAnnotationItem();
61 bool CheckIntraAnnotationsDirectoryItem();
62
63 bool CheckIntraSectionIterate(uint32_t offset, uint32_t count, uint16_t type);
64 bool CheckIntraIdSection(uint32_t offset, uint32_t count, uint16_t type);
65 bool CheckIntraDataSection(uint32_t offset, uint32_t count, uint16_t type);
66 bool CheckIntraSection();
67
68 bool CheckOffsetToTypeMap(uint32_t offset, uint16_t type);
69 uint16_t FindFirstClassDataDefiner(const byte* ptr) const;
70 uint16_t FindFirstAnnotationsDirectoryDefiner(const byte* ptr) const;
71
72 bool CheckInterStringIdItem();
73 bool CheckInterTypeIdItem();
74 bool CheckInterProtoIdItem();
75 bool CheckInterFieldIdItem();
76 bool CheckInterMethodIdItem();
77 bool CheckInterClassDefItem();
78 bool CheckInterAnnotationSetRefList();
79 bool CheckInterAnnotationSetItem();
80 bool CheckInterClassDataItem();
81 bool CheckInterAnnotationsDirectoryItem();
82
83 bool CheckInterSectionIterate(uint32_t offset, uint32_t count, uint16_t type);
84 bool CheckInterSection();
85
86 DexFile* dex_file_;
Ian Rogers30fab402012-01-23 15:43:46 -080087 const byte* begin_;
jeffhao10037c82012-01-23 15:06:23 -080088 size_t length_;
89 const DexFile::Header* header_;
90
91 std::map<uint32_t, uint16_t> offset_to_type_map_;
92 const byte* ptr_;
93 const void* previous_item_;
94};
95
96} // namespace art
97
98#endif // ART_SRC_DEX_FILE_VERIFIER_H_