blob: 5538d4aa75e125a6c94cb438ee3b94aeb2f2cc06 [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
jeffhao10037c82012-01-23 15:06:23 -080020#include "dex_file.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070021#include "safe_map.h"
jeffhao10037c82012-01-23 15:06:23 -080022
23namespace art {
24
25class DexFileVerifier {
26 public:
jeffhaof6174e82012-01-31 16:14:17 -080027 static bool Verify(const DexFile* dex_file, const byte* begin, size_t size);
jeffhao10037c82012-01-23 15:06:23 -080028
29 private:
jeffhaof6174e82012-01-31 16:14:17 -080030 DexFileVerifier(const DexFile* dex_file, const byte* begin, size_t size)
31 : dex_file_(dex_file), begin_(begin), size_(size),
jeffhao10037c82012-01-23 15:06:23 -080032 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
jeffhaof6174e82012-01-31 16:14:17 -080085 const DexFile* dex_file_;
Ian Rogers30fab402012-01-23 15:43:46 -080086 const byte* begin_;
jeffhaof6174e82012-01-31 16:14:17 -080087 size_t size_;
jeffhao10037c82012-01-23 15:06:23 -080088 const DexFile::Header* header_;
89
Elliott Hughesa0e18062012-04-13 15:59:59 -070090 SafeMap<uint32_t, uint16_t> offset_to_type_map_;
jeffhao10037c82012-01-23 15:06:23 -080091 const byte* ptr_;
92 const void* previous_item_;
93};
94
95} // namespace art
96
97#endif // ART_SRC_DEX_FILE_VERIFIER_H_