Skip to content

Commit

Permalink
added cast instructions (#192)
Browse files Browse the repository at this point in the history
Co-authored-by: nbauma109 <nbauma109@github.com>
  • Loading branch information
nbauma109 and nbauma109 authored Mar 17, 2023
1 parent c00ffd2 commit 4d82eb8
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 531 deletions.
13 changes: 11 additions & 2 deletions src/main/java/jd/core/model/classfile/ClassFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class ClassFile extends Base
private final String internalPackageName;

private ClassFile outerClass;
private ClassFile superClassFile;
private Field outerThisField;
private List<ClassFile> innerClassFiles;

Expand Down Expand Up @@ -382,7 +383,7 @@ public Method getMethod(int methodNameIndex, int methodDescriptorIndex)
return method;
}
}
return null;
return superClassFile == null ? null : superClassFile.getMethod(methodNameIndex, methodDescriptorIndex);
}

public Method getMethod(String methodName, String methodDescriptor)
Expand All @@ -402,7 +403,7 @@ public Method getMethod(String methodName, String methodDescriptor)
}
}
}
return null;
return superClassFile == null ? null : superClassFile.getMethod(methodName, methodDescriptor);
}

public List<Instruction> getEnumValues()
Expand Down Expand Up @@ -444,6 +445,14 @@ public VariableNameGenerator getVariableNameGenerator() {
return variableNameGenerator;
}

public ClassFile getSuperClassFile() {
return superClassFile;
}

public void setSuperClassFile(ClassFile superClassFile) {
this.superClassFile = superClassFile;
}

@Override
public String toString() {
return internalClassName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,29 @@
******************************************************************************/
package jd.core.model.instruction.bytecode.instruction;

import java.util.function.BiFunction;

import jd.core.model.classfile.ClassFile;
import jd.core.model.classfile.LocalVariables;

public class AConstNull extends Instruction
{
private BiFunction<ClassFile, LocalVariables, String> signatureFunction;

public AConstNull(int opcode, int offset, int lineNumber)
{
super(opcode, offset, lineNumber);
}

public void setSignatureFunction(BiFunction<ClassFile, LocalVariables, String> signatureFunction)
{
this.signatureFunction = signatureFunction;
}

@Override
public String getReturnedSignature(
ClassFile classFile, LocalVariables localVariables)
{
return null;
return signatureFunction == null ? null : signatureFunction.apply(classFile, localVariables);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.bcel.classfile.ConstantCP;
import org.apache.bcel.classfile.ConstantNameAndType;
import org.apache.bcel.classfile.Signature;
import org.jd.core.v1.util.StringConstants;

import java.util.List;

Expand Down Expand Up @@ -60,8 +59,7 @@ public String getReturnedSignature(
String methodDescriptor = constants.getConstantUtf8(cnat.getSignatureIndex());

String methodReturnedSignature = SignatureUtil.getMethodReturnedSignature(methodDescriptor);
if (methodReturnedSignature.endsWith(StringConstants.INTERNAL_OBJECT_SIGNATURE)
&& internalClassName.charAt(0) != '['
if (internalClassName.charAt(0) != '['
&& classFile.getLoader().canLoad(internalClassName)) {
ClassFile javaClass = ClassFileDeserializer.deserialize(classFile.getLoader(), internalClassName);
Method method = javaClass.getMethod(methodName, methodDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import jd.core.model.classfile.LocalVariables;
import jd.core.model.classfile.Method;
import jd.core.model.instruction.bytecode.ByteCodeConstants;
import jd.core.model.instruction.bytecode.instruction.AConstNull;
import jd.core.model.instruction.bytecode.instruction.ALoad;
import jd.core.model.instruction.bytecode.instruction.AStore;
import jd.core.model.instruction.bytecode.instruction.ArrayLoadInstruction;
Expand All @@ -52,7 +53,6 @@
import jd.core.model.instruction.bytecode.instruction.ReturnInstruction;
import jd.core.model.instruction.bytecode.instruction.StoreInstruction;
import jd.core.model.instruction.bytecode.instruction.TernaryOpStore;
import jd.core.process.analyzer.classfile.visitor.AddCheckCastVisitor;
import jd.core.process.analyzer.classfile.visitor.SearchInstructionByOffsetVisitor;
import jd.core.process.analyzer.instruction.bytecode.util.ByteCodeUtil;
import jd.core.process.analyzer.util.InstructionUtil;
Expand Down Expand Up @@ -208,7 +208,7 @@ public static void analyze(
String returnedSignature = getReturnedSignature(classFile, method);

analyzeMethodCode(
classFile, localVariables, list, listForAnalyze,
classFile, localVariables, listForAnalyze,
returnedSignature);

// Upgrade byte type to char type
Expand Down Expand Up @@ -687,8 +687,7 @@ private static void checkLocalVariableRangesForIndexInstruction(
*/
private static void analyzeMethodCode(
ClassFile classFile,
LocalVariables localVariables, List<Instruction> list,
List<Instruction> listForAnalyze, String returnedSignature)
LocalVariables localVariables, List<Instruction> listForAnalyze, String returnedSignature)
{
ConstantPool constants = classFile.getConstantPool();

Expand All @@ -711,6 +710,16 @@ private static void analyzeMethodCode(
((IndexInstruction)instruction).getIndex(), i,
returnedSignature);
}

if (instruction.getOpcode() == ByteCodeConstants.TERNARYOPSTORE && i<length-1) {
TernaryOpStore ternaryOpStore = (TernaryOpStore) instruction;
int ternaryOp2ndValueOffset = ternaryOpStore.getTernaryOp2ndValueOffset();
Instruction ternaryOp2ndValue = SearchInstructionByOffsetVisitor.visit(listForAnalyze.get(i+1), ternaryOp2ndValueOffset);
if (ternaryOp2ndValue instanceof AConstNull) {
AConstNull aConstNull = (AConstNull) ternaryOp2ndValue;
aConstNull.setSignatureFunction(ternaryOpStore::getReturnedSignature);
}
}
}

// Analyse inverse
Expand Down Expand Up @@ -796,16 +805,6 @@ private static void analyzeMethodCode(
break;
}
}

LocalVariable lv;
// Ajout d'instructions "cast"
for (int i=0; i<length; i++)
{
lv = localVariables.getLocalVariableAt(i);
if (lv.getSignatureIndex() == internalObjectSignatureIndex) {
addCastInstruction(constants, list, localVariables, lv);
}
}
}

/** Analyse du type de la variable locale No varIndex. */
Expand Down Expand Up @@ -1832,16 +1831,4 @@ private static boolean reverseAnalyzePutStaticPutField(

return false;
}

private static void addCastInstruction(
ConstantPool constants, List<Instruction> list,
LocalVariables localVariables, LocalVariable lv)
{
// Add cast instruction before all 'ALoad' instruction for local
// variable le used type is not 'Object'.
AddCheckCastVisitor visitor = new AddCheckCastVisitor(
constants, localVariables, lv);

list.forEach(visitor::visit);
}
}
Loading

0 comments on commit 4d82eb8

Please sign in to comment.