blob: cae10639bf203e6d85ab925bed58ff227e6cb535 [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
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:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070027 static bool Verify(const DexFile* dex_file, const byte* begin, size_t size,
28 const char* location, std::string* error_msg);
29
30 const std::string& FailureReason() const {
31 return failure_reason_;
32 }
jeffhao10037c82012-01-23 15:06:23 -080033
34 private:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070035 DexFileVerifier(const DexFile* dex_file, const byte* begin, size_t size, const char* location)
36 : dex_file_(dex_file), begin_(begin), size_(size), location_(location),
jeffhao10037c82012-01-23 15:06:23 -080037 header_(&dex_file->GetHeader()), ptr_(NULL), previous_item_(NULL) {
38 }
39
40 bool Verify();
41
Ian Rogers8d31bbd2013-10-13 10:44:14 -070042 bool CheckShortyDescriptorMatch(char shorty_char, const char* descriptor, bool is_return_type);
Andreas Gampe50d1bc12014-07-17 21:49:24 -070043 bool CheckListSize(const void* start, size_t count, size_t element_size, const char* label);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070044 bool CheckIndex(uint32_t field, uint32_t limit, const char* label);
jeffhao10037c82012-01-23 15:06:23 -080045
Ian Rogers8d31bbd2013-10-13 10:44:14 -070046 bool CheckHeader();
47 bool CheckMap();
jeffhao10037c82012-01-23 15:06:23 -080048
49 uint32_t ReadUnsignedLittleEndian(uint32_t size);
50 bool CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070051 uint32_t* handler_offsets, uint32_t handlers_size);
52 bool CheckClassDataItemField(uint32_t idx, uint32_t access_flags, bool expect_static);
jeffhao10037c82012-01-23 15:06:23 -080053 bool CheckClassDataItemMethod(uint32_t idx, uint32_t access_flags, uint32_t code_offset,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070054 bool expect_direct);
Ian Rogers8a6bbfc2014-01-23 13:29:07 -080055 bool CheckPadding(size_t offset, uint32_t aligned_offset);
jeffhao10037c82012-01-23 15:06:23 -080056 bool CheckEncodedValue();
57 bool CheckEncodedArray();
58 bool CheckEncodedAnnotation();
59
60 bool CheckIntraClassDataItem();
61 bool CheckIntraCodeItem();
62 bool CheckIntraStringDataItem();
63 bool CheckIntraDebugInfoItem();
64 bool CheckIntraAnnotationItem();
65 bool CheckIntraAnnotationsDirectoryItem();
66
Ian Rogers8a6bbfc2014-01-23 13:29:07 -080067 bool CheckIntraSectionIterate(size_t offset, uint32_t count, uint16_t type);
68 bool CheckIntraIdSection(size_t offset, uint32_t count, uint16_t type);
69 bool CheckIntraDataSection(size_t offset, uint32_t count, uint16_t type);
jeffhao10037c82012-01-23 15:06:23 -080070 bool CheckIntraSection();
71
Ian Rogers8a6bbfc2014-01-23 13:29:07 -080072 bool CheckOffsetToTypeMap(size_t offset, uint16_t type);
Andreas Gampee09269c2014-06-06 18:45:35 -070073
Andreas Gampe5e31dda2014-06-13 11:35:12 -070074 // Note: as sometimes kDexNoIndex16, being 0xFFFF, is a valid return value, we need an
75 // additional out parameter to signal any errors loading an index.
76 uint16_t FindFirstClassDataDefiner(const byte* ptr, bool* success);
77 uint16_t FindFirstAnnotationsDirectoryDefiner(const byte* ptr, bool* success);
jeffhao10037c82012-01-23 15:06:23 -080078
79 bool CheckInterStringIdItem();
80 bool CheckInterTypeIdItem();
81 bool CheckInterProtoIdItem();
82 bool CheckInterFieldIdItem();
83 bool CheckInterMethodIdItem();
84 bool CheckInterClassDefItem();
85 bool CheckInterAnnotationSetRefList();
86 bool CheckInterAnnotationSetItem();
87 bool CheckInterClassDataItem();
88 bool CheckInterAnnotationsDirectoryItem();
89
Ian Rogers8a6bbfc2014-01-23 13:29:07 -080090 bool CheckInterSectionIterate(size_t offset, uint32_t count, uint16_t type);
jeffhao10037c82012-01-23 15:06:23 -080091 bool CheckInterSection();
92
Andreas Gampee09269c2014-06-06 18:45:35 -070093 // Load a string by (type) index. Checks whether the index is in bounds, printing the error if
94 // not. If there is an error, nullptr is returned.
95 const char* CheckLoadStringByIdx(uint32_t idx, const char* error_fmt);
96 const char* CheckLoadStringByTypeIdx(uint32_t type_idx, const char* error_fmt);
97
98 // Load a field/method Id by index. Checks whether the index is in bounds, printing the error if
99 // not. If there is an error, nullptr is returned.
100 const DexFile::FieldId* CheckLoadFieldId(uint32_t idx, const char* error_fmt);
101 const DexFile::MethodId* CheckLoadMethodId(uint32_t idx, const char* error_fmt);
102
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700103 void ErrorStringPrintf(const char* fmt, ...)
104 __attribute__((__format__(__printf__, 2, 3))) COLD_ATTR;
105
106 const DexFile* const dex_file_;
107 const byte* const begin_;
108 const size_t size_;
109 const char* const location_;
110 const DexFile::Header* const header_;
jeffhao10037c82012-01-23 15:06:23 -0800111
Elliott Hughesa0e18062012-04-13 15:59:59 -0700112 SafeMap<uint32_t, uint16_t> offset_to_type_map_;
jeffhao10037c82012-01-23 15:06:23 -0800113 const byte* ptr_;
114 const void* previous_item_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700115
116 std::string failure_reason_;
jeffhao10037c82012-01-23 15:06:23 -0800117};
118
119} // namespace art
120
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700121#endif // ART_RUNTIME_DEX_FILE_VERIFIER_H_