Skip to content

Check for ArrayStoreException on array update #971

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 1 commit into from
Sep 25, 2022

Conversation

dtim
Copy link
Collaborator

@dtim dtim commented Sep 19, 2022

Description

This PR adds a check to TraversalContext.traverseAssignLeftPart to detect if ArrayStoreException should be thrown for the given array element assignment statement.

The exception is generated if the type of the value on the right-hand side of the assignment is not a subtype of the array elements type and is not null.

This change affects all code that explicitly assigns array elements, as well as all calls to Objects.arraycopy and similar Arrays static methods.

Fixes #923

Type of Change

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

New executions may be generated for the code that involves array modification.

How Has This Been Tested?

Automated Testing

All existing unit tests should pass.

A new test suite org.utbot.examples.arrays.ArrayStoreExceptionExamplesTest has been added, it should pass both with and without concrete execution.

Manual Scenario

Generate a test suite (with fuzzer turned off) for a method that assigns values to array elements of a incompatible reference type, e.g.:

public class Foo {
    public int foo() {
        Integer[] integers = { 1, 2, 3 };
        Object[] objects = integers;
        objects[1] = "";
        return 1;
    }
}

A single test should be generated, that corresponds to ArrayStoreException:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;

public class FooTest {
    ///region Test suites for executable basic.Foo.foo

    ///region SYMBOLIC EXECUTION ENGINE: ERROR SUITE for method foo()

    /**
     * @utbot.classUnderTest {@link Foo}
     * @utbot.methodUnderTest {@link Foo#foo()}
     * @utbot.throwsException {@link ArrayStoreException} in: objects[1] = "";
     */
    @Test
    @DisplayName("foo: objects[1] = \"\" -> ThrowArrayStoreException")
    public void testFoo_IntegerValueOf() {
        Foo foo = new Foo();
        
        /* This test fails because method [basic.Foo.foo] produces [java.lang.ArrayStoreException: java.lang.String]
            basic.Foo.foo(Foo.java:9) */
        foo.foo();
    }
    ///endregion

    ///endregion
}

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

@dtim dtim force-pushed the dtim/923_arraystoreexception branch 7 times, most recently from 383f621 to bfdc5b1 Compare September 23, 2022 12:59
@dtim dtim marked this pull request as ready for review September 23, 2022 12:59
@dtim dtim requested a review from CaelmBleidd September 23, 2022 12:59
@dtim dtim marked this pull request as draft September 23, 2022 13:56
@dtim dtim force-pushed the dtim/923_arraystoreexception branch 2 times, most recently from 45d6600 to 474dc19 Compare September 23, 2022 15:47
@dtim dtim marked this pull request as ready for review September 23, 2022 15:48
Copy link
Member

@CaelmBleidd CaelmBleidd left a comment

Choose a reason for hiding this comment

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

LGTM

// any reference type may be assigned to elements of an Object array, so we should not generate
// [ArrayStoreException] in these cases.

// TODO: remove enclosing `if` when [UtIfExpression] negation is fixed
Copy link
Member

Choose a reason for hiding this comment

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

Do we have an issue with it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I created the issue: #1007.

@dtim dtim force-pushed the dtim/923_arraystoreexception branch from 474dc19 to 5313a49 Compare September 25, 2022 18:59
@dtim dtim force-pushed the dtim/923_arraystoreexception branch from 5313a49 to 7df774b Compare September 25, 2022 18:59
@dtim dtim enabled auto-merge (squash) September 25, 2022 19:01
@dtim dtim merged commit 13d07aa into main Sep 25, 2022
@dtim dtim deleted the dtim/923_arraystoreexception branch September 25, 2022 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect ArrayStoreException on illegal array writes
2 participants