Home | History | Annotate | Download | only in libdex

Lines Matching full:state

92 static inline u4 fileOffset(const CheckState* state, const void* ptr) {
93 return ((const u1*) ptr) - state->fileStart;
99 static inline void* filePointer(const CheckState* state, u4 offset) {
100 return (void*) (state->fileStart + offset);
111 static inline bool checkPtrRange(const CheckState* state,
113 const void* fileStart = state->fileStart;
114 const void* fileEnd = state->fileEnd;
118 fileOffset(state, start), fileOffset(state, end));
131 * Assumes "const CheckState* state".
134 const u1* _startPtr = (const u1*) filePointer(state, (_start)); \
135 const u1* _endPtr = (const u1*) filePointer(state, (_end)); \
136 if (!checkPtrRange(state, _startPtr, _endPtr, \
150 * Assumes "const CheckState* state".
153 if (!checkPtrRange(state, (_start), (_end), #_start ".." #_end)) { \
161 * Assumes "const CheckState* state" and "typeof(_count) == typeof(_elemSize)"
168 !checkPtrRange(state, _start, _end, #_ptr)) { \
179 * Assumes "const CheckState* state".
233 static bool verifyFieldDefiner(const CheckState* state, u4 definingClass,
235 const DexFieldId* field = dexGetFieldId(state->pDexFile, fieldIdx);
240 static bool verifyMethodDefiner(const CheckState* state, u4 definingClass,
242 const DexMethodId* meth = dexGetMethodId(state->pDexFile, methodIdx);
250 static size_t calcDefinedClassBitsSize(const CheckState* state)
253 return (state->pHeader->typeIdsSize + 0x1f) >> 5;
259 static bool setDefinedClassBit(const CheckState* state, u4 typeIdx) {
262 u4* element = &state->pDefinedClassBits[arrayIdx];
273 static bool swapDexHeader(const CheckState* state, DexHeader* pHeader)
323 static bool checkHeaderSection(const CheckState* state, u4 sectionOffset,
335 const DexHeader* pHeader = (const DexHeader*) filePointer(state, 0);
393 * passes, allocate the state's DexDataMap.
395 static bool swapMap(CheckState* state, DexMapList* pMap)
400 u4 dataItemsLeft = state->pHeader->dataSize; // See use below.
424 if (item->offset >= state->pHeader->fileSize) {
426 item->offset, state->pHeader->fileSize);
475 && ((state->pHeader->stringIdsOff != 0)
476 || (state->pHeader->stringIdsSize != 0))) {
482 && ((state->pHeader->typeIdsOff != 0)
483 || (state->pHeader->typeIdsSize != 0))) {
489 && ((state->pHeader->protoIdsOff != 0)
490 || (state->pHeader->protoIdsSize != 0))) {
496 && ((state->pHeader->fieldIdsOff != 0)
497 || (state->pHeader->fieldIdsSize != 0))) {
503 && ((state->pHeader->methodIdsOff != 0)
504 || (state->pHeader->methodIdsSize != 0))) {
510 && ((state->pHeader->classDefsOff != 0)
511 || (state->pHeader->classDefsSize != 0))) {
516 state->pDataMap = dexDataMapAlloc(dataItemCount);
517 if (state->pDataMap == NULL) {
526 static bool checkMapSection(const CheckState* state, u4 sectionOffset,
533 if (sectionOffset != state->pHeader->mapOff) {
535 sectionOffset, state->pHeader->mapOff);
539 const DexMapList* pMap = (const DexMapList*) filePointer(state, sectionOffset);
547 static void* swapStringIdItem(const CheckState* state, void* ptr) {
557 static void* crossVerifyStringIdItem(const CheckState* state, void* ptr) {
560 if (!dexDataMapVerify(state->pDataMap,
565 const DexStringId* item0 = (const DexStringId*) state->previousItem;
568 const char* s0 = dexGetStringData(state->pDexFile, item0);
569 const char* s1 = dexGetStringData(state->pDexFile, item);
580 static void* swapTypeIdItem(const CheckState* state, void* ptr) {
584 SWAP_INDEX4(item->descriptorIdx, state->pHeader->stringIdsSize);
590 static void* crossVerifyTypeIdItem(const CheckState* state, void* ptr) {
593 dexStringById(state->pDexFile, item->descriptorIdx);
600 const DexTypeId* item0 = (const DexTypeId*) state->previousItem;
614 static void* swapProtoIdItem(const CheckState* state, void* ptr) {
618 SWAP_INDEX4(item->shortyIdx, state->pHeader->stringIdsSize);
619 SWAP_INDEX4(item->returnTypeIdx, state->pHeader->typeIdsSize);
671 static void* crossVerifyProtoIdItem(const CheckState* state, void* ptr) {
674 dexStringById(state->pDexFile, item->shortyIdx);
676 if (!dexDataMapVerify0Ok(state->pDataMap,
682 dexStringByTypeIdx(state->pDexFile, item->returnTypeIdx),
687 u4 protoIdx = item - state->pDexFile->pProtoIds;
688 DexProto proto = { state->pDexFile, protoIdx };
718 const DexProtoId* item0 = (const DexProtoId*) state->previousItem;
726 DexProto proto0 = { state->pDexFile, protoIdx - 1 };
764 static void* swapFieldIdItem(const CheckState* state, void* ptr) {
768 SWAP_INDEX2(item->classIdx, state->pHeader->typeIdsSize);
769 SWAP_INDEX2(item->typeIdx, state->pHeader->typeIdsSize);
770 SWAP_INDEX4(item->nameIdx, state->pHeader->stringIdsSize);
776 static void* crossVerifyFieldIdItem(const CheckState* state, void* ptr) {
780 s = dexStringByTypeIdx(state->pDexFile, item->classIdx);
786 s = dexStringByTypeIdx(state->pDexFile, item->typeIdx);
792 s = dexStringById(state->pDexFile, item->nameIdx);
798 const DexFieldId* item0 = (const DexFieldId*) state->previousItem;
836 static void* swapMethodIdItem(const CheckState* state, void* ptr) {
840 SWAP_INDEX2(item->classIdx, state->pHeader->typeIdsSize);
841 SWAP_INDEX2(item->protoIdx, state->pHeader->protoIdsSize);
842 SWAP_INDEX4(item->nameIdx, state->pHeader->stringIdsSize);
848 static void* crossVerifyMethodIdItem(const CheckState* state, void* ptr) {
852 s = dexStringByTypeIdx(state->pDexFile, item->classIdx);
858 s = dexStringById(state->pDexFile, item->nameIdx);
864 const DexMethodId* item0 = (const DexMethodId*) state->previousItem;
902 static void* swapClassDefItem(const CheckState* state, void* ptr) {
906 SWAP_INDEX4(item->classIdx, state->pHeader->typeIdsSize);
908 SWAP_INDEX4_OR_NOINDEX(item->superclassIdx, state->pHeader->typeIdsSize);
910 SWAP_INDEX4_OR_NOINDEX(item->sourceFileIdx, state->pHeader->stringIdsSize);
918 static u4 findFirstClassDataDefiner(const CheckState* state,
920 static u4 findFirstAnnotationsDirectoryDefiner(const CheckState* state,
925 static bool verifyClassDataIsForDef(const CheckState* state, u4 offset,
931 const u1* data = (const u1*) filePointer(state, offset);
944 u4 dataDefiner = findFirstClassDataDefiner(state, classData);
954 static bool verifyAnnotationsDirectoryIsForDef(const CheckState* state,
961 (const DexAnnotationsDirectoryItem*) filePointer(state, offset);
962 u4 annoDefiner = findFirstAnnotationsDirectoryDefiner(state, dir);
968 static void* crossVerifyClassDefItem(const CheckState* state, void* ptr) {
971 const char* descriptor = dexStringByTypeIdx(state->pDexFile, classIdx);
978 if (setDefinedClassBit(state, classIdx)) {
984 dexDataMapVerify0Ok(state->pDataMap,
986 && dexDataMapVerify0Ok(state->pDataMap,
988 && dexDataMapVerify0Ok(state->pDataMap,
990 && dexDataMapVerify0Ok(state
998 descriptor = dexStringByTypeIdx(state->pDexFile, item->superclassIdx);
1006 dexGetInterfacesList(state->pDexFile, item);
1016 descriptor = dexStringByTypeIdx(state->pDexFile,
1038 dexStringByTypeIdx(state->pDexFile, idx1));
1045 if (!verifyClassDataIsForDef(state, item->classDataOff, item->classIdx)) {
1050 if (!verifyAnnotationsDirectoryIsForDef(state, item->annotationsOff,
1062 static u1* swapFieldAnnotations(const CheckState* state, u4 count, u1* addr) {
1070 SWAP_INDEX4(item->fieldIdx, state->pHeader->fieldIdsSize);
1091 static u1* swapMethodAnnotations(const CheckState* state, u4 count, u1* addr) {
1099 SWAP_INDEX4(item->methodIdx, state->pHeader->methodIdsSize);
1120 static u1* swapParameterAnnotations(const CheckState* state, u4 count,
1129 SWAP_INDEX4(item->methodIdx, state->pHeader->methodIdsSize);
1149 static void* swapAnnotationsDirectoryItem(const CheckState* state, void* ptr) {
1161 addr = swapFieldAnnotations(state, item->fieldsSize, addr);
1168 addr = swapMethodAnnotations(state, item->methodsSize, addr);
1175 addr = swapParameterAnnotations(state, item->parametersSize, addr);
1186 static const u1* crossVerifyFieldAnnotations(const CheckState* state, u4 count,
1191 if (!verifyFieldDefiner(state, definingClass, item->fieldIdx)) {
1194 if (!dexDataMapVerify(state->pDataMap, item->annotationsOff,
1206 static const u1* crossVerifyMethodAnnotations(const CheckState* state,
1211 if (!verifyMethodDefiner(state, definingClass, item->methodIdx)) {
1214 if (!dexDataMapVerify(state->pDataMap, item->annotationsOff,
1226 static const u1* crossVerifyParameterAnnotations(const CheckState* state,
1232 if (!verifyMethodDefiner(state, definingClass, item->methodIdx)) {
1235 if (!dexDataMapVerify(state->pDataMap, item->annotationsOff,
1248 static u4 findFirstAnnotationsDirectoryDefiner(const CheckState* state,
1252 dexGetFieldAnnotations(state->pDexFile, dir);
1254 dexGetFieldId(state->pDexFile, fields[0].fieldIdx);
1260 dexGetMethodAnnotations(state->pDexFile, dir);
1262 dexGetMethodId(state->pDexFile, methods[0].methodIdx);
1268 dexGetParameterAnnotations(state->pDexFile, dir);
1270 dexGetMethodId(state->pDexFile, parameters[0].methodIdx);
1278 static void* crossVerifyAnnotationsDirectoryItem(const CheckState* state,
1281 u4 definingClass = findFirstAnnotationsDirectoryDefiner(state, item);
1283 if (!dexDataMapVerify0Ok(state->pDataMap,
1291 addr = crossVerifyFieldAnnotations(state, item->fieldsSize, addr,
1299 addr = crossVerifyMethodAnnotations(state, item->methodsSize, addr,
1307 addr = crossVerifyParameterAnnotations(state, item->parametersSize,
1318 static void* swapTypeList(const CheckState* state, void* ptr)
1331 SWAP_INDEX2(pType->typeIdx, state->pHeader->typeIdsSize);
1340 static void* swapAnnotationSetRefList(const CheckState* state, void* ptr) {
1360 static void* crossVerifyAnnotationSetRefList(const CheckState* state,
1367 if (!dexDataMapVerify0Ok(state->pDataMap,
1379 static void* swapAnnotationSetItem(const CheckState* state, void* ptr) {
1406 static void* crossVerifyAnnotationSetItem(const CheckState* state, void* ptr) {
1414 if (!dexDataMapVerify0Ok(state->pDataMap,
1420 dexGetAnnotationItem(state->pDexFile, set, i);
1438 static bool verifyFields(const CheckState* state, u4 size,
1447 CHECK_INDEX(field->fieldIdx, state->pHeader->fieldIdsSize);
1464 static bool verifyMethods(const CheckState* state, u4 size,
1471 CHECK_INDEX(method->methodIdx, state->pHeader->methodIdsSize);
1508 static bool verifyClassDataItem0(const CheckState* state,
1512 okay = verifyFields(state, classData->header.staticFieldsSize,
1520 verifyFields(state, classData->header.instanceFieldsSize,
1528 okay = verifyMethods(state
1536 okay = verifyMethods(state, classData->header.virtualMethodsSize,
1548 static void* intraVerifyClassDataItem(const CheckState* state, void* ptr) {
1550 DexClassData* classData = dexReadAndVerifyClassData(&data, state->fileEnd);
1557 bool okay = verifyClassDataItem0(state, classData);
1571 static u4 findFirstClassDataDefiner(const CheckState* state,
1575 const DexFieldId* field = dexGetFieldId(state->pDexFile, fieldIdx);
1581 const DexFieldId* field = dexGetFieldId(state->pDexFile, fieldIdx);
1587 const DexMethodId* meth = dexGetMethodId(state->pDexFile, methodIdx);
1593 const DexMethodId* meth = dexGetMethodId(state->pDexFile, methodIdx);
1601 static void* crossVerifyClassDataItem(const CheckState* state, void* ptr) {
1603 DexClassData* classData = dexReadAndVerifyClassData(&data, state->fileEnd);
1604 u4 definingClass = findFirstClassDataDefiner(state, classData);
1611 okay = verifyFieldDefiner(state, definingClass, field->fieldIdx);
1617 okay = verifyFieldDefiner(state, definingClass, field->fieldIdx);
1623 okay = dexDataMapVerify0Ok(state->pDataMap, meth->codeOff,
1625 && verifyMethodDefiner(state, definingClass, meth->methodIdx);
1631 okay = dexDataMapVerify0Ok(state->pDataMap, meth->codeOff,
1633 && verifyMethodDefiner(state, definingClass, meth->methodIdx);
1648 static u4 setHandlerOffsAndVerify(const CheckState* state,
1650 const u1* fileEnd = state->fileEnd;
1689 CHECK_INDEX(typeIdx, state->pHeader->typeIdsSize);
1726 static void* swapTriesAndCatches(const CheckState* state, DexCode* code) {
1731 readAndVerifyUnsignedLeb128(&encodedPtr, state->fileEnd, &okay);
1744 u4 endOffset = setHandlerOffsAndVerify(state, code,
1801 static void* swapCodeItem(const CheckState* state, void* ptr) {
1853 ptr = swapTriesAndCatches(state, item);
1860 static void* intraVerifyStringDataItem(const CheckState* state, void* ptr) {
1861 const u1* fileEnd = state->fileEnd;
1961 static void* intraVerifyDebugInfoItem(const CheckState* state, void* ptr) {
1962 const u1* fileEnd = state->fileEnd;
1998 CHECK_INDEX(parameterName, state->pHeader->stringIdsSize);
2031 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2037 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2063 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2069 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2075 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2084 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2103 static const u1* verifyEncodedValue(const CheckState* state, const u1* data,
2105 static const u1* verifyEncodedAnnotation(const CheckState* state,
2110 static u4 readUnsignedLittleEndian(const CheckState* state, const u1** pData,
2128 static const u1* verifyEncodedArray(const CheckState* state,
2131 u4 size = readAndVerifyUnsignedLeb128(&data, state->fileEnd, &okay);
2139 data = verifyEncodedValue(state, data, crossVerify);
2151 static const u1* verifyEncodedValue(const CheckState* state,
2196 u4 idx = readUnsignedLittleEndian(state, &data, valueArg + 1);
2197 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2205 u4 idx = readUnsignedLittleEndian(state, &data, valueArg + 1);
2206 CHECK_INDEX(idx, state->pHeader->typeIdsSize);
2215 u4 idx = readUnsignedLittleEndian(state, &data, valueArg + 1);
2216 CHECK_INDEX(idx, state->pHeader->fieldIdsSize);
2224 u4 idx = readUnsignedLittleEndian(state, &data, valueArg + 1);
2225 CHECK_INDEX(idx, state->pHeader->methodIdsSize);
2233 data = verifyEncodedArray(state, data, crossVerify);
2241 data = verifyEncodedAnnotation(state, data, crossVerify);
2271 static const u1* verifyEncodedAnnotation(const CheckState* state,
2273 const u1* fileEnd = state->fileEnd;
2282 CHECK_INDEX(idx, state->pHeader->typeIdsSize);
2285 const char* descriptor = dexStringByTypeIdx(state->pDexFile, idx);
2309 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2312 const char* name = dexStringById(state->pDexFile, idx);
2327 data = verifyEncodedValue(state, data, crossVerify);
2339 static void* intraVerifyEncodedArrayItem(const CheckState* state, void* ptr) {
2340 return (void*) verifyEncodedArray(state, (const u1*) ptr, false);
2344 static void* intraVerifyAnnotationItem(const CheckState* state, void* ptr) {
2361 return (void*) verifyEncodedAnnotation(state, data, false);
2365 static void* crossVerifyAnnotationItem(const CheckState* state, void* ptr) {
2371 return (void*) verifyEncodedAnnotation(state, data, true);
2380 typedef void* ItemVisitorFunction(const CheckState* state, void* ptr);
2387 static bool iterateSectionWithOptionalUpdate(CheckState* state,
2393 state->previousItem = NULL;
2397 u1* ptr = (u1*) filePointer(state, newOffset);
2400 ptr = (u1*) filePointer(state, offset);
2414 u1* newPtr = (u1*) func(state, ptr);
2415 newOffset = fileOffset(state, newPtr);
2422 if (newOffset > state->fileLen) {
2428 dexDataMapAdd(state->pDataMap, offset, mapType);
2431 state->previousItem = ptr;
2447 static bool iterateSection(CheckState* state, u4 offset, u4 count,
2449 return iterateSectionWithOptionalUpdate(state, offset, count, func,
2457 static bool checkBoundsAndIterateSection(CheckState* state,
2472 return iterateSection(state, offset, count, func, alignment, nextOffset);
2479 static bool iterateDataSection(CheckState* state, u4 offset, u4 count,
2481 u4 dataStart = state->pHeader->dataOff;
2482 u4 dataEnd = dataStart + state->pHeader->dataSize;
2491 if (!iterateSectionWithOptionalUpdate(state, offset, count, func,
2513 static bool swapEverythingButHeaderAndMap(CheckState* state,
2527 const u1* ptr = (const u1*) filePointer(state, lastOffset);
2554 okay = checkHeaderSection(state, sectionOffset, sectionCount,
2559 okay = checkBoundsAndIterateSection(state, sectionOffset,
2560 sectionCount, state->pHeader->stringIdsOff,
2561 state->pHeader->stringIdsSize, swapStringIdItem,
2566 okay = checkBoundsAndIterateSection(state, sectionOffset,
2567 sectionCount, state->pHeader->typeIdsOff,
2568 state->pHeader->typeIdsSize, swapTypeIdItem,
2573 okay = checkBoundsAndIterateSection(state, sectionOffset,
2574 sectionCount, state->pHeader->protoIdsOff,
2575 state->pHeader->protoIdsSize, swapProtoIdItem,
2580 okay = checkBoundsAndIterateSection(state, sectionOffset,
2581 sectionCount, state->pHeader->fieldIdsOff,
2582 state->pHeader->fieldIdsSize, swapFieldIdItem,
2587 okay = checkBoundsAndIterateSection(state, sectionOffset,
2588 sectionCount, state->pHeader->methodIdsOff,
2589 state
2594 okay = checkBoundsAndIterateSection(state, sectionOffset,
2595 sectionCount, state->pHeader->classDefsOff,
2596 state->pHeader->classDefsSize, swapClassDefItem,
2605 okay = checkMapSection(state, sectionOffset, sectionCount,
2610 okay = iterateDataSection(state, sectionOffset, sectionCount,
2615 okay = iterateDataSection(state, sectionOffset, sectionCount,
2621 okay = iterateDataSection(state, sectionOffset, sectionCount,
2626 okay = iterateDataSection(state, sectionOffset, sectionCount,
2632 okay = iterateDataSection(state, sectionOffset, sectionCount,
2637 okay = iterateDataSection(state, sectionOffset, sectionCount,
2643 okay = iterateDataSection(state, sectionOffset, sectionCount,
2649 okay = iterateDataSection(state, sectionOffset, sectionCount,
2655 okay = iterateDataSection(state, sectionOffset, sectionCount,
2661 okay = iterateDataSection(state, sectionOffset, sectionCount,
2687 static bool crossVerifyEverything(CheckState* state, DexMapList* pMap)
2710 okay = iterateSection(state, sectionOffset, sectionCount,
2715 okay = iterateSection(state, sectionOffset, sectionCount,
2720 okay = iterateSection(state, sectionOffset, sectionCount,
2725 okay = iterateSection(state, sectionOffset, sectionCount,
2730 okay = iterateSection(state, sectionOffset, sectionCount,
2736 size_t arraySize = calcDefinedClassBitsSize(state);
2739 state->pDefinedClassBits = definedClassBits;
2741 okay = iterateSection(state, sectionOffset, sectionCount,
2744 state->pDefinedClassBits = NULL;
2748 okay = iterateSection(state, sectionOffset, sectionCount,
2753 okay = iterateSection(state, sectionOffset, sectionCount,
2758 okay = iterateSection(state, sectionOffset, sectionCount,
2763 okay = iterateSection(state, sectionOffset, sectionCount,
2820 CheckState state;
2823 memset(&state, 0, sizeof(state));
2874 state.fileStart = addr;
2875 state.fileEnd = addr + len;
2876 state.fileLen = len;
2877 state.pDexFile = NULL;
2878 state.pDataMap = NULL;
2879 state.pDefinedClassBits = NULL;
2880 state.previousItem = NULL;
2885 okay = swapDexHeader(&state, pHeader);
2889 state.pHeader = pHeader;
2911 okay = okay && swapMap(&state, pDexMap);
2912 okay = okay && swapEverythingButHeaderAndMap(&state, pDexMap);
2915 state.pDexFile = &dexFile;
2917 okay = okay && crossVerifyEverything(&state, pDexMap);
2928 if (state.pDataMap != NULL) {
2929 dexDataMapFree(state.pDataMap);