/* * Check that analyze_enum_clinit returns the correct enum field -> ordinal * mapping. */ TEST_F(RedexTest, OrdinalAnalysis) { always_assert(load_class_file(std::getenv("enum_class_file"))); auto dexfile = std::getenv("dexfile"); std::vector<DexStore> stores; DexMetadata dm; dm.set_id("classes"); DexStore root_store(dm); root_store.add_classes(load_classes_from_dex(dexfile)); auto scope = build_class_scope(root_store.get_dexen()); auto enumA = type_class(DexType::get_type(ENUM_A)); auto enum_field_to_ordinal = optimize_enums::analyze_enum_clinit(enumA); auto enumA_zero = static_cast<DexField*>( DexField::get_field("Lcom/facebook/redextest/EnumA;.TYPE_A_0:Lcom/" "facebook/redextest/EnumA;")); auto enumA_one = static_cast<DexField*>( DexField::get_field("Lcom/facebook/redextest/EnumA;.TYPE_A_1:Lcom/" "facebook/redextest/EnumA;")); auto enumA_two = static_cast<DexField*>( DexField::get_field("Lcom/facebook/redextest/EnumA;.TYPE_A_2:Lcom/" "facebook/redextest/EnumA;")); EXPECT_EQ(enum_field_to_ordinal.at(enumA_zero), 0); EXPECT_EQ(enum_field_to_ordinal.at(enumA_one), 1); EXPECT_EQ(enum_field_to_ordinal.at(enumA_two), 2); }
/* Load in all the class files. */ void load_classes( ) { FILE *fpList; const char *filename; char classlist[256]; int i; MAX_PC_CLASS = 0; /* * Pre-init the class_table with blank classes */ for ( i = 0; i < MAX_CLASS; ++i ) class_table[i] = NULL; snprintf( classlist, 256, "%s%s", CLASSDIR, CLASS_LIST ); if ( ( fpList = FileOpen( classlist, "r" ) ) == NULL ) { bug( "%s: Can't open %s for reading", __FUNCTION__, CLASS_LIST ); perror( classlist ); exit( 1 ); } for ( ;; ) { filename = feof( fpList ) ? "$" : fread_word( fpList ); if ( filename[0] == '$' ) break; if ( !load_class_file( filename ) ) { bug( "%s Cannot load class file: %s", __FUNCTION__, filename ); } else ++MAX_PC_CLASS; } FileClose( fpList ); for ( i = 0; i < MAX_CLASS; ++i ) { if ( class_table[i] == NULL ) { CREATE( class_table[i], struct class_type, 1 ); create_new_class( i, ( char * ) "" ); } }