UnitTestBot / UTBotJava

Automated unit test generation and precise code analysis for Java
Apache License 2.0
133 stars 40 forks source link

sun.misc.Unsafe in code generation #121

Open denis-fokin opened 2 years ago

denis-fokin commented 2 years ago

Description

Our tests may contain usages of sun.misc.Unsafe class. Removal of sun.misc.Unsafe class is a backward incompatible change in Java internal API. Tests that have been generated this way cannot be run with Java 9+.

Expected behavior

The generated code does not contain sun.misc.Unsafe invocations.

Environment

Run the utbot-framework tests, find generated tests with unsafe.

Potential alternatives

For Java 9 it was possible to use flags in order to allow internal API usage. But it does not work for higher versions of Java.

Context

No additional context.

dtim commented 2 years ago

I think there are two unrelated topics.

  1. The engine sometimes generates calls to Unsafe that should not be present (see #884 for an example and further details). I suppose that we would mostly get jdk.internal.misc.Unsafe for incorrectly processed JDK calls, although sun.misc.Unsafe can be produced during analysis of user code and third-party libraries.

  2. References to sun.misc.Unsafe can be correctly generated when UnitTestBot analyses the code that refers to sun.misc.Unsafe in its public API (e.g., as a class field, a method parameter, or a return value). In this case, I suppose that the usage of sun.misc.Unsafe is legit and relatively safe. As far as I understand, sun.misc.Unsafe is considered a critical API by JEP 260 and should be present in current (and probably future) JDK releases (see this article for an overview).