From 8481c4bfc2601d1aa12ec4937c34d15f34f90d4e Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Sun, 31 Dec 2023 20:00:16 +0100 Subject: [PATCH 1/3] Use information of operand value to determine type for java locals This PR uses the already existing information of the operand's value type to determine the type for java locals. Previously, most of the stack variables were of "unknown" type . Fixes #635 --- .../sootup/java/bytecode/frontend/AsmMethodSource.java | 7 ++++++- .../main/java/sootup/java/bytecode/frontend/Operand.java | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java index 3c84cbaa119..adee45052ad 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java @@ -291,9 +291,14 @@ void setStmt(@Nonnull AbstractInsnNode insn, @Nonnull Stmt stmt) { @Nonnull Local newStackLocal() { + return newStackLocal(UnknownType.getInstance()); + } + + @Nonnull + Local newStackLocal(Type type) { int idx = nextLocal++; JavaLocal l = - JavaJimple.newLocal("$stack" + idx, UnknownType.getInstance(), Collections.emptyList()); + JavaJimple.newLocal("$stack" + idx, type, Collections.emptyList()); locals.set(idx, l); return l; } diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/Operand.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/Operand.java index 5f185ce9224..a2ef2b58853 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/Operand.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/Operand.java @@ -67,7 +67,7 @@ class Operand { Local getOrAssignValueToStackLocal() { if (stackLocal == null) { - changeStackLocal(methodSource.newStackLocal()); + changeStackLocal(methodSource.newStackLocal(value.getType())); } return stackLocal; From 5f72ac96f3cb230c438d038975c88ba2acc86d23 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Sun, 31 Dec 2023 22:05:01 +0100 Subject: [PATCH 2/3] Getting rid of more unknowns --- .../sootup/java/bytecode/frontend/AsmMethodSource.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java index adee45052ad..aaa17475999 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java @@ -252,13 +252,18 @@ private Object resolveAnnotationsInDefaultValue(Object a) { @Nonnull private JavaLocal getOrCreateLocal(int idx) { + return getOrCreateLocal(idx, null); + } + + @Nonnull + private JavaLocal getOrCreateLocal(int idx, @Nullable Type typeHint) { if (idx >= maxLocals) { throw new IllegalArgumentException("Invalid local index: " + idx); } JavaLocal local = locals.get(idx); if (local == null) { String name = determineLocalName(idx, false); // FIXME: isField - local = JavaJimple.newLocal(name, UnknownType.getInstance(), Collections.emptyList()); + local = JavaJimple.newLocal(name, typeHint != null ? typeHint : UnknownType.getInstance(), Collections.emptyList()); locals.set(idx, local); } return local; @@ -1218,7 +1223,7 @@ private void convertVarStoreInsn(@Nonnull VarInsnNode insn) { OperandMerging merging = operandStack.getOrCreateMerging(insn); Operand opr = dword ? operandStack.popDual() : operandStack.pop(); merging.mergeInputs(opr); - Local local = getOrCreateLocal(insn.var); + Local local = getOrCreateLocal(insn.var, opr.value.getType()); AbstractDefinitionStmt as; if (opr.stackLocal == null || opr.stackLocal == local) { // Can skip creating a new stack local for the operand From 08f1d330d452981c8e2836e8aa7d36f57c2f3aa8 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Tue, 2 Jan 2024 14:31:20 +0100 Subject: [PATCH 3/3] Type of an JArrayRef is now the element type of its base --- .../java/sootup/core/jimple/common/ref/JArrayRef.java | 9 ++++++++- .../interceptors/typeresolving/CastCounterTest.java | 5 +++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/jimple/common/ref/JArrayRef.java b/sootup.core/src/main/java/sootup/core/jimple/common/ref/JArrayRef.java index 43e4d641590..7cd15bb3fdd 100644 --- a/sootup.core/src/main/java/sootup/core/jimple/common/ref/JArrayRef.java +++ b/sootup.core/src/main/java/sootup/core/jimple/common/ref/JArrayRef.java @@ -27,7 +27,9 @@ import javax.annotation.Nonnull; import sootup.core.jimple.basic.*; import sootup.core.jimple.visitor.RefVisitor; +import sootup.core.types.ArrayType; import sootup.core.types.Type; +import sootup.core.types.UnknownType; import sootup.core.util.Copyable; import sootup.core.util.printer.StmtPrinter; @@ -88,7 +90,12 @@ public List getUses() { @Override @Nonnull public Type getType() { - return base.getType(); + Type baseType = base.getType(); + if(baseType instanceof ArrayType) { + return ((ArrayType)baseType).getElementType(); + } else { + return UnknownType.getInstance(); + } } @Override diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/interceptors/typeresolving/CastCounterTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/interceptors/typeresolving/CastCounterTest.java index f6f4e1482aa..e11df897308 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/interceptors/typeresolving/CastCounterTest.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/interceptors/typeresolving/CastCounterTest.java @@ -149,8 +149,9 @@ public void testAssignStmtWithNewCasts() { Assert.assertEquals( Stream.of( "CastCounterDemos $l0", - "Super1[] $#l0, $#l1", - "unknown $l1, $l2, $stack3", + "Sub1 $stack3", + "Super1 $l2", + "Super1[] $#l0, $#l1, $l1", "$l0 := @this: CastCounterDemos", "$l1 = newarray (Super1)[10]", "$stack3 = new Sub1",