When extracting a call to an inherited method, substitute the callee's type parameters appropriately.

This commit is contained in:
Chris Smowton
2022-01-26 18:28:56 +00:00
committed by Ian Lynagh
parent 8c9c37ca47
commit 4d408159aa
8 changed files with 51 additions and 28 deletions

View File

@@ -4,9 +4,11 @@ import com.github.codeql.comments.CommentExtractor
import com.github.codeql.utils.TypeSubstitution
import com.github.codeql.utils.versions.functionN
import com.github.codeql.utils.substituteTypeAndArguments
import com.github.codeql.utils.substituteTypeArguments
import com.github.codeql.utils.toRawType
import com.semmle.extractor.java.OdasaOutput
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.backend.common.pop
import org.jetbrains.kotlin.builtins.functions.BuiltInFunctionArity
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.java.JavaVisibilities
@@ -927,6 +929,42 @@ open class KotlinFileExtractor(
}
}
private fun getDeclaringTypeArguments(callTarget: IrFunction, receiverType: IrSimpleType): List<IrTypeArgument> {
val declaringType = callTarget.parentAsClass
val receiverClass = receiverType.classifier.owner as? IrClass ?: return listOf()
val ancestorTypes = ArrayList<IrSimpleType>()
// Populate ancestorTypes with the path from receiverType's class to its ancestor, callTarget's declaring type.
fun walkFrom(c: IrClass): Boolean {
if(declaringType == c)
return true
else {
c.superTypes.forEach {
val ancestorClass = (it as? IrSimpleType)?.classifier?.owner as? IrClass ?: return false
ancestorTypes.add(it)
if (walkFrom(ancestorClass))
return true
else
ancestorTypes.pop()
}
return false
}
}
// If a path was found, repeatedly substitute types to get the corresponding specialisation of that ancestor.
return if (!walkFrom(receiverClass)) {
logger.warnElement(Severity.ErrorSevere, "Failed to find a class declaring ${callTarget.name}", callTarget)
listOf()
} else {
var subbedType = receiverType
ancestorTypes.forEach {
val thisClass = subbedType.classifier.owner as IrClass
subbedType = it.substituteTypeArguments(thisClass.typeParameters, subbedType.arguments) as IrSimpleType
}
subbedType.arguments
}
}
fun extractCall(c: IrCall, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int, enclosingStmt: Label<out DbStmt>) {
with("call", c) {
fun isFunction(pkgName: String, className: String, fName: String, hasQuestionMark: Boolean = false): Boolean {
@@ -1028,7 +1066,7 @@ open class KotlinFileExtractor(
val drType = dr?.type
val methodId =
if (drType != null && extractClassTypeArguments && drType is IrSimpleType && !isUnspecialised(drType))
useFunction<DbCallable>(callTarget, drType.arguments)
useFunction<DbCallable>(callTarget, getDeclaringTypeArguments(callTarget, drType))
else
useFunction<DbCallable>(callTarget)

View File

@@ -1 +1 @@
| A.java:4:19:4:29 | someFun(...) | OC$C.class:0:0:0:0 | someFun |
| A.java:4:19:4:29 | someFun(...) | OC$C.class:0:0:0:0 | someFun | OC$C.class:0:0:0:0 | C<D1,D2,E1,E2> | OC.class:0:0:0:0 | OC<F1,F2> |

View File

@@ -1,4 +1,4 @@
import java
from MethodAccess c
select c, c.getCallee()
select c, c.getCallee(), c.getCallee().getDeclaringType(), c.getCallee().getDeclaringType().(NestedType).getEnclosingType()

View File

@@ -1,17 +1,17 @@
class A {
fun foo(z: OB.B<E1, E2>) {
fun foo(z: OB<G1, G2>.B<E1, E2>) {
val foo = z.someFun()
}
}
class OB<S1, S2>: OC<F1, F2>() {
public class B<T1, T2>: OC.C<D1, D2, T1, T2>() {
public inner class B<T1, T2>: OC<F1, F2>.C<D1, D2, T1, T2>() {
}
}
open class OC<U1, U2> {
open class C<X1, X2, Y1, Y2> {
open inner class C<X1, X2, Y1, Y2> {
fun someFun(): Int {
return 5
}

View File

@@ -1,14 +1,3 @@
| A.kt:2:1:6:1 | <obinit>(...) | A.kt:2:1:6:1 | <obinit> |
| A.kt:4:21:4:29 | someFun(...) | A.kt:9:9:10:2 | someFun |
| A.kt:8:1:11:1 | <obinit>(...) | A.kt:8:1:11:1 | <obinit> |
| A.kt:9:2:10:2 | <obinit>(...) | A.kt:9:2:10:2 | <obinit> |
| A.kt:13:1:19:1 | <obinit>(...) | A.kt:13:1:19:1 | <obinit> |
| A.kt:14:2:18:2 | <obinit>(...) | A.kt:14:2:18:2 | <obinit> |
| A.kt:21:1:21:11 | <obinit>(...) | A.kt:21:1:21:11 | <obinit> |
| A.kt:22:1:22:11 | <obinit>(...) | A.kt:22:1:22:11 | <obinit> |
| A.kt:24:1:24:11 | <obinit>(...) | A.kt:24:1:24:11 | <obinit> |
| A.kt:25:1:25:11 | <obinit>(...) | A.kt:25:1:25:11 | <obinit> |
| A.kt:27:1:27:11 | <obinit>(...) | A.kt:27:1:27:11 | <obinit> |
| A.kt:28:1:28:11 | <obinit>(...) | A.kt:28:1:28:11 | <obinit> |
| A.kt:30:1:30:11 | <obinit>(...) | A.kt:30:1:30:11 | <obinit> |
| A.kt:31:1:31:11 | <obinit>(...) | A.kt:31:1:31:11 | <obinit> |
| A.kt:4:21:4:29 | someFun(...) | A.kt:15:6:17:6 | someFun | A.kt:14:2:18:2 | C<D1,D2,E1,E2> | A.kt:13:1:19:1 | OC<F1,F2> |
| A.kt:9:2:10:2 | <obinit>(...) | A.kt:9:2:10:2 | <obinit> | A.kt:9:2:10:2 | B | A.kt:8:1:11:1 | OB |
| A.kt:14:2:18:2 | <obinit>(...) | A.kt:14:2:18:2 | <obinit> | A.kt:14:2:18:2 | C | A.kt:13:1:19:1 | OC |

View File

@@ -1,4 +1,4 @@
import java
from MethodAccess c
select c, c.getCallee()
select c, c.getCallee(), c.getCallee().getDeclaringType(), c.getCallee().getDeclaringType().(NestedType).getEnclosingType()

View File

@@ -1,6 +1,2 @@
| A.kt:2:1:6:1 | <obinit>(...) | A.kt:2:1:6:1 | <obinit> |
| A.kt:4:21:4:29 | someFun(...) | file:///!unknown-binary-location/OB$B.class:0:0:0:0 | someFun |
| A.kt:8:1:8:11 | <obinit>(...) | A.kt:8:1:8:11 | <obinit> |
| A.kt:9:1:9:11 | <obinit>(...) | A.kt:9:1:9:11 | <obinit> |
| A.kt:11:1:11:11 | <obinit>(...) | A.kt:11:1:11:11 | <obinit> |
| A.kt:12:1:12:11 | <obinit>(...) | A.kt:12:1:12:11 | <obinit> |
| A.kt:4:21:4:29 | someFun(...) | file:///!unknown-binary-location/OC$C.class:0:0:0:0 | someFun | file:///!unknown-binary-location/OC$C.class:0:0:0:0 | C<D1,D2,E1,E2> | OC.class:0:0:0:0 | OC<F1,F2> |
| A.kt:4:21:4:29 | someFun(...) | file:///!unknown-binary-location/OC$C.class:0:0:0:0 | someFun | file:///!unknown-binary-location/OC$C.class:0:0:0:0 | C<D1,D2,E1,E2> | file:///!unknown-binary-location/OC.class:0:0:0:0 | OC<F1,F2> |

View File

@@ -1,4 +1,4 @@
import java
from MethodAccess c
select c, c.getCallee()
select c, c.getCallee(), c.getCallee().getDeclaringType(), c.getCallee().getDeclaringType().(NestedType).getEnclosingType()