-
Notifications
You must be signed in to change notification settings - Fork 23
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
Support static fields #261
base: main
Are you sure you want to change the base?
Conversation
usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsStaticFieldsRegion.kt
Dismissed
Show dismissed
Hide dismissed
usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsStaticFieldsRegion.kt
Dismissed
Show dismissed
Hide dismissed
usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsStaticFieldsRegion.kt
Dismissed
Show dismissed
Hide dismissed
e40180a
to
ed144e0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issues with field usage in lvalues and processing of unresolved sorts
private val staticFieldReadings = mkAstInterner<TsStaticFieldReading<*>>() | ||
fun <Sort : USort> mkStaticFieldReading( | ||
regionId: TsStaticFieldRegionId<Sort>, | ||
field: EtsFieldSignature, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fieldSignature might be confusing due to type usage in it
) : UComposer<EtsType, TsSizeSort>(ctx, memory, ownership), TsTransformer | ||
) : UComposer<EtsType, TsSizeSort>(ctx, memory, ownership), TsTransformer { | ||
override fun <Sort : USort> transform(expr: TsStaticFieldReading<Sort>): UExpr<Sort> { | ||
return memory.read(TsStaticFieldLValue(expr.field, expr.sort)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's create util methods for these field values consturctors, that will create lValue from fields' name
|
||
fun translate(expr: TsStaticFieldReading<Sort>): UExpr<Sort> = | ||
translated.getOrPut(expr.field) { | ||
expr.sort.mkConst("${expr.field.enclosingClass}_${regionId.sort}_${expr.field.name}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
usage of enclosing class seems suspicious here since it if often unk/unk
, so many fields will be conflicting
override fun read(key: TsStaticFieldLValue<Sort>): UExpr<Sort> { | ||
val translated = translatedFields[key.field] | ||
?: translator.translate( | ||
key.sort.tctx.mkStaticFieldReading(key.memoryRegionId as TsStaticFieldRegionId, key.field, key.sort) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again, usage of fields should be considered as suspicious
usvm-ts/src/main/kotlin/org/usvm/machine/expr/TsExprResolver.kt
Outdated
Show resolved
Hide resolved
} | ||
|
||
val fieldType = scene.fieldLookUp(value.field).type | ||
val sort = typeToSort(fieldType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if sort is unresolved, so we need to process using fake objects somehow
val instance = exprResolver.resolve(lhv.instance)?.asExpr(addressSort) ?: return@doWithState | ||
val field = lhv.field | ||
val fieldType = scene.fieldLookUp(field).type | ||
val sort = typeToSort(fieldType) | ||
val lValue = UFieldLValue(sort, instance, field.name) | ||
memory.write(lValue, expr.asExpr(lValue.sort), guard = trueExpr) | ||
} | ||
|
||
is EtsStaticFieldRef -> { | ||
val field = lhv.field | ||
val fieldType = scene.fieldLookUp(field).type | ||
val sort = typeToSort(fieldType) | ||
val lValue = TsStaticFieldLValue(field, sort) | ||
memory.write(lValue, expr.asExpr(lValue.sort), guard = trueExpr) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issued with field and unresolved sort
localVarToIdx.getOrPut(method) { | ||
method.getDeclaredLocals().mapIndexed { idx, local -> | ||
local.name to idx + method.parametersWithThisCount | ||
}.toMap() | ||
}[local.name] | ||
?: error("Local not declared: $local") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting, lets extract something into variables
class TsStaticFieldReading<Sort : USort> internal constructor( | ||
ctx: TsContext, | ||
val regionId: TsStaticFieldRegionId<Sort>, | ||
val field: EtsFieldSignature, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should replace signature with name or how to process it correctly?
58c8dee
to
3579c90
Compare
val clazz = classes.single() | ||
return clazz.fields.single { it.name == field.name } | ||
} | ||
val fields = classes.flatMap { it.fields.filter { it.name == field.name } } |
Check warning
Code scanning / detekt
Disallow shadowing variable declarations. Warning
val clazz = classes.single() | ||
return clazz.fields.single { it.name == field.name } | ||
} | ||
val fields = classes.flatMap { it.fields.filter { it.name == field.name } } |
Check warning
Code scanning / detekt
Disallow shadowing variable declarations. Warning
} | ||
} | ||
|
||
val fields = ctx.scene.projectAndSdkClasses.flatMap { it.fields.filter { it.name == field.name } } |
Check warning
Code scanning / detekt
Disallow shadowing variable declarations. Warning
Squashed, Jc-like impl
17ad4d3
to
9541694
Compare
} | ||
|
||
internal fun TsState.isInitialized(clazz: EtsClass): Boolean { | ||
val instance = staticStorage[clazz]!! |
Check warning
Code scanning / detekt
Unsafe calls on nullable types detected. These calls will throw a NullPointerException in case the nullable value is null. Warning
} | ||
|
||
internal fun TsState.markInitialized(clazz: EtsClass) { | ||
val instance = staticStorage[clazz]!! |
Check warning
Code scanning / detekt
Unsafe calls on nullable types detected. These calls will throw a NullPointerException in case the nullable value is null. Warning
This PR adds support for static fields.
Currently, this is just a (partial) copy of Jc-impl.