Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid StackOverflowErrors and empty phi errors #155

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/main/kotlin/de/sirywell/handlehints/dfa/SsaAnalyzer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ class SsaAnalyzer(private val controlFlow: ControlFlow, val typeData: TypeData)
} else if (value is Phi) {
val type = value.blockToValue.values
.flatMap { if (it is Holder) listOf(it.value) else resolvePhi(it as Phi) }
.reduce { acc, mhType -> join(acc, mhType) }
.reduceOrNull { acc, mhType -> join(acc, mhType) }
typeData[element] = type
?: topForType(instruction.variable.type, instruction.variable)
} else {
typeData[element] = typeData[instruction.variable] ?: return
}
Expand All @@ -72,10 +73,17 @@ class SsaAnalyzer(private val controlFlow: ControlFlow, val typeData: TypeData)
@Suppress("UnstableApiUsage")
private fun isUnstableVariable(element: PsiReferenceExpression, variable: PsiVariable): Boolean {
// always assume static final variables are stable, no matter how they are referenced
return !(variable.hasModifier(JvmModifier.STATIC) && variable.hasModifier(JvmModifier.FINAL))
// otherwise, if it has a qualifier, it must be 'this' to be stable
&& element.qualifierExpression != null
&& element.qualifierExpression !is PsiThisExpression
if (variable.hasModifier(JvmModifier.STATIC) && variable.hasModifier(JvmModifier.FINAL)) {
return false
}
if (variable is PsiField) {
if (variable.hasModifier(JvmModifier.FINAL)) {
// can not be changed, and we record the value in constructors
return element.qualifierExpression != null && element.qualifierExpression !is PsiThisExpression
}
return true
}
return variable !is PsiLocalVariable && variable !is PsiParameter
}

private fun join(first: TypeLatticeElement<*>, second: TypeLatticeElement<*>): TypeLatticeElement<*> {
Expand All @@ -91,12 +99,19 @@ class SsaAnalyzer(private val controlFlow: ControlFlow, val typeData: TypeData)
throw AssertionError("unexpected join: $first - $second")
}

private fun <T> resolvePhi(phi: Phi<T>, mut: MutableList<T> = mutableListOf()): List<T> {
private fun <T> resolvePhi(
phi: Phi<T>,
mut: MutableList<T> = mutableListOf(),
seen: MutableSet<Phi<T>> = mutableSetOf() // avoid StackOverflowError
): List<T> {
if (!seen.add(phi)) {
return mut.toList()
}
phi.blockToValue.values.forEach {
if (it is Holder) {
mut.add(it.value)
} else {
resolvePhi(it as Phi<T>, mut)
resolvePhi(it as Phi<T>, mut, seen)
}
}
return mut.toList()
Expand Down Expand Up @@ -517,17 +532,19 @@ class SsaAnalyzer(private val controlFlow: ControlFlow, val typeData: TypeData)
expression: PsiMethodCallExpression,
arguments: List<PsiExpression>,
block: Block
) : TypeLatticeElement<*>? {
): TypeLatticeElement<*>? {
val qualifier = expression.methodExpression.qualifierExpression
return when (expression.methodName) {
"withInvokeBehavior" -> {
if (arguments.isNotEmpty() || qualifier == null) noMatch()
else varHandleHelper.withInvokeBehavior(qualifier, block)
}

"withInvokeExactBehavior" -> {
if (arguments.isNotEmpty() || qualifier == null) noMatch()
else varHandleHelper.withInvokeExactBehavior(qualifier, block)
}

else -> noMatch()
}
}
Expand Down Expand Up @@ -801,10 +818,12 @@ class SsaAnalyzer(private val controlFlow: ControlFlow, val typeData: TypeData)
if (arguments.size != 2) return noMatch()
methodHandlesInitializer.byteArrayViewVarHandle(arguments[0], arguments[1])
}

"byteBufferViewVarHandle" -> {
if (arguments.size != 2) return noMatch()
methodHandlesInitializer.byteBufferViewVarHandle(arguments[0], arguments[1])
}

"classData",
"classDataAt",
"lookup",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ class SsaConstruction<T>(private val controlFlow: ControlFlow) {
fun appendOperand(value: Value<T>, block: Block) {
blockToValue[block] = value
}

override fun hashCode(): Int {
return System.identityHashCode(this)
}

override fun equals(other: Any?): Boolean {
return this === other
}
}

data class Holder<T>(val value: T) : Value<T>
Expand Down
2 changes: 2 additions & 0 deletions src/test/testData/FinalFields.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class FinalFields {
}

<info descr="(⊤)int">private final MethodType methodType;</info>
private MethodType mutableMethodType;

FinalFields() {
<info descr="(String)int">this.methodType = <info descr="(String)int">MethodType.methodType(int.class, String.class)</info></info>;
Expand All @@ -39,5 +40,6 @@ void m() {
<info descr="(double)⊤">MethodHandle ms = <info descr="(double)⊤">MethodHandles.empty(METHOD_TYPE)</info>;</info>

<info descr="(⊤)int">MethodHandle mn = <info descr="(⊤)int">MethodHandles.empty(this.methodType)</info>;</info>
<info descr="⊤">MethodType mmt = mutableMethodType;</info>
}
}
Loading