Home | History | Annotate | Download | only in libdex

Lines Matching refs: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);
924 static u4 findFirstClassDataDefiner(const CheckState* state,
926 static u4 findFirstAnnotationsDirectoryDefiner(const CheckState* state,
931 static bool verifyClassDataIsForDef(const CheckState* state, u4 offset,
937 const u1* data = (const u1*) filePointer(state, offset);
950 u4 dataDefiner = findFirstClassDataDefiner(state, classData);
960 static bool verifyAnnotationsDirectoryIsForDef(const CheckState* state,
967 (const DexAnnotationsDirectoryItem*) filePointer(state, offset);
968 u4 annoDefiner = findFirstAnnotationsDirectoryDefiner(state, dir);
974 static void* crossVerifyClassDefItem(const CheckState* state, void* ptr) {
977 const char* descriptor = dexStringByTypeIdx(state->pDexFile, classIdx);
984 if (setDefinedClassBit(state, classIdx)) {
990 dexDataMapVerify0Ok(state->pDataMap,
992 && dexDataMapVerify0Ok(state->pDataMap,
994 && dexDataMapVerify0Ok(state->pDataMap,
996 && dexDataMapVerify0Ok(state->pDataMap,
1004 descriptor = dexStringByTypeIdx(state->pDexFile, item->superclassIdx);
1012 dexGetInterfacesList(state->pDexFile, item);
1022 descriptor = dexStringByTypeIdx(state->pDexFile,
1044 dexStringByTypeIdx(state->pDexFile, idx1));
1051 if (!verifyClassDataIsForDef(state, item->classDataOff, item->classIdx)) {
1056 if (!verifyAnnotationsDirectoryIsForDef(state, item->annotationsOff,
1068 static u1* swapFieldAnnotations(const CheckState* state, u4 count, u1* addr) {
1076 SWAP_INDEX4(item->fieldIdx, state->pHeader->fieldIdsSize);
1097 static u1* swapMethodAnnotations(const CheckState* state, u4 count, u1* addr) {
1105 SWAP_INDEX4(item->methodIdx, state->pHeader->methodIdsSize);
1126 static u1* swapParameterAnnotations(const CheckState* state, u4 count,
1135 SWAP_INDEX4(item->methodIdx, state->pHeader->methodIdsSize);
1155 static void* swapAnnotationsDirectoryItem(const CheckState* state, void* ptr) {
1167 addr = swapFieldAnnotations(state, item->fieldsSize, addr);
1174 addr = swapMethodAnnotations(state, item->methodsSize, addr);
1181 addr = swapParameterAnnotations(state, item->parametersSize, addr);
1192 static const u1* crossVerifyFieldAnnotations(const CheckState* state, u4 count,
1197 if (!verifyFieldDefiner(state, definingClass, item->fieldIdx)) {
1200 if (!dexDataMapVerify(state->pDataMap, item->annotationsOff,
1212 static const u1* crossVerifyMethodAnnotations(const CheckState* state,
1217 if (!verifyMethodDefiner(state, definingClass, item->methodIdx)) {
1220 if (!dexDataMapVerify(state->pDataMap, item->annotationsOff,
1232 static const u1* crossVerifyParameterAnnotations(const CheckState* state,
1238 if (!verifyMethodDefiner(state, definingClass, item->methodIdx)) {
1241 if (!dexDataMapVerify(state->pDataMap, item->annotationsOff,
1254 static u4 findFirstAnnotationsDirectoryDefiner(const CheckState* state,
1258 dexGetFieldAnnotations(state->pDexFile, dir);
1260 dexGetFieldId(state->pDexFile, fields[0].fieldIdx);
1266 dexGetMethodAnnotations(state->pDexFile, dir);
1268 dexGetMethodId(state->pDexFile, methods[0].methodIdx);
1274 dexGetParameterAnnotations(state->pDexFile, dir);
1276 dexGetMethodId(state->pDexFile, parameters[0].methodIdx);
1284 static void* crossVerifyAnnotationsDirectoryItem(const CheckState* state,
1287 u4 definingClass = findFirstAnnotationsDirectoryDefiner(state, item);
1289 if (!dexDataMapVerify0Ok(state->pDataMap,
1297 addr = crossVerifyFieldAnnotations(state, item->fieldsSize, addr,
1305 addr = crossVerifyMethodAnnotations(state, item->methodsSize, addr,
1313 addr = crossVerifyParameterAnnotations(state, item->parametersSize,
1324 static void* swapTypeList(const CheckState* state, void* ptr)
1337 SWAP_INDEX2(pType->typeIdx, state->pHeader->typeIdsSize);
1346 static void* swapAnnotationSetRefList(const CheckState* state, void* ptr) {
1366 static void* crossVerifyAnnotationSetRefList(const CheckState* state,
1373 if (!dexDataMapVerify0Ok(state->pDataMap,
1385 static void* swapAnnotationSetItem(const CheckState* state, void* ptr) {
1412 static void* crossVerifyAnnotationSetItem(const CheckState* state, void* ptr) {
1420 if (!dexDataMapVerify0Ok(state->pDataMap,
1426 dexGetAnnotationItem(state->pDexFile, set, i);
1444 static bool verifyFields(const CheckState* state, u4 size,
1453 CHECK_INDEX(field->fieldIdx, state->pHeader->fieldIdsSize);
1471 static bool verifyMethods(const CheckState* state, u4 size,
1478 CHECK_INDEX(method->methodIdx, state->pHeader->methodIdsSize);
1520 static bool verifyClassDataItem0(const CheckState* state,
1524 okay = verifyFields(state, classData->header.staticFieldsSize,
1532 verifyFields(state, classData->header.instanceFieldsSize,
1540 okay = verifyMethods(state, classData->header.directMethodsSize,
1548 okay = verifyMethods(state, classData->header.virtualMethodsSize,
1560 static void* intraVerifyClassDataItem(const CheckState* state, void* ptr) {
1562 DexClassData* classData = dexReadAndVerifyClassData(&data, state->fileEnd);
1569 bool okay = verifyClassDataItem0(state, classData);
1583 static u4 findFirstClassDataDefiner(const CheckState* state,
1587 const DexFieldId* field = dexGetFieldId(state->pDexFile, fieldIdx);
1593 const DexFieldId* field = dexGetFieldId(state->pDexFile, fieldIdx);
1599 const DexMethodId* meth = dexGetMethodId(state->pDexFile, methodIdx);
1605 const DexMethodId* meth = dexGetMethodId(state->pDexFile, methodIdx);
1613 static void* crossVerifyClassDataItem(const CheckState* state, void* ptr) {
1615 DexClassData* classData = dexReadAndVerifyClassData(&data, state->fileEnd);
1616 u4 definingClass = findFirstClassDataDefiner(state, classData);
1623 okay = verifyFieldDefiner(state, definingClass, field->fieldIdx);
1629 okay = verifyFieldDefiner(state, definingClass, field->fieldIdx);
1635 okay = dexDataMapVerify0Ok(state->pDataMap, meth->codeOff,
1637 && verifyMethodDefiner(state, definingClass, meth->methodIdx);
1643 okay = dexDataMapVerify0Ok(state->pDataMap, meth->codeOff,
1645 && verifyMethodDefiner(state, definingClass, meth->methodIdx);
1660 static u4 setHandlerOffsAndVerify(const CheckState* state,
1662 const u1* fileEnd = state->fileEnd;
1701 CHECK_INDEX(typeIdx, state->pHeader->typeIdsSize);
1738 static void* swapTriesAndCatches(const CheckState* state, DexCode* code) {
1743 readAndVerifyUnsignedLeb128(&encodedPtr, state->fileEnd, &okay);
1756 u4 endOffset = setHandlerOffsAndVerify(state, code,
1813 static void* swapCodeItem(const CheckState* state, void* ptr) {
1865 ptr = swapTriesAndCatches(state, item);
1872 static void* intraVerifyStringDataItem(const CheckState* state, void* ptr) {
1873 const u1* fileEnd = state->fileEnd;
1973 static void* intraVerifyDebugInfoItem(const CheckState* state, void* ptr) {
1974 const u1* fileEnd = state->fileEnd;
2010 CHECK_INDEX(parameterName, state->pHeader->stringIdsSize);
2043 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2049 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2075 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2081 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2087 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2096 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2115 static const u1* verifyEncodedValue(const CheckState* state, const u1* data,
2117 static const u1* verifyEncodedAnnotation(const CheckState* state,
2122 static u4 readUnsignedLittleEndian(const CheckState* state, const u1** pData,
2140 static const u1* verifyEncodedArray(const CheckState* state,
2143 u4 size = readAndVerifyUnsignedLeb128(&data, state->fileEnd, &okay);
2151 data = verifyEncodedValue(state, data, crossVerify);
2163 static const u1* verifyEncodedValue(const CheckState* state,
2208 u4 idx = readUnsignedLittleEndian(state, &data, valueArg + 1);
2209 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2217 u4 idx = readUnsignedLittleEndian(state, &data, valueArg + 1);
2218 CHECK_INDEX(idx, state->pHeader->typeIdsSize);
2227 u4 idx = readUnsignedLittleEndian(state, &data, valueArg + 1);
2228 CHECK_INDEX(idx, state->pHeader->fieldIdsSize);
2236 u4 idx = readUnsignedLittleEndian(state, &data, valueArg + 1);
2237 CHECK_INDEX(idx, state->pHeader->methodIdsSize);
2245 data = verifyEncodedArray(state, data, crossVerify);
2253 data = verifyEncodedAnnotation(state, data, crossVerify);
2283 static const u1* verifyEncodedAnnotation(const CheckState* state,
2285 const u1* fileEnd = state->fileEnd;
2294 CHECK_INDEX(idx, state->pHeader->typeIdsSize);
2297 const char* descriptor = dexStringByTypeIdx(state->pDexFile, idx);
2321 CHECK_INDEX(idx, state->pHeader->stringIdsSize);
2324 const char* name = dexStringById(state->pDexFile, idx);
2339 data = verifyEncodedValue(state, data, crossVerify);
2351 static void* intraVerifyEncodedArrayItem(const CheckState* state, void* ptr) {
2352 return (void*) verifyEncodedArray(state, (const u1*) ptr, false);
2356 static void* intraVerifyAnnotationItem(const CheckState* state, void* ptr) {
2373 return (void*) verifyEncodedAnnotation(state, data, false);
2377 static void* crossVerifyAnnotationItem(const CheckState* state, void* ptr) {
2383 return (void*) verifyEncodedAnnotation(state, data, true);
2392 typedef void* ItemVisitorFunction(const CheckState* state, void* ptr);
2399 static bool iterateSectionWithOptionalUpdate(CheckState* state,
2405 state->previousItem = NULL;
2409 u1* ptr = (u1*) filePointer(state, newOffset);
2412 ptr = (u1*) filePointer(state, offset);
2426 u1* newPtr = (u1*) func(state, ptr);
2427 newOffset = fileOffset(state, newPtr);
2434 if (newOffset > state->fileLen) {
2440 dexDataMapAdd(state->pDataMap, offset, mapType);
2443 state->previousItem = ptr;
2459 static bool iterateSection(CheckState* state, u4 offset, u4 count,
2461 return iterateSectionWithOptionalUpdate(state, offset, count, func,
2469 static bool checkBoundsAndIterateSection(CheckState* state,
2484 return iterateSection(state, offset, count, func, alignment, nextOffset);
2491 static bool iterateDataSection(CheckState* state, u4 offset, u4 count,
2493 u4 dataStart = state->pHeader->dataOff;
2494 u4 dataEnd = dataStart + state->pHeader->dataSize;
2503 if (!iterateSectionWithOptionalUpdate(state, offset, count, func,
2525 static bool swapEverythingButHeaderAndMap(CheckState* state,
2539 const u1* ptr = (const u1*) filePointer(state, lastOffset);
2566 okay = checkHeaderSection(state, sectionOffset, sectionCount,
2571 okay = checkBoundsAndIterateSection(state, sectionOffset,
2572 sectionCount, state->pHeader->stringIdsOff,
2573 state->pHeader->stringIdsSize, swapStringIdItem,
2578 okay = checkBoundsAndIterateSection(state, sectionOffset,
2579 sectionCount, state->pHeader->typeIdsOff,
2580 state->pHeader->typeIdsSize, swapTypeIdItem,
2585 okay = checkBoundsAndIterateSection(state, sectionOffset,
2586 sectionCount, state->pHeader->protoIdsOff,
2587 state->pHeader->protoIdsSize, swapProtoIdItem,
2592 okay = checkBoundsAndIterateSection(state, sectionOffset,
2593 sectionCount, state->pHeader->fieldIdsOff,
2594 state->pHeader->fieldIdsSize, swapFieldIdItem,
2599 okay = checkBoundsAndIterateSection(state, sectionOffset,
2600 sectionCount, state->pHeader->methodIdsOff,
2601 state->pHeader->methodIdsSize, swapMethodIdItem,
2606 okay = checkBoundsAndIterateSection(state, sectionOffset,
2607 sectionCount, state->pHeader->classDefsOff,
2608 state->pHeader->classDefsSize, swapClassDefItem,
2617 okay = checkMapSection(state, sectionOffset, sectionCount,
2622 okay = iterateDataSection(state, sectionOffset, sectionCount,
2627 okay = iterateDataSection(state, sectionOffset, sectionCount,
2633 okay = iterateDataSection(state, sectionOffset, sectionCount,
2638 okay = iterateDataSection(state, sectionOffset, sectionCount,
2644 okay = iterateDataSection(state, sectionOffset, sectionCount,
2649 okay = iterateDataSection(state, sectionOffset, sectionCount,
2655 okay = iterateDataSection(state, sectionOffset, sectionCount,
2661 okay = iterateDataSection(state, sectionOffset, sectionCount,
2667 okay = iterateDataSection(state, sectionOffset, sectionCount,
2673 okay = iterateDataSection(state, sectionOffset, sectionCount,
2699 static bool crossVerifyEverything(CheckState* state, DexMapList* pMap)
2722 okay = iterateSection(state, sectionOffset, sectionCount,
2727 okay = iterateSection(state, sectionOffset, sectionCount,
2732 okay = iterateSection(state, sectionOffset, sectionCount,
2737 okay = iterateSection(state, sectionOffset, sectionCount,
2742 okay = iterateSection(state, sectionOffset, sectionCount,
2748 size_t arraySize = calcDefinedClassBitsSize(state);
2751 state->pDefinedClassBits = definedClassBits;
2753 okay = iterateSection(state, sectionOffset, sectionCount,
2756 state->pDefinedClassBits = NULL;
2760 okay = iterateSection(state, sectionOffset, sectionCount,
2765 okay = iterateSection(state, sectionOffset, sectionCount,
2770 okay = iterateSection(state, sectionOffset, sectionCount,
2775 okay = iterateSection(state, sectionOffset, sectionCount,
2832 CheckState state;
2835 memset(&state, 0, sizeof(state));
2886 state.fileStart = addr;
2887 state.fileEnd = addr + len;
2888 state.fileLen = len;
2889 state.pDexFile = NULL;
2890 state.pDataMap = NULL;
2891 state.pDefinedClassBits = NULL;
2892 state.previousItem = NULL;
2897 okay = swapDexHeader(&state, pHeader);
2901 state.pHeader = pHeader;
2923 okay = okay && swapMap(&state, pDexMap);
2924 okay = okay && swapEverythingButHeaderAndMap(&state, pDexMap);
2927 state.pDexFile = &dexFile;
2929 okay = okay && crossVerifyEverything(&state, pDexMap);
2940 if (state.pDataMap != NULL) {
2941 dexDataMapFree(state.pDataMap);