2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Functions for dealing with try-catch info.
21 #ifndef _LIBDEX_DEXCATCH
22 #define _LIBDEX_DEXCATCH
28 * Catch handler entry, used while iterating over catch_handler_items.
30 typedef struct DexCatchHandler {
31 u4 typeIdx; /* type index of the caught exception type */
32 u4 address; /* handler address */
35 /* Get the first handler offset for the given DexCode.
36 * It's not 0 because the handlers list is prefixed with its size
37 * (in entries) as a uleb128. */
38 u4 dexGetFirstHandlerOffset(const DexCode* pCode);
40 /* Get count of handler lists for the given DexCode. */
41 u4 dexGetHandlersSize(const DexCode* pCode);
44 * Iterator over catch handler data. This structure should be treated as
47 typedef struct DexCatchIterator {
48 const u1* pEncodedData;
51 DexCatchHandler handler;
54 /* Initialize a DexCatchIterator to emptiness. This mostly exists to
55 * squelch innocuous warnings. */
56 DEX_INLINE void dexCatchIteratorClear(DexCatchIterator* pIterator) {
57 pIterator->pEncodedData = NULL;
58 pIterator->catchesAll = false;
59 pIterator->countRemaining = 0;
60 pIterator->handler.typeIdx = 0;
61 pIterator->handler.address = 0;
64 /* Initialize a DexCatchIterator with a direct pointer to encoded handlers. */
65 DEX_INLINE void dexCatchIteratorInitToPointer(DexCatchIterator* pIterator,
66 const u1* pEncodedData)
68 s4 count = readSignedLeb128(&pEncodedData);
71 pIterator->catchesAll = true;
74 pIterator->catchesAll = false;
77 pIterator->pEncodedData = pEncodedData;
78 pIterator->countRemaining = count;
81 /* Initialize a DexCatchIterator to a particular handler offset. */
82 DEX_INLINE void dexCatchIteratorInit(DexCatchIterator* pIterator,
83 const DexCode* pCode, u4 offset)
85 dexCatchIteratorInitToPointer(pIterator,
86 dexGetCatchHandlerData(pCode) + offset);
89 /* Get the next item from a DexCatchIterator. Returns NULL if at end. */
90 DEX_INLINE DexCatchHandler* dexCatchIteratorNext(DexCatchIterator* pIterator) {
91 if (pIterator->countRemaining == 0) {
92 if (! pIterator->catchesAll) {
96 pIterator->catchesAll = false;
97 pIterator->handler.typeIdx = kDexNoIndex;
99 u4 typeIdx = readUnsignedLeb128(&pIterator->pEncodedData);
100 pIterator->handler.typeIdx = typeIdx;
101 pIterator->countRemaining--;
104 pIterator->handler.address = readUnsignedLeb128(&pIterator->pEncodedData);
105 return &pIterator->handler;
108 /* Get the handler offset just past the end of the one just iterated over.
109 * This ends the iteration if it wasn't already. */
110 u4 dexCatchIteratorGetEndOffset(DexCatchIterator* pIterator,
111 const DexCode* pCode);
113 /* Helper for dexFindCatchHandler(). Do not call directly. */
114 int dexFindCatchHandlerOffset0(u2 triesSize, const DexTry* pTries,
117 /* Find the handler associated with a given address, if any.
118 * Initializes the given iterator and returns true if a match is
119 * found. Returns false if there is no applicable handler. */
120 DEX_INLINE bool dexFindCatchHandler(DexCatchIterator *pIterator,
121 const DexCode* pCode, u4 address) {
122 u2 triesSize = pCode->triesSize;
125 // Short-circuit the overwhelmingly common cases.
131 const DexTry* tries = dexGetTries(pCode);
132 u4 start = tries[0].startAddr;
134 if (address < start) {
138 u4 end = start + tries[0].insnCount;
140 if (address >= end) {
144 offset = tries[0].handlerOff;
148 offset = dexFindCatchHandlerOffset0(triesSize, dexGetTries(pCode),
154 dexCatchIteratorClear(pIterator); // This squelches warnings.
157 dexCatchIteratorInit(pIterator, pCode, offset);