blob: 0af3549eae62cb1120f7d9e4a0b79f4bd63ac395 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_DEX_FILE_VERIFIER_H_
18#define ART_RUNTIME_DEX_FILE_VERIFIER_H_
jeffhao10037c82012-01-23 15:06:23 -080019
Andreas Gampe0ba238d2014-07-29 01:22:07 -070020#include <unordered_set>
21
jeffhao10037c82012-01-23 15:06:23 -080022#include "dex_file.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070023#include "safe_map.h"
jeffhao10037c82012-01-23 15:06:23 -080024
25namespace art {
26
27class DexFileVerifier {
28 public:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070029 static bool Verify(const DexFile* dex_file, const byte* begin, size_t size,
30 const char* location, std::string* error_msg);
31
32 const std::string& FailureReason() const {
33 return failure_reason_;
34 }
jeffhao10037c82012-01-23 15:06:23 -080035
36 private:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070037 DexFileVerifier(const DexFile* dex_file, const byte* begin, size_t size, const char* location)
38 : dex_file_(dex_file), begin_(begin), size_(size), location_(location),
jeffhao10037c82012-01-23 15:06:23 -080039 header_(&dex_file->GetHeader()), ptr_(NULL), previous_item_(NULL) {
40 }
41
42 bool Verify();
43
Ian Rogers8d31bbd2013-10-13 10:44:14 -070044 bool CheckShortyDescriptorMatch(char shorty_char, const char* descriptor, bool is_return_type);
Andreas Gampe50d1bc12014-07-17 21:49:24 -070045 bool CheckListSize(const void* start, size_t count, size_t element_size, const char* label);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070046 bool CheckIndex(uint32_t field, uint32_t limit, const char* label);
jeffhao10037c82012-01-23 15:06:23 -080047
Ian Rogers8d31bbd2013-10-13 10:44:14 -070048 bool CheckHeader();
49 bool CheckMap();
jeffhao10037c82012-01-23 15:06:23 -080050
51 uint32_t ReadUnsignedLittleEndian(uint32_t size);
52 bool CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070053 uint32_t* handler_offsets, uint32_t handlers_size);
54 bool CheckClassDataItemField(uint32_t idx, uint32_t access_flags, bool expect_static);
jeffhao10037c82012-01-23 15:06:23 -080055 bool CheckClassDataItemMethod(uint32_t idx, uint32_t access_flags, uint32_t code_offset,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070056 bool expect_direct);
Ian Rogers8a6bbfc2014-01-23 13:29:07 -080057 bool CheckPadding(size_t offset, uint32_t aligned_offset);
jeffhao10037c82012-01-23 15:06:23 -080058 bool CheckEncodedValue();
59 bool CheckEncodedArray();
60 bool CheckEncodedAnnotation();
61
62 bool CheckIntraClassDataItem();
63 bool CheckIntraCodeItem();
64 bool CheckIntraStringDataItem();
65 bool CheckIntraDebugInfoItem();
66 bool CheckIntraAnnotationItem();
67 bool CheckIntraAnnotationsDirectoryItem();
68
Ian Rogers8a6bbfc2014-01-23 13:29:07 -080069 bool CheckIntraSectionIterate(size_t offset, uint32_t count, uint16_t type);
70 bool CheckIntraIdSection(size_t offset, uint32_t count, uint16_t type);
71 bool CheckIntraDataSection(size_t offset, uint32_t count, uint16_t type);
jeffhao10037c82012-01-23 15:06:23 -080072 bool CheckIntraSection();
73
Ian Rogers8a6bbfc2014-01-23 13:29:07 -080074 bool CheckOffsetToTypeMap(size_t offset, uint16_t type);
Andreas Gampee09269c2014-06-06 18:45:35 -070075
Andreas Gampe5e31dda2014-06-13 11:35:12 -070076 // Note: as sometimes kDexNoIndex16, being 0xFFFF, is a valid return value, we need an
77 // additional out parameter to signal any errors loading an index.
78 uint16_t FindFirstClassDataDefiner(const byte* ptr, bool* success);
79 uint16_t FindFirstAnnotationsDirectoryDefiner(const byte* ptr, bool* success);
jeffhao10037c82012-01-23 15:06:23 -080080
81 bool CheckInterStringIdItem();
82 bool CheckInterTypeIdItem();
83 bool CheckInterProtoIdItem();
84 bool CheckInterFieldIdItem();
85 bool CheckInterMethodIdItem();
86 bool CheckInterClassDefItem();
87 bool CheckInterAnnotationSetRefList();
88 bool CheckInterAnnotationSetItem();
89 bool CheckInterClassDataItem();
90 bool CheckInterAnnotationsDirectoryItem();
91
Ian Rogers8a6bbfc2014-01-23 13:29:07 -080092 bool CheckInterSectionIterate(size_t offset, uint32_t count, uint16_t type);
jeffhao10037c82012-01-23 15:06:23 -080093 bool CheckInterSection();
94
Andreas Gampee09269c2014-06-06 18:45:35 -070095 // Load a string by (type) index. Checks whether the index is in bounds, printing the error if
96 // not. If there is an error, nullptr is returned.
97 const char* CheckLoadStringByIdx(uint32_t idx, const char* error_fmt);
98 const char* CheckLoadStringByTypeIdx(uint32_t type_idx, const char* error_fmt);
99
100 // Load a field/method Id by index. Checks whether the index is in bounds, printing the error if
101 // not. If there is an error, nullptr is returned.
102 const DexFile::FieldId* CheckLoadFieldId(uint32_t idx, const char* error_fmt);
103 const DexFile::MethodId* CheckLoadMethodId(uint32_t idx, const char* error_fmt);
104
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700105 void ErrorStringPrintf(const char* fmt, ...)
106 __attribute__((__format__(__printf__, 2, 3))) COLD_ATTR;
107
108 const DexFile* const dex_file_;
109 const byte* const begin_;
110 const size_t size_;
111 const char* const location_;
112 const DexFile::Header* const header_;
jeffhao10037c82012-01-23 15:06:23 -0800113
Elliott Hughesa0e18062012-04-13 15:59:59 -0700114 SafeMap<uint32_t, uint16_t> offset_to_type_map_;
jeffhao10037c82012-01-23 15:06:23 -0800115 const byte* ptr_;
116 const void* previous_item_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700117
118 std::string failure_reason_;
Andreas Gampe0ba238d2014-07-29 01:22:07 -0700119
120 // Set of type ids for which there are ClassDef elements in the dex file.
121 std::unordered_set<decltype(DexFile::ClassDef::class_idx_)> defined_classes_;
jeffhao10037c82012-01-23 15:06:23 -0800122};
123
124} // namespace art
125
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700126#endif // ART_RUNTIME_DEX_FILE_VERIFIER_H_