Skip to content

Commit f761e57

Browse files
authored
Merge pull request #10591 from tamasvajk/kotlin-unbound-symbol
Kotlin: Log error when unbound symbol is found
2 parents e0c68c3 + 463173e commit f761e57

File tree

7 files changed

+14
-103
lines changed

7 files changed

+14
-103
lines changed

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.github.codeql
33
import com.github.codeql.comments.CommentExtractor
44
import com.github.codeql.utils.*
55
import com.github.codeql.utils.versions.functionN
6-
import com.github.codeql.utils.versions.getIrStubFromDescriptor
76
import com.github.codeql.utils.versions.isUnderscoreParameter
87
import com.semmle.extractor.java.OdasaOutput
98
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
@@ -1788,7 +1787,8 @@ open class KotlinFileExtractor(
17881787

17891788
private fun extractCall(c: IrCall, callable: Label<out DbCallable>, stmtExprParent: StmtExprParent) {
17901789
with("call", c) {
1791-
val target = tryReplaceSyntheticFunction(c.symbol.owner)
1790+
val owner = getBoundSymbolOwner(c.symbol, c) ?: return
1791+
val target = tryReplaceSyntheticFunction(owner)
17921792

17931793
// The vast majority of types of call want an expr context, so make one available lazily:
17941794
val exprParent by lazy {
@@ -2965,15 +2965,7 @@ open class KotlinFileExtractor(
29652965
tw.writeCallableEnclosingExpr(id, callable)
29662966
tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt)
29672967

2968-
val owner = if (e.symbol.isBound) {
2969-
e.symbol.owner
2970-
}
2971-
else {
2972-
logger.warnElement("Unbound enum value, trying to use enum entry stub from descriptor", e)
2973-
2974-
@OptIn(ObsoleteDescriptorBasedAPI::class)
2975-
getIrStubFromDescriptor() { it.generateEnumEntryStub(e.symbol.descriptor) }
2976-
} ?: return
2968+
val owner = getBoundSymbolOwner(e.symbol, e) ?: return
29772969

29782970
val vId = useEnumEntry(owner)
29792971
tw.writeVariableBinding(id, vId)
@@ -3150,15 +3142,7 @@ open class KotlinFileExtractor(
31503142
// automatically-generated `public static final MyObject INSTANCE`
31513143
// field that we are accessing here.
31523144
val exprParent = parent.expr(e, callable)
3153-
val c = if (e.symbol.isBound) {
3154-
e.symbol.owner
3155-
}
3156-
else {
3157-
logger.warnElement("Unbound object value, trying to use class stub from descriptor", e)
3158-
3159-
@OptIn(ObsoleteDescriptorBasedAPI::class)
3160-
getIrStubFromDescriptor() { it.generateClassStub(e.symbol.descriptor) }
3161-
} ?: return
3145+
val c = getBoundSymbolOwner(e.symbol, e) ?: return
31623146

31633147
val instance = if (c.isCompanion) useCompanionObjectClassInstance(c) else useObjectClassInstance(c)
31643148

@@ -3271,6 +3255,15 @@ open class KotlinFileExtractor(
32713255
}
32723256
}
32733257

3258+
private inline fun <D: DeclarationDescriptor, reified B: IrSymbolOwner> getBoundSymbolOwner(symbol: IrBindableSymbol<D, B>, e: IrExpression): B? {
3259+
if (symbol.isBound) {
3260+
return symbol.owner
3261+
}
3262+
3263+
logger.errorElement("Unbound symbol found, skipping extraction of expression", e)
3264+
return null
3265+
}
3266+
32743267
private fun extractSuperAccess(irType: IrType, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int, enclosingStmt: Label<out DbStmt>, locId: Label<out DbLocation>) =
32753268
tw.getFreshIdLabel<DbSuperaccess>().also {
32763269
val type = useType(irType)

java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_4_32/Descriptors.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.

java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_20/Descriptors.kt

Lines changed: 0 additions & 18 deletions
This file was deleted.

java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_6_0/Descriptors.kt

Lines changed: 0 additions & 18 deletions
This file was deleted.

java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_7_0/Descriptors.kt

Lines changed: 0 additions & 27 deletions
This file was deleted.

java/ql/integration-tests/posix-only/kotlin/gradle_kotlinx_serialization/PrintAst.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ app/src/main/kotlin/testProject/App.kt:
183183
# 7| 0: [ReturnStmt] return ...
184184
# 7| 0: [ArrayCreationExpr] new KSerializer<?>[]
185185
# 7| -2: [ArrayInit] {...}
186-
# 7| 0: [VarAccess] INSTANCE
187-
# 7| 1: [VarAccess] INSTANCE
188186
# 7| -1: [TypeAccess] KSerializer<?>
189187
# 7| 0: [IntegerLiteral] 2
190188
# 0| 3: [Method] deserialize
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| CodeQL Kotlin extractor | 2 | | Unbound object value, trying to use class stub from descriptor | app/src/main/kotlin/testProject/App.kt:7:1:8:55 | app/src/main/kotlin/testProject/App.kt:7:1:8:55 |
1+
| CodeQL Kotlin extractor | 5 | | Unbound symbol found, skipping extraction of expression | app/src/main/kotlin/testProject/App.kt:7:1:8:55 | app/src/main/kotlin/testProject/App.kt:7:1:8:55 |

0 commit comments

Comments
 (0)