org.benf.cfr.reader.state.DCCommonState Java Examples

The following examples show how to use org.benf.cfr.reader.state.DCCommonState. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConstructorInvokationAnonymousInner.java    From cfr with MIT License 6 votes vote down vote up
public ConstructorInvokationAnonymousInner(MemberFunctionInvokation constructorInvokation,
                                           InferredJavaType inferredJavaType, List<Expression> args,
                                           DCCommonState dcCommonState, JavaTypeInstance anonymousTypeInstance) {
    super((inferredJavaType), constructorInvokation.getFunction(), args);
    this.constructorInvokation = constructorInvokation;
    this.anonymousTypeInstance = anonymousTypeInstance;
    /*
     * As much as I'd rather not tie this to its use, we have to make sure that the target variables etc
     * are available at the time of usage, so we can hide anonymous inner member clones.
     */
    ClassFile classFile = null;
    try {
        classFile = dcCommonState.getClassFile(
                constructorInvokation.getMethodPrototype().getReturnType().getDeGenerifiedType()
        );
    } catch (CannotLoadClassException e) {
        // Can't find class - live with it.
    }
    this.classFile = classFile;
}
 
Example #2
Source File: EnumClassRewriter.java    From cfr with MIT License 6 votes vote down vote up
public static void rewriteEnumClass(ClassFile classFile, DCCommonState state) {
    Options options = state.getOptions();
    ClassFileVersion classFileVersion = classFile.getClassFileVersion();

    if (!options.getOption(OptionsImpl.ENUM_SUGAR, classFileVersion)) return;

    JavaTypeInstance classType = classFile.getClassType();
    if (!classFile.getBindingSupers().containsBase(TypeConstants.ENUM)) {
        return;
    }

    EnumClassRewriter c = new EnumClassRewriter(classFile, classType, state);
    if (!c.rewrite()) {
        c.removeAllRemainingSupers();
    }
}
 
Example #3
Source File: ClassFile.java    From cfr with MIT License 6 votes vote down vote up
private void checkInnerClassAssumption(AttributeInnerClasses attributeInnerClasses, JavaRefTypeInstance typeInstance, DCCommonState state) {
    if (attributeInnerClasses != null) {
        for (InnerClassAttributeInfo innerClassAttributeInfo : attributeInnerClasses.getInnerClassAttributeInfoList()) {
            if (innerClassAttributeInfo.getInnerClassInfo().equals(typeInstance)) {
                return;
            }
        }
    }
    // If we're handling obfuscation where we've been given inner class info, then that might be more trustworthy.
    if (state.getObfuscationMapping().providesInnerClassInfo()) {
        return;
    }
    /*
     * No - we are NOT an inner class, regardless of what the guessed information says.
     */
    typeInstance.markNotInner();
}
 
Example #4
Source File: JavaRefTypeInstance.java    From cfr with MIT License 6 votes vote down vote up
private JavaRefTypeInstance(final String className, DCCommonState dcCommonState) {
    this.innerClassInfo = InnerClassInfo.NOT;
    this.dcCommonState = dcCommonState;
    /*
     * This /MAY/ be a red herring.  It's possible for a class name to contain a dollar
     * without it being an inner class.
     */
    if (className.contains(MiscConstants.INNER_CLASS_SEP_STR)) {
        int idx = className.lastIndexOf(MiscConstants.INNER_CLASS_SEP_CHAR);

        if (idx == className.length()-1) {
            /*
             * if this is the case, it's likely to be a non-inner scala class.....
             * Would be nice to have a better way to verify though.
             */
            MiscUtils.handyBreakPoint();
        } else {
            String outer = className.substring(0, idx);
            JavaRefTypeInstance outerClassTmp = dcCommonState.getClassCache().getRefClassFor(outer);
            innerClassInfo = new RefTypeInnerClassInfo(outerClassTmp);
        }
    }
    this.className = className;
    this.shortName = getShortName(className, innerClassInfo);
}
 
Example #5
Source File: CodeAnalyser.java    From cfr with MIT License 5 votes vote down vote up
private AnalysisResult getAnalysisOrWrapFail(int passIdx, List<Op01WithProcessedDataAndByteJumps> instrs, DCCommonState commonState, Options options, List<DecompilerComment> extraComments, BytecodeMeta bytecodeMeta) {
    try {
        AnalysisResult res = getAnalysisInner(instrs, commonState, options, bytecodeMeta, passIdx);
        if (extraComments != null) res.getComments().addComments(extraComments);
        return res;
    } catch (RuntimeException e) {
        return new AnalysisResultFromException(e);
    }
}
 
Example #6
Source File: ConstantPool.java    From cfr with MIT License 5 votes vote down vote up
public ConstantPool(ClassFile classFile, DCCommonState dcCommonState, ByteData raw, int count) {
    this.classFile = classFile;
    this.options = dcCommonState.getOptions();
    count--;

    RawTmp tmp = processRaw(raw, count);
    this.entries = tmp.entries;
    this.length = tmp.rawLength;
    this.dynamicConstants = tmp.dynamicConstants;
    this.dcCommonState = dcCommonState;
    this.classCache = dcCommonState.getClassCache();
    this.isLoaded = true;
}
 
Example #7
Source File: EnumClassRewriter.java    From cfr with MIT License 5 votes vote down vote up
private EnumClassRewriter(ClassFile classFile, JavaTypeInstance classType, DCCommonState state) {
    this.classFile = classFile;
    this.classType = classType;
    this.state = state;
    this.options = state.getOptions();
    this.clazzIJT = new InferredJavaType(classType, InferredJavaType.Source.UNKNOWN, true);
}
 
Example #8
Source File: ExceptionCheckImpl.java    From cfr with MIT License 5 votes vote down vote up
public ExceptionCheckImpl(DCCommonState dcCommonState, Set<JavaRefTypeInstance> caught) {
    this.dcCommonState = dcCommonState;
    JavaRefTypeInstance runtimeExceptionType = dcCommonState.getClassTypeOrNull(TypeConstants.runtimeExceptionPath);
    if (runtimeExceptionType == null) {
        mightUseUnchecked = true;
        missingInfo = true;
        return;
    }

    boolean lmightUseUnchecked = false;
    boolean lmissinginfo = false;
    for (JavaRefTypeInstance ref : caught) {
        BindingSuperContainer superContainer = ref.getBindingSupers();
        if (superContainer == null) {
            lmightUseUnchecked = true;
            lmissinginfo = true;
            continue;
        }
        Map<JavaRefTypeInstance, ?> supers = superContainer.getBoundSuperClasses();
        if (supers == null) {
            lmightUseUnchecked = true;
            lmissinginfo = true;
            continue;
        }
        if (supers.containsKey(runtimeExceptionType)) {
            lmightUseUnchecked = true;
            caughtUnchecked.add(ref);
        } else {
            caughtChecked.add(ref);
        }
    }
    mightUseUnchecked = lmightUseUnchecked;
    missingInfo = lmissinginfo;
}
 
Example #9
Source File: CFRDecompiler.java    From java-disassembler with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String decompileClassNode(FileContainer container, ClassNode cn) {
    try {
        byte[] bytes = JDA.getClassBytes(container, cn);
        GetOptParser getOptParser = new GetOptParser();
        Pair processedArgs = getOptParser.parse(generateMainMethod(), OptionsImpl.getFactory());
        List files = (List)processedArgs.getFirst();
        Options options = (Options)processedArgs.getSecond();
        ClassFileSourceImpl classFileSource = new ClassFileSourceImpl(options);
        DCCommonState dcCommonState = new DCCommonState(options, classFileSource);
        return doClass(dcCommonState, bytes);
    } catch (Exception e) {
        return parseException(e);
    }
}
 
Example #10
Source File: CondenseConstruction.java    From cfr with MIT License 5 votes vote down vote up
static void condenseConstruction(DCCommonState state, Method method, List<Op03SimpleStatement> statements, AnonymousClassUsage anonymousClassUsage) {
    CreationCollector creationCollector = new CreationCollector(anonymousClassUsage);
    for (Op03SimpleStatement statement : statements) {
        statement.findCreation(creationCollector);
    }
    creationCollector.condenseConstructions(method, state);
}
 
Example #11
Source File: CodeAnalyserWholeClass.java    From cfr with MIT License 5 votes vote down vote up
private static void inlineAccessors(DCCommonState state, ClassFile classFile) {
    for (Method method : classFile.getMethods()) {
        if (method.hasCodeAttribute()) {
            Op04StructuredStatement code = method.getAnalysis();
            Op04StructuredStatement.inlineSyntheticAccessors(state, method, code);
        }
    }
}
 
Example #12
Source File: RecordRewriter.java    From cfr with MIT License 5 votes vote down vote up
public static void rewrite(ClassFile classFile, DCCommonState state) {
    if (!TypeConstants.RECORD.equals(classFile.getBaseClassType())) return;

    /* For a record, we can eliminate
     * * the constructor (if it only assigns fields, and assigns ALL fields)
     *    (but we must leave validation code in place).
     * * equals, hashcode and toString, if they exactly match the expected bootstrap pattern.
     * * getters (if they are exactly getters).
     */
    rewriteIfRecord(classFile, state);
}
 
Example #13
Source File: Op02WithProcessedDataAndRefs.java    From cfr with MIT License 5 votes vote down vote up
private Statement buildInvokeDynamic(Method method, DCCommonState dcCommonState) {
    ConstantPoolEntryInvokeDynamic invokeDynamic = (ConstantPoolEntryInvokeDynamic) cpEntries[0];
    ConstantPoolEntryNameAndType nameAndType = invokeDynamic.getNameAndTypeEntry();
    int idx = invokeDynamic.getBootstrapMethodAttrIndex();
    ConstantPoolEntryUTF8 descriptor = nameAndType.getDescriptor();
    ConstantPoolEntryUTF8 name = nameAndType.getName();
    MethodPrototype dynamicPrototype = ConstantPoolUtils.parseJavaMethodPrototype(dcCommonState, null, null, "", false, Method.MethodConstructor.NOT, descriptor, cp, false, false, new VariableNamerDefault());
    return buildInvokeDynamic(method.getClassFile(), dcCommonState, name.getValue(), dynamicPrototype, idx, false);
}
 
Example #14
Source File: CodeAnalyserWholeClass.java    From cfr with MIT License 5 votes vote down vote up
public static void wholeClassAnalysisPass3(ClassFile classFile, DCCommonState state, TypeUsageCollectingDumper typeUsage) {
    Options options = state.getOptions();
    if (options.getOption(OptionsImpl.REMOVE_BOILERPLATE)) {
        removeRedundantSupers(classFile);
    }

    if (options.getOption(OptionsImpl.REMOVE_DEAD_METHODS)) {
        removeDeadMethods(classFile);
    }

    rewriteUnreachableStatics(classFile, typeUsage);

    detectFakeMethods(classFile, typeUsage);
}
 
Example #15
Source File: ClassFile.java    From cfr with MIT License 5 votes vote down vote up
private void analyseInnerClassesPass1(DCCommonState state) {
    if (innerClassesByTypeInfo == null) return;
    for (Pair<InnerClassAttributeInfo, ClassFile> innerClassInfoClassFilePair : innerClassesByTypeInfo.values()) {
        ClassFile classFile = innerClassInfoClassFilePair.getSecond();
        classFile.analyseMid(state);
    }
}
 
Example #16
Source File: ClassFile.java    From cfr with MIT License 5 votes vote down vote up
private List<InnerClassAttributeInfo> getInnerClassAttributeInfos(DCCommonState state) {
    AttributeInnerClasses attributeInnerClasses = attributes.getByName(AttributeInnerClasses.ATTRIBUTE_NAME);
    List<InnerClassAttributeInfo> innerClassAttributeInfoList = attributeInnerClasses == null ? null : attributeInnerClasses.getInnerClassAttributeInfoList();
    if (innerClassAttributeInfoList != null) {
        return innerClassAttributeInfoList;
    }
    // If we don't have any inner class attributes, we might be dealing with obfuscated code that has stripped them.
    return state.getObfuscationMapping().getInnerClassInfo(getClassType());
}
 
Example #17
Source File: Op02WithProcessedDataAndRefs.java    From cfr with MIT License 5 votes vote down vote up
public static List<Op03SimpleStatement> convertToOp03List(List<Op02WithProcessedDataAndRefs> op2list,
                                                          final Method method,
                                                          final VariableFactory variableFactory,
                                                          final BlockIdentifierFactory blockIdentifierFactory,
                                                          final DCCommonState dcCommonState,
                                                          final TypeHintRecovery typeHintRecovery) {


    final List<Op03SimpleStatement> op03SimpleParseNodesTmp = ListFactory.newList();
    // Convert the op2s into a simple set of statements.
    // Do these need to be processed in a sensible order?  Could just iterate?

    final GraphConversionHelper<Op02WithProcessedDataAndRefs, Op03SimpleStatement> conversionHelper = new GraphConversionHelper<Op02WithProcessedDataAndRefs, Op03SimpleStatement>();
    // By only processing reachable bytecode, we ignore deliberate corruption.   However, we could
    // Nop out unreachable code, so as to not have this ugliness.
    // We start at 0 as that's not controversial ;)

    GraphVisitor<Op02WithProcessedDataAndRefs> o2Converter = new GraphVisitorFIFO<Op02WithProcessedDataAndRefs>(op2list.get(0),
            new BinaryProcedure<Op02WithProcessedDataAndRefs, GraphVisitor<Op02WithProcessedDataAndRefs>>() {
                @Override
                public void call(Op02WithProcessedDataAndRefs arg1, GraphVisitor<Op02WithProcessedDataAndRefs> arg2) {
                    Op03SimpleStatement res = new Op03SimpleStatement(arg1, arg1.createStatement(method, variableFactory, blockIdentifierFactory, dcCommonState, typeHintRecovery));
                    conversionHelper.registerOriginalAndNew(arg1, res);
                    op03SimpleParseNodesTmp.add(res);
                    for (Op02WithProcessedDataAndRefs target : arg1.getTargets()) {
                        arg2.enqueue(target);
                    }
                }
            }
    );
    o2Converter.process();
    conversionHelper.patchUpRelations();

    return op03SimpleParseNodesTmp;
}
 
Example #18
Source File: ClassFile.java    From cfr with MIT License 5 votes vote down vote up
private boolean testIsInnerClass(DCCommonState dcCommonState) {
    List<InnerClassAttributeInfo> innerClassAttributeInfoList = getInnerClassAttributeInfos(dcCommonState);
    if (innerClassAttributeInfoList == null) return false;
    final JavaTypeInstance thisType = thisClass.getTypeInstance();

    for (InnerClassAttributeInfo innerClassAttributeInfo : innerClassAttributeInfoList) {
        JavaTypeInstance innerType = innerClassAttributeInfo.getInnerClassInfo();
        if (innerType == thisType) return true;
    }
    return false;
}
 
Example #19
Source File: JavaRefTypeInstance.java    From cfr with MIT License 5 votes vote down vote up
private JavaRefTypeInstance(final String className, final JavaRefTypeInstance knownOuter, DCCommonState dcCommonState) {
    this.className = className;
    this.dcCommonState = dcCommonState;
    String innerSub = className.substring(knownOuter.className.length());
    /*
     * Now, we have to make an assumption at this point that if the first character of innerSub is a $ (Sep)
     * that we should replace it.  This isn't mandated by anything, so we could create illegals again here. :P
     *
     */
    if (innerSub.charAt(0) == MiscConstants.INNER_CLASS_SEP_CHAR) {
        innerSub = innerSub.substring(1);
    }
    this.innerClassInfo = new RefTypeInnerClassInfo(knownOuter);
    this.shortName = innerSub;
}
 
Example #20
Source File: RecoveryOptions.java    From cfr with MIT License 5 votes vote down vote up
public Applied apply(DCCommonState commonState, Options originalOptions, BytecodeMeta bytecodeMeta) {
    MutableOptions mutableOptions = new MutableOptions(originalOptions);
    List<DecompilerComment> appliedComments = ListFactory.newList();
    boolean hadEffect = false;
    for (RecoveryOption<?> option : recoveryOptions) {
        if (option.apply(mutableOptions, appliedComments, bytecodeMeta)) hadEffect = true;
    }
    return new Applied(mutableOptions, appliedComments, hadEffect);
}
 
Example #21
Source File: CfrDriverImpl.java    From cfr with MIT License 5 votes vote down vote up
@Override
public void analyse(List<String> toAnalyse) {
    /*
     * There's an interesting question here - do we want to skip inner classes, if we've been given a wildcard?
     * (or a wildcard expanded by the operating system).
     *
     * Assume yes.
     */
    boolean skipInnerClass = toAnalyse.size() > 1 && options.getOption(OptionsImpl.SKIP_BATCH_INNER_CLASSES);

    Collections.sort(toAnalyse);
    for (String path : toAnalyse) {
        // TODO : We shouldn't have to discard state here.  But we do, because
        // it causes test fails.  (used class name table retains useful symbols).
        classFileSource.informAnalysisRelativePathDetail(null, null);
        // Note - both of these need to be reset, as they have caches.
        DCCommonState dcCommonState = new DCCommonState(options, classFileSource);
        DumperFactory dumperFactory = outputSinkFactory != null ?
                new SinkDumperFactory(outputSinkFactory, options) :
                new InternalDumperFactoryImpl(options);

        AnalysisType type = options.getOption(OptionsImpl.ANALYSE_AS);
        if (type == null || type == AnalysisType.DETECT) {
            type = dcCommonState.detectClsJar(path);
        }

        if (type == AnalysisType.JAR || type == AnalysisType.WAR) {
            Driver.doJar(dcCommonState, path, type, dumperFactory);
        } else if (type == AnalysisType.CLASS) {
            Driver.doClass(dcCommonState, path, skipInnerClass, dumperFactory);
        }
    }
}
 
Example #22
Source File: JavaRefTypeInstance.java    From cfr with MIT License 5 votes vote down vote up
public static Pair<JavaRefTypeInstance, JavaRefTypeInstance> createKnownInnerOuter(String inner, String outer, JavaRefTypeInstance outerType, DCCommonState dcCommonState) {
    if (outerType == null) outerType = new JavaRefTypeInstance(outer, dcCommonState);
    JavaRefTypeInstance innerType;
    if (!inner.startsWith(outer)) {
        // Handle the illegal inner/outer combo.
        innerType = new JavaRefTypeInstance(inner, dcCommonState);
    } else {
        innerType = new JavaRefTypeInstance(inner, outerType, dcCommonState);
    }
    return Pair.make(innerType, outerType);
}
 
Example #23
Source File: PluginRunner.java    From cfr with MIT License 5 votes vote down vote up
private static DCCommonState initDCState(Map<String, String> optionsMap, ClassFileSource classFileSource) {
    OptionsImpl options = new OptionsImpl(optionsMap);
    ClassFileSource2 source = classFileSource == null ?
            new ClassFileSourceImpl(options) :
            new ClassFileSourceWrapper(classFileSource);
    return new DCCommonState(options, source);
}
 
Example #24
Source File: MappingFactory.java    From cfr with MIT License 5 votes vote down vote up
public static ObfuscationMapping get(Options options, DCCommonState state) {
    String path = options.getOption(OptionsImpl.OBFUSCATION_PATH);
    if (path == null) {
        return NullMapping.INSTANCE;
    }
    return new MappingFactory(options, state.getClassCache()).createFromPath(path);
}
 
Example #25
Source File: J14ClassObjectRewriter.java    From cfr with MIT License 4 votes vote down vote up
public J14ClassObjectRewriter(ClassFile classFile, DCCommonState state) {
    this.classFile = classFile;
    this.state = state;
}
 
Example #26
Source File: CodeAnalyserWholeClass.java    From cfr with MIT License 4 votes vote down vote up
private static void resugarRecords(ClassFile classFile, DCCommonState state) {
    RecordRewriter.rewrite(classFile, state);
}
 
Example #27
Source File: SwitchEnumRewriter.java    From cfr with MIT License 4 votes vote down vote up
public SwitchEnumRewriter(DCCommonState dcCommonState, ClassFile classFile, BlockIdentifierFactory blockIdentifierFactory) {
    this.dcCommonState = dcCommonState;
    this.classFile = classFile;
    this.classFileVersion = classFile.getClassFileVersion();
    this.blockIdentifierFactory = blockIdentifierFactory;
}
 
Example #28
Source File: SyntheticAccessorRewriter.java    From cfr with MIT License 4 votes vote down vote up
public SyntheticAccessorRewriter(DCCommonState state, JavaTypeInstance thisClassType) {
    this.state = state;
    this.thisClassType = thisClassType;
}
 
Example #29
Source File: LambdaRewriter.java    From cfr with MIT License 4 votes vote down vote up
public LambdaRewriter(DCCommonState state, Method method) {
    this.state = state;
    this.method = method;
    this.thisClassFile = method.getClassFile();
    this.typeInstance = thisClassFile.getClassType().getDeGenerifiedType();
}
 
Example #30
Source File: TryRewriter.java    From cfr with MIT License 4 votes vote down vote up
static void extendTryBlocks(DCCommonState dcCommonState, List<Op03SimpleStatement> in) {
    List<Op03SimpleStatement> tries = Functional.filter(in, new TypeFilter<TryStatement>(TryStatement.class));
    for (Op03SimpleStatement tryStatement : tries) {
        extendTryBlock(tryStatement, in, dcCommonState);
    }
}