Repository navigation
[TS] Execute declared getters and preserve source class metadata #649
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
Draft
CaelmBleidd
wants to merge
3
commits into
caelmbleidd/ts-declared-accessor-read-boundary
Choose a base branch
from
caelmbleidd/ts-execute-declared-getters
base: caelmbleidd/ts-declared-accessor-read-boundary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
usvm-ts/src/main/kotlin/org/usvm/machine/expr/DeclaredClassAccessor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package org.usvm.machine.expr | ||
|
|
||
| import org.jacodb.ets.model.EtsClass | ||
| import org.jacodb.ets.model.EtsClassCategory | ||
| import org.jacodb.ets.model.EtsClassType | ||
| import org.jacodb.ets.model.EtsFieldSignature | ||
| import org.jacodb.ets.model.EtsInstanceCallExpr | ||
| import org.jacodb.ets.model.EtsInstanceFieldRef | ||
| import org.jacodb.ets.model.EtsMethod | ||
| import org.jacodb.ets.utils.INSTANCE_INIT_METHOD_NAME | ||
| import org.usvm.UConcreteHeapRef | ||
| import org.usvm.UExpr | ||
| import org.usvm.UHeapRef | ||
| import org.usvm.api.typeStreamOf | ||
| import org.usvm.isAllocatedConcreteHeapRef | ||
| import org.usvm.isFalse | ||
| import org.usvm.machine.TsConcreteMethodCallStmt | ||
| import org.usvm.machine.interpreter.TsStepScope | ||
| import org.usvm.machine.state.TsMethodResult | ||
| import org.usvm.machine.state.lastStmt | ||
| import org.usvm.machine.state.newStmt | ||
| import org.usvm.types.singleOrNull | ||
| import org.usvm.util.EtsHierarchy | ||
|
|
||
| /** Only source allocations carry an exact runtime class. Symbolic inputs retain the prototype boundary. */ | ||
| private fun EtsHierarchy.allocatedClass(scope: TsStepScope, receiver: UHeapRef): EtsClass? { | ||
| if (!isAllocatedConcreteHeapRef(receiver)) return null | ||
|
|
||
| val type = scope.calcOnState { memory.typeStreamOf(receiver).singleOrNull() } as? EtsClassType ?: return null | ||
| return classesForType(type).singleOrNull()?.takeIf { it.category == EtsClassCategory.CLASS } | ||
| } | ||
|
|
||
| internal fun TsExprResolver.completedClassGetter(field: EtsInstanceFieldRef): UExpr<*>? { | ||
| val result = scope.calcOnState { methodResult } as? TsMethodResult.Success.RegularCall ?: return null | ||
| val method = result.method | ||
| if (!method.isAccessor || method.parameters.isNotEmpty() || method.name != field.field.name) return null | ||
|
|
||
| scope.doWithState { methodResult = TsMethodResult.NoCall } | ||
| return result.value | ||
| } | ||
|
|
||
| internal fun TsExprResolver.readClassAccessor( | ||
| field: EtsInstanceFieldRef, | ||
| receiver: UHeapRef, | ||
| ): TsIntrinsicFieldResult<UExpr<*>> { | ||
| val name = field.field.name | ||
| val clazz = hierarchy.allocatedClass(scope, receiver) ?: return TsIntrinsicFieldResult.NotApplicable | ||
| val members = hierarchy.classPrototypeAccessors(clazz, name) ?: return TsIntrinsicFieldResult.NotApplicable | ||
| if (scope.hasWrittenOwnProperty(receiver, name)) return TsIntrinsicFieldResult.NotApplicable | ||
|
|
||
| val couldHaveOwnDeclaration = hierarchy.getAncestors(clazz).any { owner -> | ||
| owner.category == EtsClassCategory.CLASS && | ||
| owner.fields.any { !it.isStatic && !it.isDeclare && it.name == name } | ||
| } | ||
| if (couldHaveOwnDeclaration) { | ||
| throw UnsupportedOperationException("Own data property '$name' may shadow a class accessor") | ||
| } | ||
|
|
||
| val hasChangedPrototype = scope.calcOnState { (receiver to PROTOTYPE_PROPERTY_NAME) in writtenConcreteFields } | ||
| if (hasChangedPrototype) { | ||
| throw UnsupportedOperationException("Reading class accessor '$name' after prototype mutation is unsupported") | ||
| } | ||
|
|
||
| val getter = resolveClassGetter(members, name) | ||
| if (getter == null) return TsIntrinsicFieldResult.Handled(ctx.mkUndefinedValue()) | ||
|
|
||
| val call = EtsInstanceCallExpr( | ||
| instance = field.instance, | ||
| callee = getter.signature, | ||
| args = emptyList(), | ||
| type = field.type, | ||
| ) | ||
| val returnSite = scope.calcOnState { lastStmt } | ||
| val getterCall = TsConcreteMethodCallStmt( | ||
| callee = getter, | ||
| call = call, | ||
| resolvedReceiver = receiver, | ||
| instance = receiver, | ||
| args = emptyList(), | ||
| returnSite = returnSite, | ||
| ) | ||
| scope.doWithState { newStmt(getterCall) } | ||
| return TsIntrinsicFieldResult.Stopped | ||
| } | ||
|
|
||
| private fun resolveClassGetter(members: List<EtsMethod>, name: String): EtsMethod? { | ||
| val accessorsAreUnambiguous = members.all { it.isAccessor && it.parameters.size <= 1 } && | ||
| members.count { it.parameters.isEmpty() } <= 1 && | ||
| members.count { it.parameters.size == 1 } <= 1 | ||
| if (!accessorsAreUnambiguous) { | ||
| throw UnsupportedOperationException("Class prototype property '$name' needs accessor or method-value lookup") | ||
| } | ||
|
|
||
| val getter = members.singleOrNull { it.parameters.isEmpty() } ?: return null | ||
| if (getter.cfg.stmts.isEmpty()) { | ||
| throw UnsupportedOperationException("Class accessor '$name' has no executable getter") | ||
| } | ||
| return getter | ||
| } | ||
|
|
||
| internal fun TsExprResolver.needsClassAccessorRead(receiver: UHeapRef, name: String): Boolean { | ||
| val clazz = hierarchy.allocatedClass(scope, receiver) ?: return false | ||
| return hierarchy.classPrototypeAccessors(clazz, name)?.any { it.isAccessor } == true | ||
| } | ||
|
|
||
| internal fun TsExprResolver.hasClassAccessors(receiver: UHeapRef): Boolean { | ||
| val clazz = hierarchy.allocatedClass(scope, receiver) ?: return false | ||
| return hierarchy.hasClassAccessors(clazz) | ||
| } | ||
|
|
||
| internal fun TsExprResolver.ensureClassPrototypeIsNotExposed(receiver: UHeapRef, name: String) { | ||
| val unsupportedAccess = when (name) { | ||
| PROTOTYPE_PROPERTY_NAME -> ctx.allocatedNamedProjectClass(scope, receiver) != null | ||
| "prototype" -> hasProtectedClassPrototype(receiver) | ||
| else -> false | ||
| } | ||
| if (unsupportedAccess) throw UnsupportedOperationException("Project class prototype access is unsupported") | ||
| } | ||
|
|
||
| private fun TsExprResolver.hasProtectedClassPrototype(receiver: UHeapRef): Boolean { | ||
| val constructor = receiver as? UConcreteHeapRef ?: return false | ||
| val signature = ctx.classConstructorSignature(constructor) ?: return false | ||
| if (ctx.namedProjectClass(signature) != null) return true | ||
|
|
||
| val clazz = hierarchy.classesForType(EtsClassType(signature)).singleOrNull() ?: return false | ||
| return hierarchy.hasClassAccessors(clazz) | ||
| } | ||
|
|
||
| private fun EtsHierarchy.hasClassAccessors(clazz: EtsClass): Boolean = getAncestors(clazz).any { owner -> | ||
| owner.category == EtsClassCategory.CLASS && owner.methods.any { !it.isStatic && it.isAccessor } | ||
| } | ||
|
|
||
| internal fun TsStepScope.ensureNoClassAccessorWrite( | ||
| receiver: UHeapRef, | ||
| field: EtsFieldSignature, | ||
| hierarchy: EtsHierarchy, | ||
| ) { | ||
| val clazz = hierarchy.allocatedClass(this, receiver) ?: return | ||
| if (field.name == PROTOTYPE_PROPERTY_NAME && hierarchy.hasClassAccessors(clazz)) { | ||
| throw UnsupportedOperationException("Prototype mutation on classes with accessors is unsupported") | ||
| } | ||
|
|
||
| val members = hierarchy.classPrototypeAccessors(clazz, field.name) ?: return | ||
| if (members.none { it.isAccessor } || hasWrittenOwnProperty(receiver, field.name)) return | ||
|
|
||
| val method = calcOnState { lastEnteredMethod } | ||
| val definingClassField = method.name == INSTANCE_INIT_METHOD_NAME && | ||
| method.signature.enclosingClass == field.enclosingClass && | ||
| method.enclosingClass?.fields?.any { !it.isStatic && it.name == field.name } == true | ||
| if (definingClassField) return | ||
|
|
||
| throw UnsupportedOperationException("Writing class accessor '${field.name}' is unsupported") | ||
| } | ||
|
|
||
| private fun TsStepScope.hasWrittenOwnProperty(receiver: UHeapRef, name: String): Boolean = calcOnState { | ||
| val written = (receiver to name) in writtenConcreteFields | ||
| written && memory.read(ctx.deletedFieldLValue(receiver, name)).isFalse | ||
| } | ||
125 changes: 125 additions & 0 deletions
125
usvm-ts/src/main/kotlin/org/usvm/machine/expr/ProjectClassConstructor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package org.usvm.machine.expr | ||
|
|
||
| import org.jacodb.ets.model.EtsAssignStmt | ||
| import org.jacodb.ets.model.EtsClass | ||
| import org.jacodb.ets.model.EtsClassCategory | ||
| import org.jacodb.ets.model.EtsClassSignature | ||
| import org.jacodb.ets.model.EtsClassType | ||
| import org.jacodb.ets.model.EtsReturnStmt | ||
| import org.jacodb.ets.model.EtsThis | ||
| import org.jacodb.ets.utils.CONSTRUCTOR_NAME | ||
| import org.jacodb.ets.utils.STATIC_INIT_METHOD_NAME | ||
| import org.usvm.UConcreteHeapRef | ||
| import org.usvm.UExpr | ||
| import org.usvm.UHeapRef | ||
| import org.usvm.api.typeStreamOf | ||
| import org.usvm.isAllocatedConcreteHeapRef | ||
| import org.usvm.isFalse | ||
| import org.usvm.isTrue | ||
| import org.usvm.machine.TsContext | ||
| import org.usvm.machine.interpreter.TsStepScope | ||
| import org.usvm.machine.interpreter.isInitialized | ||
| import org.usvm.types.singleOrNull | ||
| import org.usvm.util.canonicalizeExecutableOverloads | ||
| import org.usvm.util.hasStringName | ||
|
|
||
| /** Named source classes have a constructor identity, even while a base initializer is running. */ | ||
| internal fun TsContext.namedProjectClass(signature: EtsClassSignature): EtsClass? { | ||
| if (signature.name.isEmpty() || signature.name.startsWith('%') || isSourceModelClass(signature)) return null | ||
|
|
||
| return scene.projectClasses.singleOrNull { owner -> | ||
| owner.signature == signature && owner.category == EtsClassCategory.CLASS && !owner.isDeclare | ||
| } | ||
| } | ||
|
|
||
| internal fun TsContext.namedProjectConstructor(reference: UHeapRef): EtsClass? { | ||
| val concrete = reference as? UConcreteHeapRef ?: return null | ||
| val signature = classConstructorSignature(concrete) ?: return null | ||
| return namedProjectClass(signature) | ||
| } | ||
|
|
||
| internal fun TsContext.allocatedNamedProjectClass(scope: TsStepScope, receiver: UHeapRef): EtsClass? { | ||
| if (!isAllocatedConcreteHeapRef(receiver)) return null | ||
|
|
||
| val type = scope.calcOnState { memory.typeStreamOf(receiver).singleOrNull() } as? EtsClassType ?: return null | ||
| return namedProjectClass(type.signature) | ||
| } | ||
|
|
||
| internal fun TsContext.readProjectClassConstructorField( | ||
| scope: TsStepScope, | ||
| receiver: UHeapRef, | ||
| name: String, | ||
| ): UExpr<*>? = when (name) { | ||
| CONSTRUCTOR_NAME -> readProjectConstructor(scope, receiver) | ||
| "name" -> readProjectConstructorName(scope, receiver) | ||
| else -> null | ||
| } | ||
|
|
||
| private fun TsContext.readProjectConstructor(scope: TsStepScope, receiver: UHeapRef): UExpr<*>? { | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| val clazz = allocatedNamedProjectClass(scope, receiver) ?: return null | ||
| val hasPrototypeWrite = scope.calcOnState { (receiver to PROTOTYPE_PROPERTY_NAME) in writtenConcreteFields } | ||
| val wasWritten = scope.calcOnState { (receiver to CONSTRUCTOR_NAME) in writtenConcreteFields } | ||
| val deleted = scope.calcOnState { memory.read(deletedFieldLValue(receiver, CONSTRUCTOR_NAME)) } | ||
| val unsupportedLookup = when { | ||
| hasPrototypeWrite -> "Project class constructor lookup after prototype mutation is unsupported" | ||
| wasWritten && !deleted.isFalse && !deleted.isTrue -> { | ||
| "Conditional deletion of a project class constructor property is unsupported" | ||
| } | ||
| else -> null | ||
| } | ||
| if (unsupportedLookup != null) throw UnsupportedOperationException(unsupportedLookup) | ||
| if (wasWritten && deleted.isFalse) return null | ||
|
|
||
| ensureOrdinaryProjectConstructor(scope, clazz) | ||
| val constructors = clazz.methods.filter { !it.isStatic && it.hasStringName(CONSTRUCTOR_NAME) } | ||
| .canonicalizeExecutableOverloads() | ||
| val hasDeclaredOwnConstructor = clazz.fields.any { | ||
| !it.isStatic && !it.isDeclare && it.hasStringName(CONSTRUCTOR_NAME) | ||
| } | ||
| if (constructors.size != 1 || hasDeclaredOwnConstructor) { | ||
| throw UnsupportedOperationException("Project class constructor property may be shadowed: ${clazz.signature}") | ||
| } | ||
|
|
||
| return classConstructorRef(clazz.signature) | ||
| } | ||
|
|
||
| private fun TsContext.readProjectConstructorName(scope: TsStepScope, receiver: UHeapRef): UExpr<*>? { | ||
| val clazz = namedProjectConstructor(receiver) ?: return null | ||
| ensureOrdinaryProjectConstructor(scope, clazz) | ||
| if (clazz.declaresStaticName()) { | ||
| throw UnsupportedOperationException( | ||
| "Project class constructor name is shadowed by a static member: ${clazz.signature}", | ||
| ) | ||
| } | ||
|
|
||
| return mkStringConstant(clazz.name, scope) | ||
| } | ||
|
|
||
| private fun ensureOrdinaryProjectConstructor(scope: TsStepScope, clazz: EtsClass) { | ||
| val hasDecorator = clazz.decorators.isNotEmpty() || clazz.methods.any { it.decorators.isNotEmpty() } || | ||
| clazz.fields.any { it.decorators.isNotEmpty() } | ||
| if (hasDecorator) { | ||
| throw UnsupportedOperationException("Project class constructor reflection with decorators is unsupported") | ||
| } | ||
|
|
||
| val initializer = clazz.methods.singleOrNull { it.name == STATIC_INIT_METHOD_NAME } | ||
| val initializerCanHaveEffects = initializer?.cfg?.stmts.orEmpty().any { stmt -> | ||
| stmt !is EtsReturnStmt && (stmt !is EtsAssignStmt || stmt.rhv !is EtsThis) | ||
| } | ||
| if (initializerCanHaveEffects && !scope.calcOnState { isInitialized(clazz) }) { | ||
| throw UnsupportedOperationException("Project class constructor may be changed by pending static initialization") | ||
| } | ||
| } | ||
|
|
||
| private fun EtsClass.declaresStaticName(): Boolean = | ||
| fields.any { it.isStatic && !it.isDeclare && it.hasStringName("name") } || | ||
| methods.any { it.isStatic && !it.isDeclare && it.hasStringName("name") } | ||
|
|
||
| internal fun TsContext.ensureProjectConstructorPropertyUnmodified(receiver: UHeapRef, name: String) { | ||
| if (name != "name" && name != "prototype") return | ||
| val clazz = namedProjectConstructor(receiver) ?: return | ||
|
|
||
| if (name == "prototype" || !clazz.declaresStaticName()) { | ||
| throw UnsupportedOperationException("Mutating project class constructor property '$name' is unsupported") | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.