Skip to content

Fixes for analysis of reflection and Unsafe #721 #797

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
Aug 29, 2022

Conversation

dtim
Copy link
Collaborator

@dtim dtim commented Aug 26, 2022

Description

Add support for symbolic/concrete analysis of reflection and Unsafe calls under JDK 11 and improve their analysis under JDK 8.

  • Updated system packages list to prevent mocking of jdk.internal.* classes on JDK 11.
  • Refactored FieldId.isInaccessibleViaReflection property to consistently use it in all relevant checks (engine, codegen, concrete executor, mocking), updated the class/field list to JDK 11.
  • Implemented a wrapper for SecurityManager that simulates the "enable all" policy to avoid long analysis of the real privilege checking code. We can lose branches, but the hypothesis is that we will keep "interesting" branches. Two corresponding methods have been added to the [Class] override to save a bit more of work.

As the concrete executor now implements a sandbox to disable unsafe and dangerous operations, UtSettings.disableSandbox option has been introduced. It allows to disable sandboxing without using non-trivial security manager configuration file. A utility function withoutSandbox can now be used to disable sandbox in a specific unit test.

Fixes #721
Fixes #785

Note: the StringExamplesTest.testByteToString unit test has been temporarily disabled as flaky. The change of its behavior seems to be related to this PR (new wrappers may change the set of generated constraints), but the test itself is buggy and very fragile due to limitations of the current String wrapper implementation (related issue: #131). The test will be re-enabled as a part of new string support (without Z3-backed UtNativeString), as its problems are rooted in the Z3 string theory and are not related to SecurityManager and other stuff affected by this PR.

Type of Change

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

This PR adds a new way to disable sandbox, alters the list of classes and fields that can be mocked, can make some static fields not meaningful, and replaces the real analysis of privilege checking with a wrapper/overrides.

How Has This Been Tested?

Automated Testing

New tests have been added:

  • org.utbot.examples.exceptions.JvmCrashExamplesTest#testCrashPrivileged: checks that Unsafe call can be analyzed when the MUT contains an explicit call to doPrivileged method.
  • org.utbot.examples.unsafe.UnsafeOperationsTest#checkGetAddressSizeOrZero: checks a "safe" Unsafe call.
  • org.utbot.examples.unsafe.UnsafeOperationsTest#checkGetAddressSizeOrZeroWithMocks: checks a "safe" Unsafe call with mocking enabled.

Manual Scenario

It should be now possible to generate unit tests for the code that involves unsafe calls, e.g.:

    public int getAddressSizeOrZero() {
        try {
            Field f = Unsafe.class.getDeclaredField("theUnsafe");
            f.setAccessible(true);
            Unsafe unsafe = (Unsafe) f.get(null);
            return unsafe.addressSize();
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException("Reflection failed");
        }
    }

It should be possible to generate tests for the "crash" example from the description of #721, but it is necessary to disable summary generation by setting UtSettings.enableMachineLearningModule = false. Reason: the only execution that can be generated results in JVM failure, but by default this execution is removed by the summarizer (related issue: #800).

Note: make sure that sandbox is either turned off or configured with all necessary privileges during testing, or the method under test wraps privileged calls to doPrivileged, otherwise only "disabled by sandbox" tests will be generated.

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/721_reflection_unsafe branch 2 times, most recently from 3d26899 to fe736fc Compare August 26, 2022 21:07
@dtim dtim changed the title [draft for testing] dtim/721 reflection unsafe Fixes for analysis of reflection and Unsafe #721 Aug 27, 2022
@dtim dtim marked this pull request as ready for review August 27, 2022 11:46
@dtim dtim force-pushed the dtim/721_reflection_unsafe branch from cc16746 to 08ef426 Compare August 29, 2022 12:33
  * Refactored the list of fields that can't be accessed via reflection
    to use the single rules for all cases (engine, concrete executor,
    codegen)

  * Updated this list for Java 11 classes

  * Added a check to avoid marking reflection-inaccessible static fields
    as meaningful (they can't be reflexively created by the codegen anyway)

  * Added `jdk.internal` to the list of system packages to avoid
    mocking, disabled mocking for classes that can't be accessed via
    reflection

  * Added a wrapper for `SecurityManager` and override for two related
    methods in [Class]

  * Added UtSettings option to disable sandbox

  * Temporarily disabled `StringExamplesTest.testByteToString` as flaky
@dtim dtim force-pushed the dtim/721_reflection_unsafe branch from 08ef426 to 99fad06 Compare August 29, 2022 14:29
@dtim dtim enabled auto-merge (squash) August 29, 2022 14:31
@dtim dtim merged commit 1f812d9 into main Aug 29, 2022
@dtim dtim deleted the dtim/721_reflection_unsafe branch August 29, 2022 15:25
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
5 participants