Skip to content

Commit 4102540

Browse files
Bug with invocations of static interfaces methods fixed (#213)
* Bug with invocations of static interfaces methods fixed * renaming
1 parent 0bcb1e7 commit 4102540

File tree

7 files changed

+45
-12
lines changed

7 files changed

+45
-12
lines changed

jacodb-api/src/main/kotlin/org/jacodb/api/cfg/JcRawInst.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ data class JcRawStaticCallExpr(
762762
override val argumentTypes: List<TypeName>,
763763
override val returnType: TypeName,
764764
override val args: List<JcRawValue>,
765+
val isInterfaceMethodCall: Boolean = false,
765766
) : JcRawCallExpr {
766767
override fun toString(): String =
767768
"$declaringClass.$methodName${args.joinToString(prefix = "(", postfix = ")", separator = ", ")}"

jacodb-approximations/src/main/kotlin/org/jacodb/approximation/InstSubstitutorForApproximations.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ object InstSubstitutorForApproximations : JcRawInstVisitor<JcRawInst>, JcRawExpr
365365
methodName,
366366
argumentTypes.map { it.eliminateApproximation() },
367367
returnType.eliminateApproximation(),
368-
newArgs
368+
newArgs,
369+
isInterfaceMethodCall
369370
)
370371
}
371372
}

jacodb-core/src/main/kotlin/org/jacodb/impl/cfg/MethodNodeBuilder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ class MethodNodeBuilder(
266266
val trueTarget = label(inst.trueBranch)
267267
val falseTarget = label(inst.falseBranch)
268268
val cond = inst.condition
269-
var shouldReverse = false
270269
val (zeroValue, zeroCmpOpcode, defaultOpcode) = when (cond) {
271270
is JcRawEqExpr -> when {
272271
cond.lhv.typeName == PredefinedPrimitives.Null.typeName() -> Triple(
@@ -675,7 +674,8 @@ class MethodNodeBuilder(
675674
Opcodes.INVOKESTATIC,
676675
expr.declaringClass.jvmClassName,
677676
expr.methodName,
678-
expr.methodDesc
677+
expr.methodDesc,
678+
expr.isInterfaceMethodCall
679679
)
680680
)
681681
updateStackInfo(-expr.args.size)

jacodb-core/src/main/kotlin/org/jacodb/impl/cfg/RawInstListBuilder.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ import org.jacodb.impl.cfg.util.*
2525
import org.jacodb.impl.types.TypeNameImpl
2626
import org.objectweb.asm.*
2727
import org.objectweb.asm.tree.*
28-
import org.objectweb.asm.util.Printer
29-
import org.objectweb.asm.util.Textifier
30-
import org.objectweb.asm.util.TraceMethodVisitor
31-
import java.io.PrintWriter
32-
import java.io.StringWriter
3328
import java.util.*
3429

3530

@@ -1400,7 +1395,8 @@ class RawInstListBuilder(
14001395
lookupAssignment,
14011396
JcRawStringConstant(cst.name, STRING_CLASS.typeName()),
14021397
JcRawClassConstant(cst.descriptor.typeName(), CLASS_CLASS.typeName())
1403-
) + exprs
1398+
) + exprs,
1399+
methodHande.isInterface
14041400
)
14051401
}
14061402
}
@@ -1438,7 +1434,8 @@ class RawInstListBuilder(
14381434
methodName,
14391435
argTypes,
14401436
returnType,
1441-
args
1437+
args,
1438+
insnNode.itf
14421439
)
14431440

14441441
else -> {
@@ -1555,7 +1552,7 @@ class RawInstListBuilder(
15551552
variable,
15561553
pop(),
15571554
insnNode
1558-
)?.let { addInstruction(insnNode, it) }
1555+
).let { addInstruction(insnNode, it) }
15591556

15601557
in Opcodes.ILOAD..Opcodes.ALOAD -> {
15611558
push(local(variable))

jacodb-core/src/main/kotlin/org/jacodb/impl/cfg/util/ExprMapper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ class ExprMapper(val mapping: Map<JcRawExpr, JcRawExpr>) : JcRawInstVisitor<JcRa
364364
when (expr.args) {
365365
newArgs -> expr
366366
else -> JcRawStaticCallExpr(
367-
expr.declaringClass, expr.methodName, expr.argumentTypes, expr.returnType, newArgs
367+
expr.declaringClass, expr.methodName, expr.argumentTypes, expr.returnType, newArgs, expr.isInterfaceMethodCall
368368
)
369369
}
370370
}

jacodb-core/src/test/kotlin/org/jacodb/testing/cfg/InstructionsTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ class InstructionsTest : BaseInstructionsTest() {
265265
assertEquals("defaultMethod", callDefaultMethod.method.method.name)
266266
}
267267

268+
@Test
269+
fun `static interface method call`() {
270+
val clazz = cp.findClass<StaticInterfaceMethodCall>()
271+
272+
val javaClazz = testAndLoadClass(clazz)
273+
val method = javaClazz.methods.first { it.name == "callStaticInterfaceMethod" }
274+
val res = method.invoke(null)
275+
assertNull(res)
276+
}
268277

269278
@Test
270279
fun `condition in for should work`() {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2022 UnitTestBot contributors (utbot.org)
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jacodb.testing.cfg;
18+
19+
import java.util.function.Function;
20+
21+
public class StaticInterfaceMethodCall {
22+
public static void callStaticInterfaceMethod() {
23+
Function.identity();
24+
}
25+
}

0 commit comments

Comments
 (0)