Merge pull request #10591 from tamasvajk/kotlin-unbound-symbol

Kotlin: Log error when unbound symbol is found
This commit is contained in:
Tamás Vajk 2022-09-28 14:45:13 +02:00 коммит произвёл GitHub
Родитель e0c68c3a27 463173eae4
Коммит f761e57365
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 14 добавлений и 103 удалений

Просмотреть файл

@ -3,7 +3,6 @@ package com.github.codeql
import com.github.codeql.comments.CommentExtractor
import com.github.codeql.utils.*
import com.github.codeql.utils.versions.functionN
import com.github.codeql.utils.versions.getIrStubFromDescriptor
import com.github.codeql.utils.versions.isUnderscoreParameter
import com.semmle.extractor.java.OdasaOutput
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
@ -1788,7 +1787,8 @@ open class KotlinFileExtractor(
private fun extractCall(c: IrCall, callable: Label<out DbCallable>, stmtExprParent: StmtExprParent) {
with("call", c) {
val target = tryReplaceSyntheticFunction(c.symbol.owner)
val owner = getBoundSymbolOwner(c.symbol, c) ?: return
val target = tryReplaceSyntheticFunction(owner)
// The vast majority of types of call want an expr context, so make one available lazily:
val exprParent by lazy {
@ -2965,15 +2965,7 @@ open class KotlinFileExtractor(
tw.writeCallableEnclosingExpr(id, callable)
tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt)
val owner = if (e.symbol.isBound) {
e.symbol.owner
}
else {
logger.warnElement("Unbound enum value, trying to use enum entry stub from descriptor", e)
@OptIn(ObsoleteDescriptorBasedAPI::class)
getIrStubFromDescriptor() { it.generateEnumEntryStub(e.symbol.descriptor) }
} ?: return
val owner = getBoundSymbolOwner(e.symbol, e) ?: return
val vId = useEnumEntry(owner)
tw.writeVariableBinding(id, vId)
@ -3150,15 +3142,7 @@ open class KotlinFileExtractor(
// automatically-generated `public static final MyObject INSTANCE`
// field that we are accessing here.
val exprParent = parent.expr(e, callable)
val c = if (e.symbol.isBound) {
e.symbol.owner
}
else {
logger.warnElement("Unbound object value, trying to use class stub from descriptor", e)
@OptIn(ObsoleteDescriptorBasedAPI::class)
getIrStubFromDescriptor() { it.generateClassStub(e.symbol.descriptor) }
} ?: return
val c = getBoundSymbolOwner(e.symbol, e) ?: return
val instance = if (c.isCompanion) useCompanionObjectClassInstance(c) else useObjectClassInstance(c)
@ -3271,6 +3255,15 @@ open class KotlinFileExtractor(
}
}
private inline fun <D: DeclarationDescriptor, reified B: IrSymbolOwner> getBoundSymbolOwner(symbol: IrBindableSymbol<D, B>, e: IrExpression): B? {
if (symbol.isBound) {
return symbol.owner
}
logger.errorElement("Unbound symbol found, skipping extraction of expression", e)
return null
}
private fun extractSuperAccess(irType: IrType, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int, enclosingStmt: Label<out DbStmt>, locId: Label<out DbLocation>) =
tw.getFreshIdLabel<DbSuperaccess>().also {
val type = useType(irType)

Просмотреть файл

@ -1,17 +0,0 @@
package com.github.codeql.utils.versions
import com.github.codeql.KotlinUsesExtractor
import com.github.codeql.Severity
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
import org.jetbrains.kotlin.ir.util.SymbolTable
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun <TIrStub> KotlinUsesExtractor.getIrStubFromDescriptor(generateStub: (DeclarationStubGenerator) -> TIrStub) : TIrStub? =
(pluginContext.symbolTable as? SymbolTable) ?.let {
val stubGenerator = DeclarationStubGenerator(pluginContext.moduleDescriptor, it, pluginContext.languageVersionSettings)
generateStub(stubGenerator)
} ?: run {
logger.error("Plugin context has no symbol table, couldn't get IR stub")
null
}

Просмотреть файл

@ -1,18 +0,0 @@
package com.github.codeql.utils.versions
import com.github.codeql.KotlinUsesExtractor
import com.github.codeql.Severity
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.psi2ir.generators.DeclarationStubGeneratorImpl
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun <TIrStub> KotlinUsesExtractor.getIrStubFromDescriptor(generateStub: (DeclarationStubGenerator) -> TIrStub) : TIrStub? =
(pluginContext.symbolTable as? SymbolTable) ?.let {
val stubGenerator = DeclarationStubGeneratorImpl(pluginContext.moduleDescriptor, it, pluginContext.languageVersionSettings)
generateStub(stubGenerator)
} ?: run {
logger.error("Plugin context has no symbol table, couldn't get IR stub")
null
}

Просмотреть файл

@ -1,18 +0,0 @@
package com.github.codeql.utils.versions
import com.github.codeql.KotlinUsesExtractor
import com.github.codeql.Severity
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.psi2ir.generators.DeclarationStubGeneratorImpl
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun <TIrStub> KotlinUsesExtractor.getIrStubFromDescriptor(generateStub: (DeclarationStubGenerator) -> TIrStub) : TIrStub? =
(pluginContext.symbolTable as? SymbolTable) ?.let {
val stubGenerator = DeclarationStubGeneratorImpl(pluginContext.moduleDescriptor, it, pluginContext.irBuiltIns)
generateStub(stubGenerator)
} ?: run {
logger.error("Plugin context has no symbol table, couldn't get IR stub")
null
}

Просмотреть файл

@ -1,27 +0,0 @@
package com.github.codeql.utils.versions
import com.github.codeql.KotlinUsesExtractor
import org.jetbrains.kotlin.backend.common.serialization.DescriptorByIdSignatureFinderImpl
import org.jetbrains.kotlin.idea.MainFunctionDetector
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmDescriptorMangler
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.psi2ir.generators.DeclarationStubGeneratorImpl
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun <TIrStub> KotlinUsesExtractor.getIrStubFromDescriptor(generateStub: (DeclarationStubGenerator) -> TIrStub) : TIrStub? =
(pluginContext.symbolTable as? SymbolTable) ?.let {
// Copying the construction seen in JvmIrLinker.kt
val mangler = JvmDescriptorMangler(MainFunctionDetector(pluginContext.bindingContext, pluginContext.languageVersionSettings))
val descriptorFinder = DescriptorByIdSignatureFinderImpl(
pluginContext.moduleDescriptor,
mangler,
DescriptorByIdSignatureFinderImpl.LookupMode.MODULE_ONLY
)
val stubGenerator = DeclarationStubGeneratorImpl(pluginContext.moduleDescriptor, it, pluginContext.irBuiltIns, descriptorFinder)
generateStub(stubGenerator)
} ?: run {
logger.error("Plugin context has no symbol table, couldn't get IR stub")
null
}

Просмотреть файл

@ -183,8 +183,6 @@ app/src/main/kotlin/testProject/App.kt:
# 7| 0: [ReturnStmt] return ...
# 7| 0: [ArrayCreationExpr] new KSerializer<?>[]
# 7| -2: [ArrayInit] {...}
# 7| 0: [VarAccess] INSTANCE
# 7| 1: [VarAccess] INSTANCE
# 7| -1: [TypeAccess] KSerializer<?>
# 7| 0: [IntegerLiteral] 2
# 0| 3: [Method] deserialize

Просмотреть файл

@ -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 |
| 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 |