Skip to content

Commit 7daba0b

Browse files
authored
Merge pull request #9122 from smowton/smowton/admin/update-kotlin
Kotlin: Apply changes since #9109 branched away from kotlin-main
2 parents e91a51a + 5ec9390 commit 7daba0b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+552
-179
lines changed

java/kotlin-extractor/src/main/kotlin/ExternalDeclExtractor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ class ExternalDeclExtractor(val logger: FileLogger, val invocationTrapFile: Stri
9393
ftw.writeHasLocation(ftw.fileId, ftw.getWholeFileLocation())
9494
ftw.writeCupackage(ftw.fileId, pkgId)
9595

96-
fileExtractor.extractClassSource(irDecl, !irDecl.isFileClass, false)
96+
fileExtractor.extractClassSource(irDecl, extractDeclarations = !irDecl.isFileClass, extractStaticInitializer = false, extractPrivateMembers = false, extractFunctionBodies = false)
9797
} else {
98-
fileExtractor.extractDeclaration(irDecl)
98+
fileExtractor.extractDeclaration(irDecl, extractPrivateMembers = false, extractFunctionBodies = false)
9999
}
100100
}
101101

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 67 additions & 32 deletions
Large diffs are not rendered by default.

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 148 additions & 40 deletions
Large diffs are not rendered by default.

java/kotlin-extractor/src/main/kotlin/utils/ClassNames.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import org.jetbrains.kotlin.load.kotlin.VirtualFileKotlinClass
77
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinarySourceElement
88

99
import com.intellij.openapi.vfs.VirtualFile
10+
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
1011
import org.jetbrains.kotlin.ir.declarations.*
12+
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
1113
import org.jetbrains.kotlin.ir.util.parentClassOrNull
1214
import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource
1315

@@ -84,4 +86,7 @@ fun getContainingClassOrSelf(decl: IrDeclaration): IrClass? {
8486
is IrClass -> decl
8587
else -> decl.parentClassOrNull
8688
}
87-
}
89+
}
90+
91+
fun getJavaEquivalentClassId(c: IrClass) =
92+
c.fqNameWhenAvailable?.toUnsafe()?.let { JavaToKotlinClassMap.mapKotlinToJava(it) }

java/kotlin-extractor/src/main/kotlin/utils/TypeSubstitution.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.codeql.utils
22

33
import com.github.codeql.KotlinUsesExtractor
4+
import com.github.codeql.getJavaEquivalentClassId
45
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
56
import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor
67
import org.jetbrains.kotlin.descriptors.ClassKind
@@ -19,6 +20,7 @@ import org.jetbrains.kotlin.ir.types.*
1920
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
2021
import org.jetbrains.kotlin.ir.types.impl.IrStarProjectionImpl
2122
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
23+
import org.jetbrains.kotlin.ir.util.classId
2224
import org.jetbrains.kotlin.ir.util.constructedClassType
2325
import org.jetbrains.kotlin.ir.util.constructors
2426
import org.jetbrains.kotlin.ir.util.parentAsClass
@@ -195,13 +197,25 @@ fun IrTypeArgument.withQuestionMark(b: Boolean): IrTypeArgument =
195197

196198
typealias TypeSubstitution = (IrType, KotlinUsesExtractor.TypeContext, IrPluginContext) -> IrType
197199

200+
fun matchingTypeParameters(l: IrTypeParameter?, r: IrTypeParameter): Boolean {
201+
if (l === r)
202+
return true
203+
if (l == null)
204+
return false
205+
// Special case: match List's E and MutableList's E, for example, because in the JVM lowering they will map to the same thing.
206+
val lParent = l.parent as? IrClass ?: return false
207+
val rParent = r.parent as? IrClass ?: return false
208+
val lJavaId = getJavaEquivalentClassId(lParent) ?: lParent.classId
209+
return (getJavaEquivalentClassId(rParent) ?: rParent.classId) == lJavaId && l.name == r.name
210+
}
211+
198212
// Returns true if type is C<T1, T2, ...> where C is declared `class C<T1, T2, ...> { ... }`
199213
fun isUnspecialised(paramsContainer: IrTypeParametersContainer, args: List<IrTypeArgument>): Boolean {
200214
val unspecialisedHere = paramsContainer.typeParameters.zip(args).all { paramAndArg ->
201215
(paramAndArg.second as? IrTypeProjection)?.let {
202216
// Type arg refers to the class' own type parameter?
203217
it.variance == Variance.INVARIANT &&
204-
it.type.classifierOrNull?.owner === paramAndArg.first
218+
matchingTypeParameters(it.type.classifierOrNull?.owner as? IrTypeParameter, paramAndArg.first)
205219
} ?: false
206220
}
207221
val remainingArgs = args.drop(paramsContainer.typeParameters.size)

java/ql/lib/semmle/code/java/dataflow/ExternalFlow.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ private module Frameworks {
143143
private import semmle.code.java.frameworks.JMS
144144
private import semmle.code.java.frameworks.RabbitMQ
145145
private import semmle.code.java.regex.RegexFlowModels
146+
private import semmle.code.java.frameworks.KotlinStdLib
146147
}
147148

148149
private predicate sourceModelCsv(string row) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** Definitions of taint steps in the KotlinStdLib framework */
2+
3+
import java
4+
private import semmle.code.java.dataflow.ExternalFlow
5+
6+
private class KotlinStdLibSummaryCsv extends SummaryModelCsv {
7+
override predicate row(string row) {
8+
row =
9+
"kotlin.jvm.internal;ArrayIteratorKt;false;iterator;(Object[]);;Argument[0].ArrayElement;ReturnValue.Element;value"
10+
}
11+
}

java/ql/test/kotlin/library-tests/classes/PrintAst.expected

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ generic_anonymous.kt:
578578
# 3| 1: [ExprStmt] <Expr>;
579579
# 3| 0: [ClassInstanceExpr] new (...)
580580
# 3| -3: [TypeAccess] Object
581-
# 3| 5: [Method] getX
581+
# 3| 5: [Method] getX$private
582582
# 3| 3: [TypeAccess] new Object(...) { ... }
583583
# 3| 0: [TypeAccess] T
584584
# 3| 5: [BlockStmt] { ... }
@@ -590,8 +590,69 @@ generic_anonymous.kt:
590590
# 7| 5: [BlockStmt] { ... }
591591
# 7| 0: [ReturnStmt] return ...
592592
# 7| 0: [MethodAccess] getMember(...)
593-
# 7| -1: [MethodAccess] getX(...)
593+
# 7| -1: [MethodAccess] getX$private(...)
594594
# 7| -1: [ThisAccess] this
595+
localClassField.kt:
596+
# 0| [CompilationUnit] localClassField
597+
# 1| 1: [Class] A
598+
# 1| 1: [Constructor] A
599+
# 1| 5: [BlockStmt] { ... }
600+
# 1| 0: [SuperConstructorInvocationStmt] super(...)
601+
# 1| 1: [BlockStmt] { ... }
602+
# 2| 0: [ExprStmt] <Expr>;
603+
# 2| 0: [KtInitializerAssignExpr] ...=...
604+
# 2| 0: [VarAccess] x
605+
# 7| 1: [ExprStmt] <Expr>;
606+
# 7| 0: [KtInitializerAssignExpr] ...=...
607+
# 7| 0: [VarAccess] y
608+
# 2| 2: [Method] getX
609+
# 2| 3: [TypeAccess] Object
610+
# 2| 5: [BlockStmt] { ... }
611+
# 2| 0: [ReturnStmt] return ...
612+
# 2| 0: [VarAccess] this.x
613+
# 2| -1: [ThisAccess] this
614+
# 2| 2: [FieldDeclaration] Object x;
615+
# 2| -1: [TypeAccess] Object
616+
# 2| 0: [WhenExpr] when ...
617+
# 2| 0: [WhenBranch] ... -> ...
618+
# 2| 0: [BooleanLiteral] true
619+
# 2| 1: [BlockStmt] { ... }
620+
# 3| 0: [LocalTypeDeclStmt] class ...
621+
# 3| 0: [LocalClass] L
622+
# 3| 1: [Constructor] L
623+
# 3| 5: [BlockStmt] { ... }
624+
# 3| 0: [SuperConstructorInvocationStmt] super(...)
625+
# 3| 1: [BlockStmt] { ... }
626+
# 4| 1: [ExprStmt] <Expr>;
627+
# 4| 0: [ClassInstanceExpr] new L(...)
628+
# 4| -3: [TypeAccess] L
629+
# 2| 1: [WhenBranch] ... -> ...
630+
# 2| 0: [BooleanLiteral] true
631+
# 5| 1: [BlockStmt] { ... }
632+
# 7| 4: [Method] getY
633+
# 7| 3: [TypeAccess] Object
634+
# 7| 5: [BlockStmt] { ... }
635+
# 7| 0: [ReturnStmt] return ...
636+
# 7| 0: [VarAccess] this.y
637+
# 7| -1: [ThisAccess] this
638+
# 7| 4: [FieldDeclaration] Object y;
639+
# 7| -1: [TypeAccess] Object
640+
# 7| 0: [WhenExpr] when ...
641+
# 7| 0: [WhenBranch] ... -> ...
642+
# 7| 0: [BooleanLiteral] true
643+
# 7| 1: [BlockStmt] { ... }
644+
# 8| 0: [LocalTypeDeclStmt] class ...
645+
# 8| 0: [LocalClass] L
646+
# 8| 1: [Constructor] L
647+
# 8| 5: [BlockStmt] { ... }
648+
# 8| 0: [SuperConstructorInvocationStmt] super(...)
649+
# 8| 1: [BlockStmt] { ... }
650+
# 9| 1: [ExprStmt] <Expr>;
651+
# 9| 0: [ClassInstanceExpr] new L(...)
652+
# 9| -3: [TypeAccess] L
653+
# 7| 1: [WhenBranch] ... -> ...
654+
# 7| 0: [BooleanLiteral] true
655+
# 10| 1: [BlockStmt] { ... }
595656
local_anonymous.kt:
596657
# 0| [CompilationUnit] local_anonymous
597658
# 3| 1: [Class] Class1

java/ql/test/kotlin/library-tests/classes/classes.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
| generic_anonymous.kt:0:0:0:0 | Generic_anonymousKt | Generic_anonymousKt | final, public |
3838
| generic_anonymous.kt:1:1:9:1 | Generic | Generic | final, private |
3939
| generic_anonymous.kt:3:19:5:3 | new Object(...) { ... } | <anonymous class> | final, private |
40+
| localClassField.kt:1:1:11:1 | A | A | final, public |
41+
| localClassField.kt:3:9:3:19 | L | A$L | final, private |
42+
| localClassField.kt:8:9:8:19 | L | A$L | final, private |
4043
| local_anonymous.kt:3:1:36:1 | Class1 | LocalAnonymous.Class1 | final, public |
4144
| local_anonymous.kt:5:16:7:9 | new Object(...) { ... } | <anonymous class> | final, private |
4245
| local_anonymous.kt:11:9:11:24 | | Class1$ | final, private |

java/ql/test/kotlin/library-tests/classes/ctorCalls.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ superCall
3636
| classes.kt:129:17:131:17 | super(...) |
3737
| generic_anonymous.kt:1:1:9:1 | super(...) |
3838
| generic_anonymous.kt:3:19:5:3 | super(...) |
39+
| localClassField.kt:1:1:11:1 | super(...) |
40+
| localClassField.kt:3:9:3:19 | super(...) |
41+
| localClassField.kt:8:9:8:19 | super(...) |
3942
| local_anonymous.kt:3:1:36:1 | super(...) |
4043
| local_anonymous.kt:5:16:7:9 | super(...) |
4144
| local_anonymous.kt:11:9:11:24 | super(...) |

0 commit comments

Comments
 (0)