1 /*
2  * Copyright (C) 2017 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_LIBDEXFILE_DEX_DEX_FILE_EXCEPTION_HELPERS_H_
18 #define ART_LIBDEXFILE_DEX_DEX_FILE_EXCEPTION_HELPERS_H_
19 
20 #include <android-base/logging.h>
21 
22 #include "dex_file_types.h"
23 
24 namespace art {
25 
26 namespace dex {
27 struct TryItem;
28 }  // namespace dex
29 
30 class CodeItemDataAccessor;
31 
32 class CatchHandlerIterator {
33  public:
34   CatchHandlerIterator(const CodeItemDataAccessor& accessor, uint32_t address);
35 
36   CatchHandlerIterator(const CodeItemDataAccessor& accessor, const dex::TryItem& try_item);
37 
CatchHandlerIterator(const uint8_t * handler_data)38   explicit CatchHandlerIterator(const uint8_t* handler_data) {
39     Init(handler_data);
40   }
41 
GetHandlerTypeIndex()42   dex::TypeIndex GetHandlerTypeIndex() const {
43     return handler_.type_idx_;
44   }
GetHandlerAddress()45   uint32_t GetHandlerAddress() const {
46     return handler_.address_;
47   }
48   void Next();
HasNext()49   bool HasNext() const {
50     return remaining_count_ != -1 || catch_all_;
51   }
52   // End of this set of catch blocks, convenience method to locate next set of catch blocks
EndDataPointer()53   const uint8_t* EndDataPointer() const {
54     CHECK(!HasNext());
55     return current_data_;
56   }
57 
58  private:
59   void Init(const CodeItemDataAccessor& accessor, int32_t offset);
60   void Init(const uint8_t* handler_data);
61 
62   struct CatchHandlerItem {
63     dex::TypeIndex type_idx_;  // type index of the caught exception type
64     uint32_t address_;  // handler address
65   } handler_;
66   const uint8_t* current_data_;  // the current handler in dex file.
67   int32_t remaining_count_;   // number of handlers not read.
68   bool catch_all_;            // is there a handler that will catch all exceptions in case
69                               // that all typed handler does not match.
70 };
71 
72 }  // namespace art
73 
74 #endif  // ART_LIBDEXFILE_DEX_DEX_FILE_EXCEPTION_HELPERS_H_
75