Skip to content

Make UtEnumConstModel and UtClassRefModel reference models #414 #611

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

Merged
merged 3 commits into from
Aug 4, 2022

Conversation

dtim
Copy link
Collaborator

@dtim dtim commented Jul 29, 2022

Description

Historically UtEnumConstModel and UtClassRefModel have been processed not as other reference models, but in a special way, more like to primitive types. This approach leads to several problems, especially to class cast errors when processing generic collections with enums or class references as elements.

This commit makes UtEnumConstModel and UtClassRefModel subtypes of UtReferenceModel.

  • Concrete executor is modified to respect the identity of static fields to avoid rewriting enum values and Class<?> instances.

  • Special processing for enums is implemented. When a new enum value is created, or an Object is being cast to the enum type, static values for the enum class are initialized, and the set of hard constraint is added to require that the new instance has the same address and ordinal as any one of enum constants to implement reference equality for enums.

  • Corresponding changes in fuzzer model providers have been implemented.

Limitations (probably have been there for a long time, addressed separately):

Fixes #414 (meta-issue)
Fixes #230
Fixes #300

Type of Change

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

How Has This Been Tested?

Automated Testing

All existing unit tests should pass.

New unit tests for enums have been added: org.utbot.examples.enums.ComplexEnumExamplesTest

Note: the test org.utbot.examples.enums.ComplexEnumExamplesTest#testFindState is currently disabled because of limited anonymous classes support.

Manual Scenario

To check that #230 is fixed: generate the test suite for the code example in issue #230 description. Test should be generated, no exceptions should be thrown.

To check that #300 is fixed: run the contest estimator with settings from issue #300 description. Generated file should compile, class name should be present in qualified enum constant names.

Checklist:

This is the author self-check list

  • The change followed the style guidelines of the UTBot project
  • Self-review of the code is passed
  • The change contains enough commentaries, particularly in hard-to-understand areas
  • New documentation is provided or existed one is altered
  • No new warnings
  • New tests have been added
  • All tests pass locally with my changes

Copy link
Member

@Damtev Damtev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic LGTM, some issues about codestyle

* Get model id or null if id is null or the model has no id.
*/
fun UtModel.idOrNull(): Int? = when (this) {
is UtNullModel -> 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you link this constant with org.utbot.engine.ResolverKt#NULL_ADDR somehow, please?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving the constant from Resolver into Api.kt?

@dtim dtim force-pushed the dtim/414-reference-subtypes branch 3 times, most recently from 3de411a to 84c66ce Compare August 1, 2022 17:07
Copy link
Member

@sergeypospelov sergeypospelov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The concrete part looks good to me.

import org.utbot.engine.UtNamedStore
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.FieldId
import org.utbot.framework.plugin.api.classId
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these imports are redundant.

@dtim dtim force-pushed the dtim/414-reference-subtypes branch 2 times, most recently from 54af0a3 to 410613a Compare August 3, 2022 11:23
@dtim
Copy link
Collaborator Author

dtim commented Aug 3, 2022

Updated UtModelVisitor and FieldStateVisitor to reflect model hierarchy change.

@dtim dtim requested a review from sergeypospelov August 3, 2022 11:54
@dtim dtim force-pushed the dtim/414-reference-subtypes branch 2 times, most recently from f4959aa to e29923f Compare August 4, 2022 04:19
dtim added 3 commits August 4, 2022 12:02
Historically `UtEnumConstModel` and `UtClassRefModel` have been
processed not as other reference models, but in a special way,
more like to primitive types. This approach leads to several
problems, especially to class cast errors when processing generic
collections with enums or class references as elements.

This commit makes `UtEnumConstModel` and `UtClassRefModel` subtypes of
`UtReferenceModel`.

  * Concrete executor is modified to respect the identity of static
    fields to avoid rewriting enum values and `Class<?>` instances.

  * Special processing for enums is implemented.
    When a new enum value is created, or an `Object` is being cast
    to the enum type, static values for the enum class are initialized,
    and the set of hard constraint is added to require that the new
    instance has the same address and ordinal as any one of enum
    constants to implement reference equality for enums.

  * Corresponding changes in fuzzer model providers have been
    implemented.

  * UtModelVisitor has been updated to reflect the new inheritance
    hierarchy.
A custom id generator interface hierarchy is used instead of `IntSupplier`:
  * `IdGenerator` that allows to create fresh identifiers,
  * `IdentityPreservingIdGenerator`: `IdGenerator` that can return
    the same id for the same object.

A default implementation of `IdentityPreservingIdGenerator` is used in
fuzzer. It uses reference equality for object comparison, that allows
to create distinct models of equal object (in `equals` sense), and
to always assign the same id to the same enum value.
@dtim dtim force-pushed the dtim/414-reference-subtypes branch from 374eb3c to b11b184 Compare August 4, 2022 09:03
@dtim dtim merged commit e23289b into main Aug 4, 2022
@dtim dtim deleted the dtim/414-reference-subtypes branch August 4, 2022 12:08
denis-fokin pushed a commit that referenced this pull request Aug 18, 2022
Make UtEnumConstModel and UtClassRefModel reference models #414

Historically `UtEnumConstModel` and `UtClassRefModel` have been
processed not as other reference models, but in a special way,
more like to primitive types. This approach leads to several
problems, especially to class cast errors when processing generic
collections with enums or class references as elements.

This commit makes `UtEnumConstModel` and `UtClassRefModel` subtypes of
`UtReferenceModel`.

  * Concrete executor is modified to respect the identity of static
    fields to avoid rewriting enum values and `Class<?>` instances.

  * Special processing for enums is implemented.
    When a new enum value is created, or an `Object` is being cast
    to the enum type, static values for the enum class are initialized,
    and the set of hard constraint is added to require that the new
    instance has the same address and ordinal as any one of enum
    constants to implement reference equality for enums.

  * `UtModelVisitor` has been updated to reflect the new inheritance
    hierarchy.

  * Corresponding changes in the fuzzer have been implemented, including
    id generation refactoring. A custom id generator interface hierarchy is now
    used instead of `IntSupplier`:
      - `IdGenerator` that allows to create fresh identifiers,
      - `IdentityPreservingIdGenerator`: `IdGenerator` that can return the same id
        for the same object.

    A default implementation of `IdentityPreservingIdGenerator` is used in
    fuzzer. It uses reference equality for object comparison, that allows
    to create distinct models of equal object (in `equals` sense), and
   to always assign the same id to the same enum value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
4 participants