Discussion:
Avoiding sun.misc.Unsafe and embracing modules in Java libraries: missing links
Rafael Winterhalter
2018-04-01 21:02:25 UTC
Permalink
Hello,

I am the/a maintainer of the libraries Byte Buddy, cglib and Mockito with
countless dependencies upstream and I wanted to give a summary of adopting
the JPMS and migrating away from sun.misc.Unsafe.

1. Java agents cannot define auxiliary classes.

Byte Buddy does support the JPMS fully, however, it still relies on
sun.misc.Unsafe::defineClass for its Java agent API and currently breaks on
Java 11 as this method was removed in a recent EA build. The reason for
using Unsafe is that many instrumentations need to define auxiliary classes
to aid an instrumentation similar to javac which sometimes needs to define
anonymous classes or even synthetic classes. For example, if a Java agent
wants to register an event listener to some framework, such listeners often
declare multiple methods what makes it impossible to fullfil the listener
contract using a lambda expression. Instead, one typically injects an
additional class into the same package as the instrumented class. In this
case, it is not possible to use MethodHandles.Lookup::defineClass as the
class file transformer does not necessarily have private access to the
lookup of the instrumented class.

The current workarounds are:

a) Open the package jdk.internal.misc to gain access to this package's
Unsafe class. This can be done via Instrumentation::redefineModule.
b) Open the java.lang package to access ClassLoader via reflection.
c) Open the java.lang package to access the internal lookup with global
access rights.

Of these solutions only (b) relies on standard API and is guaranteed to
function in the future but the solution still feels hacky and does not work
for instrumentations of classes on the bootstrap loader. Opening packages
also implies a risk of being applied carelessly since opening the package
to the agent's module most likely opens the package to the unnamed module
of the system class loader what invites to breaches of the JPMS
encapsulation by code that does not ship with the agent.

To offer a better solution, I would like to suggest one of the following:

a) Add a method defineClass(ClassLoader, byte[], ProtectionDomain) to the
Instrumentation interface that works similar to Unsafe::defineClass. This
would provide a very simple migration path. Since agents have access to
jdk.internal.misc, adding this method does not add any capabilities to the
agent, it merley avoids using internal API that might change.
b) Supply a MethodHandles.Lookup instance to the
ClassFileTransformer::transform API where the instance represents the
instrumented class's access rights. This does however carry the risk of
invoking the lookupClass method which would either load the instrumented
class prematurely causing a circularity error or return an unexpected value
such as null. Since the lookup API generally relies on loaded types, there
are probably other problems such as invoking Lookup::privateLookupIn before
all involved types are loaded.

For the sake of simlicity and since easy migration paths make a quick
adoption easier, I would suggestion solution (a), also in the light that
quick and dirty migrations might still choose option (b) to save time and
also since (b) might cause problems when types are not yet loaded.

2. Java proxies cannot invoke default methods of proxied interfaces

The Java proxy API does not currently allow the invocation of an overridden
default method since
the InvocationHandler API only supplies an instance of
java.lang.reflection.Method. In Java 8, it was always possible to get hold
of method handle of any method of the proxied interface and to specialize
the handle on the interface type to invoke the default implementation. With
the JPMS, even if an interface type is exported, this same type might not
be opened to another module. This implies that if an InvocationHandler is
defined by this module to which the interface is exported, this module's
InvocationHandler cannot resolve a specialized method handle to a default
method of the proxied interface. As a matter of fact, such a call is
impossible in this scenario whereas the same call is possible if the proxy
is implemented manually at compile time.

As a solution, I suggest to provide an argument to the InvocationHandler
that represents a lookup instance with the access rights of the proxy
class. Using this lookup, the specialized method handles could be resolved.

3. Mocking and serialization libraries still require
Unsafe::allocateInstance.

For Mockito, it is required to instantiate any class without invoking a
constructor with potential side-effects. This is of course a grose
violation of Java's general contract for class instantiation but this
feature is very useful.

Using a Java agent, it is already possible to emulate this feature without
internal API by instrumenting all constructors of all classes in the
hierarchy of a mocked class by transforming all constructors into the
following pseudo-code:

SomeConstructor(Void arg) {
if (MockitoBootHelper.THREAD_LOCAL.isMockInstantiatingMode()) {
super(null); // any constructor of the super class with default values
for all arguments
} else {
// original constructor code...
}
}

Before instantiating a mock, the thread local value that is held by a
bootstrap-loader injected class is set to true such that a side-effect free
construction is achieved.

This is of course too expensive and has negative effects on performance due
to additional branching and JIT-byte code limits such that one would rather
open jdk.internal.misc to access the Unsafe instantiation mechanism if a
Java agent is already available.

However, mocking and serialization libraries are not typically loaded as a
Java agent. Also, I think that the actual requirements are different. My
suggestion here is:

a) For serialization libraries, I think that adding
MethodHandles.Lookup::newInstance(Class<? extends Serializable>) with
standard deserialization mechanics would be sufficient.
b) For mocking libraries, this does not suffice as mocks can be of any
class. I understand that this breaks encapsulation but for unit tests, I
argue that the benefit of using these libraries outweights the benefit of
full encapsulation within a unit test.

As Mockito is typically run within a build which uses a JDK, we could
attach to the current VM using the attachment API. Since Java 9, this is no
longer possible to attach to the same VM but instead we start a helper JVM
process that applies the attachment indirectly. Unfortunately, this is a
rather costly operation what is especially problematic when running a
single unit test. (The difference to this approach over Unsafe is about
half a second on average.)

To overcome this, I would like to suggest to:

a) Add a method Instrumentation::allocateInstance(Class). Java agents can
already emulate this privilege as described above, this is therefore merely
a convenience.
b) Add a module jdk.test to JDK 11 with a class
JavaTest::getInstrumentation that returns an instrumentation instance for
the current JVM. This module should not be resolved by default but only
when requiring it explicitly similar to the EE modules after Java 9.

I think this solution carries two benefits:

a) Test libraries like Mockito can only be used in a testing scope. We
experience regularly that Mockito is used in production environments. The
library is not meant for that and we warn and document that this is not
intended use but people regularly ignore this directive. By requiring this
module, this form of abuse would no longer be possible and the JVM could
even give a meaningful error message if this use was intended.
b) The Instrumentation instance can be used for other meaningful testing
approaches. For example, Mockito offers an inline mock maker where the
mocking logic is inlined into a method body rather then creating a
subclass. This approach mainly targets final classes which have become more
common especially since the Kotlin language became popular. To supply this
alternative mock maker, Mockito attempts attaching an agent to the current
VM (directly or indirectly, depending on the VM's version) which suffers
the additional costs for attaching an agent that I mentioned before.

Thanks for reading this and I hope that you can consider these, as of
today, very practiced use cases. The JPMS migration has gone quite well, I
find. With these outstanding problems, JDK 11 could be the first release
where a majority of Java programs would no longer need to rely on internal
API.

Best regards, Rafael
Henri Tremblay
2018-04-03 19:26:29 UTC
Permalink
Hi Rafael,

I don't like the idea to have to explicitly load a module to create mocks.

Because Unsafe.allocateInstance is of course used to create mock but also
proxies. For instance, Spring uses it extensively through Objenesis. And
different frameworks doing serialization.

Also, It means Gradle and Maven would need to be updated to load the module
automatically in test context. And all IDE. A lot of annoyance for not much
benefit since everybody is currently happy.

I've been advocating that if creating a class without calling a constructor
is useless in multiple contexts, it should be easily available through the
API. I don't know exactly where to put it but it should be

- Available to all (java.base)
- Protected by the security manager

For completeness, there are 4 ways to create a class without calling a
constructor right now that I'm aware of:

- Unsafe.allocateInstance
- sun.reflect.ReflectionFactory.newConstructorForSerialization (my
favorite because it's the fastest one)
- Generate an extending class forgetting to call the super constructor
(so it's not exactly that same class that is instantiated). It requires
-Xverify:none
- Generate a class extending MagicAccessorImpl that will then
instantiates the wanted class but calling the wrong constructor

Regards,
Henri



On 1 April 2018 at 17:02, Rafael Winterhalter <***@gmail.com> wrote:

> Hello,
>
> I am the/a maintainer of the libraries Byte Buddy, cglib and Mockito with
> countless dependencies upstream and I wanted to give a summary of adopting
> the JPMS and migrating away from sun.misc.Unsafe.
>
> 1. Java agents cannot define auxiliary classes.
>
> Byte Buddy does support the JPMS fully, however, it still relies on
> sun.misc.Unsafe::defineClass for its Java agent API and currently breaks on
> Java 11 as this method was removed in a recent EA build. The reason for
> using Unsafe is that many instrumentations need to define auxiliary classes
> to aid an instrumentation similar to javac which sometimes needs to define
> anonymous classes or even synthetic classes. For example, if a Java agent
> wants to register an event listener to some framework, such listeners often
> declare multiple methods what makes it impossible to fullfil the listener
> contract using a lambda expression. Instead, one typically injects an
> additional class into the same package as the instrumented class. In this
> case, it is not possible to use MethodHandles.Lookup::defineClass as the
> class file transformer does not necessarily have private access to the
> lookup of the instrumented class.
>
> The current workarounds are:
>
> a) Open the package jdk.internal.misc to gain access to this package's
> Unsafe class. This can be done via Instrumentation::redefineModule.
> b) Open the java.lang package to access ClassLoader via reflection.
> c) Open the java.lang package to access the internal lookup with global
> access rights.
>
> Of these solutions only (b) relies on standard API and is guaranteed to
> function in the future but the solution still feels hacky and does not work
> for instrumentations of classes on the bootstrap loader. Opening packages
> also implies a risk of being applied carelessly since opening the package
> to the agent's module most likely opens the package to the unnamed module
> of the system class loader what invites to breaches of the JPMS
> encapsulation by code that does not ship with the agent.
>
> To offer a better solution, I would like to suggest one of the following:
>
> a) Add a method defineClass(ClassLoader, byte[], ProtectionDomain) to the
> Instrumentation interface that works similar to Unsafe::defineClass. This
> would provide a very simple migration path. Since agents have access to
> jdk.internal.misc, adding this method does not add any capabilities to the
> agent, it merley avoids using internal API that might change.
> b) Supply a MethodHandles.Lookup instance to the
> ClassFileTransformer::transform API where the instance represents the
> instrumented class's access rights. This does however carry the risk of
> invoking the lookupClass method which would either load the instrumented
> class prematurely causing a circularity error or return an unexpected value
> such as null. Since the lookup API generally relies on loaded types, there
> are probably other problems such as invoking Lookup::privateLookupIn before
> all involved types are loaded.
>
> For the sake of simlicity and since easy migration paths make a quick
> adoption easier, I would suggestion solution (a), also in the light that
> quick and dirty migrations might still choose option (b) to save time and
> also since (b) might cause problems when types are not yet loaded.
>
> 2. Java proxies cannot invoke default methods of proxied interfaces
>
> The Java proxy API does not currently allow the invocation of an overridden
> default method since
> the InvocationHandler API only supplies an instance of
> java.lang.reflection.Method. In Java 8, it was always possible to get hold
> of method handle of any method of the proxied interface and to specialize
> the handle on the interface type to invoke the default implementation. With
> the JPMS, even if an interface type is exported, this same type might not
> be opened to another module. This implies that if an InvocationHandler is
> defined by this module to which the interface is exported, this module's
> InvocationHandler cannot resolve a specialized method handle to a default
> method of the proxied interface. As a matter of fact, such a call is
> impossible in this scenario whereas the same call is possible if the proxy
> is implemented manually at compile time.
>
> As a solution, I suggest to provide an argument to the InvocationHandler
> that represents a lookup instance with the access rights of the proxy
> class. Using this lookup, the specialized method handles could be resolved.
>
> 3. Mocking and serialization libraries still require
> Unsafe::allocateInstance.
>
> For Mockito, it is required to instantiate any class without invoking a
> constructor with potential side-effects. This is of course a grose
> violation of Java's general contract for class instantiation but this
> feature is very useful.
>
> Using a Java agent, it is already possible to emulate this feature without
> internal API by instrumenting all constructors of all classes in the
> hierarchy of a mocked class by transforming all constructors into the
> following pseudo-code:
>
> SomeConstructor(Void arg) {
> if (MockitoBootHelper.THREAD_LOCAL.isMockInstantiatingMode()) {
> super(null); // any constructor of the super class with default values
> for all arguments
> } else {
> // original constructor code...
> }
> }
>
> Before instantiating a mock, the thread local value that is held by a
> bootstrap-loader injected class is set to true such that a side-effect free
> construction is achieved.
>
> This is of course too expensive and has negative effects on performance due
> to additional branching and JIT-byte code limits such that one would rather
> open jdk.internal.misc to access the Unsafe instantiation mechanism if a
> Java agent is already available.
>
> However, mocking and serialization libraries are not typically loaded as a
> Java agent. Also, I think that the actual requirements are different. My
> suggestion here is:
>
> a) For serialization libraries, I think that adding
> MethodHandles.Lookup::newInstance(Class<? extends Serializable>) with
> standard deserialization mechanics would be sufficient.
> b) For mocking libraries, this does not suffice as mocks can be of any
> class. I understand that this breaks encapsulation but for unit tests, I
> argue that the benefit of using these libraries outweights the benefit of
> full encapsulation within a unit test.
>
> As Mockito is typically run within a build which uses a JDK, we could
> attach to the current VM using the attachment API. Since Java 9, this is no
> longer possible to attach to the same VM but instead we start a helper JVM
> process that applies the attachment indirectly. Unfortunately, this is a
> rather costly operation what is especially problematic when running a
> single unit test. (The difference to this approach over Unsafe is about
> half a second on average.)
>
> To overcome this, I would like to suggest to:
>
> a) Add a method Instrumentation::allocateInstance(Class). Java agents can
> already emulate this privilege as described above, this is therefore merely
> a convenience.
> b) Add a module jdk.test to JDK 11 with a class
> JavaTest::getInstrumentation that returns an instrumentation instance for
> the current JVM. This module should not be resolved by default but only
> when requiring it explicitly similar to the EE modules after Java 9.
>
> I think this solution carries two benefits:
>
> a) Test libraries like Mockito can only be used in a testing scope. We
> experience regularly that Mockito is used in production environments. The
> library is not meant for that and we warn and document that this is not
> intended use but people regularly ignore this directive. By requiring this
> module, this form of abuse would no longer be possible and the JVM could
> even give a meaningful error message if this use was intended.
> b) The Instrumentation instance can be used for other meaningful testing
> approaches. For example, Mockito offers an inline mock maker where the
> mocking logic is inlined into a method body rather then creating a
> subclass. This approach mainly targets final classes which have become more
> common especially since the Kotlin language became popular. To supply this
> alternative mock maker, Mockito attempts attaching an agent to the current
> VM (directly or indirectly, depending on the VM's version) which suffers
> the additional costs for attaching an agent that I mentioned before.
>
> Thanks for reading this and I hope that you can consider these, as of
> today, very practiced use cases. The JPMS migration has gone quite well, I
> find. With these outstanding problems, JDK 11 could be the first release
> where a majority of Java programs would no longer need to rely on internal
> API.
>
> Best regards, Rafael
>
Jochen Theodorou
2018-04-03 22:24:19 UTC
Permalink
On 03.04.2018 21:26, Henri Tremblay wrote:
[...]
> For completeness, there are 4 ways to create a class without calling a
> constructor right now that I'm aware of:
>
> - Unsafe.allocateInstance

which is supposed to go away at some point

> - sun.reflect.ReflectionFactory.newConstructorForSerialization (my
> favorite because it's the fastest one)

which afaik works in java9 but is also one of those critical doomed APIs

> - Generate an extending class forgetting to call the super constructor
> (so it's not exactly that same class that is instantiated). It requires
> -Xverify:none

Is this really an option for a production environment?

> - Generate a class extending MagicAccessorImpl that will then
> instantiates the wanted class but calling the wrong constructor

Is jdk.internal.reflect.MagicAccessorImpl still usable in Java9+? I
thought this is no longer exported

Under the premise that all critical API usages will be removed in the
future and replacement APIs will be created I think we might indeed
still miss something here

bye Jochen
Michael Rasmussen
2018-04-04 13:27:18 UTC
Permalink
I can echo a lot of what Rafael said.

JRebel is also a heavy user of the mentioned APIs here, especially Unsafe.defineClass and Unsafe.allocateInstance -- we actually use all 4 ways mentioned for creating an instance without calling the constructor (depending on Java version and vendor).
Like testing frameworks, JRebel is also something minded for a development environment, and not a production environment.
Yes, JRebel can be quite invasive with how we instrument things, and if no official API exists for doing these things, we generally dig in the private APIs until we find it; but an official supported API would always be preferable and to me the Instrumentation API does seems like a good fit for this kind of API.

One of the main things about Unsafe.defineClass is that it can define classes in the boot (null) classloader (and in any package). We use this since we sometimes need to define companion classes in the same package as an existing on, including classes defined in the boot classloader.

/Michael


From: jigsaw-dev <jigsaw-dev-***@openjdk.java.net> on behalf of Jochen Theodorou <***@gmx.org>
Sent: 04 April 2018 01:24
To: jigsaw-***@openjdk.java.net
Subject: Re: Avoiding sun.misc.Unsafe and embracing modules in Java libraries: missing links
 

On 03.04.2018 21:26, Henri Tremblay wrote:
[...]
> For completeness, there are 4 ways to create a class without calling a
> constructor right now that I'm aware of:
>
>     - Unsafe.allocateInstance

which is supposed to go away at some point

>     - sun.reflect.ReflectionFactory.newConstructorForSerialization (my
>     favorite because it's the fastest one)

which afaik works in java9 but is also one of those critical doomed APIs

>     - Generate an extending class forgetting to call the super constructor
>     (so it's not exactly that same class that is instantiated). It requires
>     -Xverify:none

Is this really an option for a production environment?

>     - Generate a class extending MagicAccessorImpl that will then
>     instantiates the wanted class but calling the wrong constructor

Is jdk.internal.reflect.MagicAccessorImpl still usable in Java9+? I
thought this is no longer exported

Under the premise that all critical API usages will be removed in the
future and replacement APIs will be created I think we might indeed
still miss something here

bye Jochen
Rafael Winterhalter
2018-04-04 12:05:06 UTC
Permalink
Hei Henri,

for Maven, Gradle and IDEs I think it is sensible to expect that these
runners would resolve such a test module unless it is explicitly specified
otherwise. Already today, those runners patch the module under test in
order to include the test code. I would expect of these runners to adopt
similarly to a test module as they adopted to the module system in general.

As for Spring's use case, I believe that the approach was chosen such that
class proxies resemble interface proxies as much as possible. In general
however, I think that Spring could just generate a subclass proxy and
delegate to the overridden methods instead of delegating to an instance of
the proxied class. If the proxy class mimics the proxied class's
constructors' signatures, this proxy can be instantiated exactly like the
proxied class would have been. This way, using Objenesis and the like is no
longer necessary and I argue that one should avoid exposing this
functionality if there is a reasonable alternative that does not require
any derivation from Javas programming model. As a matter of fact, I often
find developers confused about how fields are not set on such Spring
proxies and I even found possible exploits in code because of it. For
example, imagine a class:

class Foo {
public final production;
Foo(boolean production) { this.production = production; } // Only call
from unit test
public Foo() { this(true); }
public void doSomethingSensible() {
if (production) { doSecurityCheck(); }
// do something ...
}
}

With Objenesis, you can create an instance of the above class and set it to
test mode with the production field being false as the default value. In
contrast, for the case of serialization, I already argued that support for
serializable types should probably go into MethodHandles.Lookup similar to
how defineClass was added. This does not render an additional security
threat, if Foo was serializable I could just synthetically create a
serialized vale of an instance with production being false and deserialize
this value in a production environment. On a side note, Spring would create
a class with the above field value automatically if Foo was a bean, making
this exploit very easy even when using a security manager as the proxy
instance is normally available to all sorts of code.

Finally, I disagree with the argument that this sort of API should be
accessible to "standard code" only shielded by a security manager because
this feature is useful today. The module system becomes useless the moment
you punch even a little hole into it. Therefore, I find it important to
remove sun.misc.Unsafe as quickly as possible as the module system can be
easily circumvented until it is removed and since the JPMS only show its
full benefit once this is no longer possible. But in order to keep testing
frameworks that have a good reason to break module boundaries up and
running, I think adding a test modules is a reasonable compromise. As an
additional benefit, it would allow libraries like Mockito that should not
be used in production to self-identify.

One could even argue that this ability to instantiate instance is nothing
that needs to be exposed to a Java agent so maybe this functionality could
also be part of the mentioned test module instead of being a part of
instrumentation. On the other hand, if there is a use case not being
considered for a Java agent, this agent might be tempted to open
jdk.internal.misc to gain this access, one might therefore just decide to
add the method to Instrumentation to offer an easy migration path to
applications that aim to migrate their code with a minimal delta.

Best regards, Rafael

2018-04-03 21:26 GMT+02:00 Henri Tremblay <***@gmail.com>:

> Hi Rafael,
>
> I don't like the idea to have to explicitly load a module to create mocks.
>
> Because Unsafe.allocateInstance is of course used to create mock but also
> proxies. For instance, Spring uses it extensively through Objenesis. And
> different frameworks doing serialization.
>
> Also, It means Gradle and Maven would need to be updated to load the
> module automatically in test context. And all IDE. A lot of annoyance for
> not much benefit since everybody is currently happy.
>
> I've been advocating that if creating a class without calling a
> constructor is useless in multiple contexts, it should be easily available
> through the API. I don't know exactly where to put it but it should be
>
> - Available to all (java.base)
> - Protected by the security manager
>
> For completeness, there are 4 ways to create a class without calling a
> constructor right now that I'm aware of:
>
> - Unsafe.allocateInstance
> - sun.reflect.ReflectionFactory.newConstructorForSerialization (my
> favorite because it's the fastest one)
> - Generate an extending class forgetting to call the super constructor
> (so it's not exactly that same class that is instantiated). It requires
> -Xverify:none
> - Generate a class extending MagicAccessorImpl that will then
> instantiates the wanted class but calling the wrong constructor
>
> Regards,
> Henri
>
>
>
> On 1 April 2018 at 17:02, Rafael Winterhalter <***@gmail.com>
> wrote:
>
>> Hello,
>>
>> I am the/a maintainer of the libraries Byte Buddy, cglib and Mockito with
>> countless dependencies upstream and I wanted to give a summary of adopting
>> the JPMS and migrating away from sun.misc.Unsafe.
>>
>> 1. Java agents cannot define auxiliary classes.
>>
>> Byte Buddy does support the JPMS fully, however, it still relies on
>> sun.misc.Unsafe::defineClass for its Java agent API and currently breaks
>> on
>> Java 11 as this method was removed in a recent EA build. The reason for
>> using Unsafe is that many instrumentations need to define auxiliary
>> classes
>> to aid an instrumentation similar to javac which sometimes needs to define
>> anonymous classes or even synthetic classes. For example, if a Java agent
>> wants to register an event listener to some framework, such listeners
>> often
>> declare multiple methods what makes it impossible to fullfil the listener
>> contract using a lambda expression. Instead, one typically injects an
>> additional class into the same package as the instrumented class. In this
>> case, it is not possible to use MethodHandles.Lookup::defineClass as the
>> class file transformer does not necessarily have private access to the
>> lookup of the instrumented class.
>>
>> The current workarounds are:
>>
>> a) Open the package jdk.internal.misc to gain access to this package's
>> Unsafe class. This can be done via Instrumentation::redefineModule.
>> b) Open the java.lang package to access ClassLoader via reflection.
>> c) Open the java.lang package to access the internal lookup with global
>> access rights.
>>
>> Of these solutions only (b) relies on standard API and is guaranteed to
>> function in the future but the solution still feels hacky and does not
>> work
>> for instrumentations of classes on the bootstrap loader. Opening packages
>> also implies a risk of being applied carelessly since opening the package
>> to the agent's module most likely opens the package to the unnamed module
>> of the system class loader what invites to breaches of the JPMS
>> encapsulation by code that does not ship with the agent.
>>
>> To offer a better solution, I would like to suggest one of the following:
>>
>> a) Add a method defineClass(ClassLoader, byte[], ProtectionDomain) to the
>> Instrumentation interface that works similar to Unsafe::defineClass. This
>> would provide a very simple migration path. Since agents have access to
>> jdk.internal.misc, adding this method does not add any capabilities to the
>> agent, it merley avoids using internal API that might change.
>> b) Supply a MethodHandles.Lookup instance to the
>> ClassFileTransformer::transform API where the instance represents the
>> instrumented class's access rights. This does however carry the risk of
>> invoking the lookupClass method which would either load the instrumented
>> class prematurely causing a circularity error or return an unexpected
>> value
>> such as null. Since the lookup API generally relies on loaded types, there
>> are probably other problems such as invoking Lookup::privateLookupIn
>> before
>> all involved types are loaded.
>>
>> For the sake of simlicity and since easy migration paths make a quick
>> adoption easier, I would suggestion solution (a), also in the light that
>> quick and dirty migrations might still choose option (b) to save time and
>> also since (b) might cause problems when types are not yet loaded.
>>
>> 2. Java proxies cannot invoke default methods of proxied interfaces
>>
>> The Java proxy API does not currently allow the invocation of an
>> overridden
>> default method since
>> the InvocationHandler API only supplies an instance of
>> java.lang.reflection.Method. In Java 8, it was always possible to get hold
>> of method handle of any method of the proxied interface and to specialize
>> the handle on the interface type to invoke the default implementation.
>> With
>> the JPMS, even if an interface type is exported, this same type might not
>> be opened to another module. This implies that if an InvocationHandler is
>> defined by this module to which the interface is exported, this module's
>> InvocationHandler cannot resolve a specialized method handle to a default
>> method of the proxied interface. As a matter of fact, such a call is
>> impossible in this scenario whereas the same call is possible if the proxy
>> is implemented manually at compile time.
>>
>> As a solution, I suggest to provide an argument to the InvocationHandler
>> that represents a lookup instance with the access rights of the proxy
>> class. Using this lookup, the specialized method handles could be
>> resolved.
>>
>> 3. Mocking and serialization libraries still require
>> Unsafe::allocateInstance.
>>
>> For Mockito, it is required to instantiate any class without invoking a
>> constructor with potential side-effects. This is of course a grose
>> violation of Java's general contract for class instantiation but this
>> feature is very useful.
>>
>> Using a Java agent, it is already possible to emulate this feature without
>> internal API by instrumenting all constructors of all classes in the
>> hierarchy of a mocked class by transforming all constructors into the
>> following pseudo-code:
>>
>> SomeConstructor(Void arg) {
>> if (MockitoBootHelper.THREAD_LOCAL.isMockInstantiatingMode()) {
>> super(null); // any constructor of the super class with default values
>> for all arguments
>> } else {
>> // original constructor code...
>> }
>> }
>>
>> Before instantiating a mock, the thread local value that is held by a
>> bootstrap-loader injected class is set to true such that a side-effect
>> free
>> construction is achieved.
>>
>> This is of course too expensive and has negative effects on performance
>> due
>> to additional branching and JIT-byte code limits such that one would
>> rather
>> open jdk.internal.misc to access the Unsafe instantiation mechanism if a
>> Java agent is already available.
>>
>> However, mocking and serialization libraries are not typically loaded as a
>> Java agent. Also, I think that the actual requirements are different. My
>> suggestion here is:
>>
>> a) For serialization libraries, I think that adding
>> MethodHandles.Lookup::newInstance(Class<? extends Serializable>) with
>> standard deserialization mechanics would be sufficient.
>> b) For mocking libraries, this does not suffice as mocks can be of any
>> class. I understand that this breaks encapsulation but for unit tests, I
>> argue that the benefit of using these libraries outweights the benefit of
>> full encapsulation within a unit test.
>>
>> As Mockito is typically run within a build which uses a JDK, we could
>> attach to the current VM using the attachment API. Since Java 9, this is
>> no
>> longer possible to attach to the same VM but instead we start a helper JVM
>> process that applies the attachment indirectly. Unfortunately, this is a
>> rather costly operation what is especially problematic when running a
>> single unit test. (The difference to this approach over Unsafe is about
>> half a second on average.)
>>
>> To overcome this, I would like to suggest to:
>>
>> a) Add a method Instrumentation::allocateInstance(Class). Java agents can
>> already emulate this privilege as described above, this is therefore
>> merely
>> a convenience.
>> b) Add a module jdk.test to JDK 11 with a class
>> JavaTest::getInstrumentation that returns an instrumentation instance for
>> the current JVM. This module should not be resolved by default but only
>> when requiring it explicitly similar to the EE modules after Java 9.
>>
>> I think this solution carries two benefits:
>>
>> a) Test libraries like Mockito can only be used in a testing scope. We
>> experience regularly that Mockito is used in production environments. The
>> library is not meant for that and we warn and document that this is not
>> intended use but people regularly ignore this directive. By requiring this
>> module, this form of abuse would no longer be possible and the JVM could
>> even give a meaningful error message if this use was intended.
>> b) The Instrumentation instance can be used for other meaningful testing
>> approaches. For example, Mockito offers an inline mock maker where the
>> mocking logic is inlined into a method body rather then creating a
>> subclass. This approach mainly targets final classes which have become
>> more
>> common especially since the Kotlin language became popular. To supply this
>> alternative mock maker, Mockito attempts attaching an agent to the current
>> VM (directly or indirectly, depending on the VM's version) which suffers
>> the additional costs for attaching an agent that I mentioned before.
>>
>> Thanks for reading this and I hope that you can consider these, as of
>> today, very practiced use cases. The JPMS migration has gone quite well, I
>> find. With these outstanding problems, JDK 11 could be the first release
>> where a majority of Java programs would no longer need to rely on internal
>> API.
>>
>> Best regards, Rafael
>>
>
>
Henri Tremblay
2018-04-06 03:37:18 UTC
Permalink
Answers inline

On 4 April 2018 at 08:05, Rafael Winterhalter <***@gmail.com> wrote:

> Hei Henri,
>
> for Maven, Gradle and IDEs I think it is sensible to expect that these
> runners would resolve such a test module unless it is explicitly specified
> otherwise. Already today, those runners patch the module under test in
> order to include the test code. I would expect of these runners to adopt
> similarly to a test module as they adopted to the module system in general.
>
> -> Yes. I agree.


> As for Spring's use case, I believe that the approach was chosen such that
> class proxies resemble interface proxies as much as possible. In general
> however, I think that Spring could just generate a subclass proxy and
> delegate to the overridden methods instead of delegating to an instance of
> the proxied class. If the proxy class mimics the proxied class's
> constructors' signatures, this proxy can be instantiated exactly like the
> proxied class would have been. This way, using Objenesis and the like is no
> longer necessary and I argue that one should avoid exposing this
> functionality if there is a reasonable alternative that does not require
> any derivation from Javas programming model. As a matter of fact, I often
> find developers confused about how fields are not set on such Spring
> proxies and I even found possible exploits in code because of it. For
> example, imagine a class:
>
> -> I think extending and delegating to the super class was the case a long
time ago but was causing all sorts of problems. I agree that proxying and
delegating to the real class also causes other problems. We should ask them
what caused then to pick the later. Doing that, we also need to be cautious
about final methods.

class Foo {
> public final production;
> Foo(boolean production) { this.production = production; } // Only call
> from unit test
> public Foo() { this(true); }
> public void doSomethingSensible() {
> if (production) { doSecurityCheck(); }
> // do something ...
> }
> }
>
> With Objenesis, you can create an instance of the above class and set it
> to test mode with the production field being false as the default value. In
> contrast, for the case of serialization, I already argued that support for
> serializable types should probably go into MethodHandles.Lookup similar to
> how defineClass was added. This does not render an additional security
> threat, if Foo was serializable I could just synthetically create a
> serialized vale of an instance with production being false and deserialize
> this value in a production environment. On a side note, Spring would create
> a class with the above field value automatically if Foo was a bean, making
> this exploit very easy even when using a security manager as the proxy
> instance is normally available to all sorts of code.
>
> Finally, I disagree with the argument that this sort of API should be
> accessible to "standard code" only shielded by a security manager because
> this feature is useful today. The module system becomes useless the moment
> you punch even a little hole into it. Therefore, I find it important to
> remove sun.misc.Unsafe as quickly as possible as the module system can be
> easily circumvented until it is removed and since the JPMS only show its
> full benefit once this is no longer possible. But in order to keep testing
> frameworks that have a good reason to break module boundaries up and
> running, I think adding a test modules is a reasonable compromise. As an
> additional benefit, it would allow libraries like Mockito that should not
> be used in production to self-identify.
>

-> I don't see the breach in JPMS in particular. Reflection is restricted.
Creating a class without calling a constructor is reflection.
-> Using mockito in production is weird. Using Mockito in a stubbing server
can make sense. But you can advocate it requires to add some parameters to
the JVM to make it work.

>
> One could even argue that this ability to instantiate instance is nothing
> that needs to be exposed to a Java agent so maybe this functionality could
> also be part of the mentioned test module instead of being a part of
> instrumentation. On the other hand, if there is a use case not being
> considered for a Java agent, this agent might be tempted to open
> jdk.internal.misc to gain this access, one might therefore just decide to
> add the method to Instrumentation to offer an easy migration path to
> applications that aim to migrate their code with a minimal delta.
>
> Best regards, Rafael
>
> 2018-04-03 21:26 GMT+02:00 Henri Tremblay <***@gmail.com>:
>
>> Hi Rafael,
>>
>> I don't like the idea to have to explicitly load a module to create mocks.
>>
>> Because Unsafe.allocateInstance is of course used to create mock but also
>> proxies. For instance, Spring uses it extensively through Objenesis. And
>> different frameworks doing serialization.
>>
>> Also, It means Gradle and Maven would need to be updated to load the
>> module automatically in test context. And all IDE. A lot of annoyance for
>> not much benefit since everybody is currently happy.
>>
>> I've been advocating that if creating a class without calling a
>> constructor is useless in multiple contexts, it should be easily available
>> through the API. I don't know exactly where to put it but it should be
>>
>> - Available to all (java.base)
>> - Protected by the security manager
>>
>> For completeness, there are 4 ways to create a class without calling a
>> constructor right now that I'm aware of:
>>
>> - Unsafe.allocateInstance
>> - sun.reflect.ReflectionFactory.newConstructorForSerialization (my
>> favorite because it's the fastest one)
>> - Generate an extending class forgetting to call the super
>> constructor (so it's not exactly that same class that is instantiated). It
>> requires -Xverify:none
>> - Generate a class extending MagicAccessorImpl that will then
>> instantiates the wanted class but calling the wrong constructor
>>
>> Regards,
>> Henri
>>
>>
>>
>> On 1 April 2018 at 17:02, Rafael Winterhalter <***@gmail.com>
>> wrote:
>>
>>> Hello,
>>>
>>> I am the/a maintainer of the libraries Byte Buddy, cglib and Mockito with
>>> countless dependencies upstream and I wanted to give a summary of
>>> adopting
>>> the JPMS and migrating away from sun.misc.Unsafe.
>>>
>>> 1. Java agents cannot define auxiliary classes.
>>>
>>> Byte Buddy does support the JPMS fully, however, it still relies on
>>> sun.misc.Unsafe::defineClass for its Java agent API and currently breaks
>>> on
>>> Java 11 as this method was removed in a recent EA build. The reason for
>>> using Unsafe is that many instrumentations need to define auxiliary
>>> classes
>>> to aid an instrumentation similar to javac which sometimes needs to
>>> define
>>> anonymous classes or even synthetic classes. For example, if a Java agent
>>> wants to register an event listener to some framework, such listeners
>>> often
>>> declare multiple methods what makes it impossible to fullfil the listener
>>> contract using a lambda expression. Instead, one typically injects an
>>> additional class into the same package as the instrumented class. In this
>>> case, it is not possible to use MethodHandles.Lookup::defineClass as the
>>> class file transformer does not necessarily have private access to the
>>> lookup of the instrumented class.
>>>
>>> The current workarounds are:
>>>
>>> a) Open the package jdk.internal.misc to gain access to this package's
>>> Unsafe class. This can be done via Instrumentation::redefineModule.
>>> b) Open the java.lang package to access ClassLoader via reflection.
>>> c) Open the java.lang package to access the internal lookup with global
>>> access rights.
>>>
>>> Of these solutions only (b) relies on standard API and is guaranteed to
>>> function in the future but the solution still feels hacky and does not
>>> work
>>> for instrumentations of classes on the bootstrap loader. Opening packages
>>> also implies a risk of being applied carelessly since opening the package
>>> to the agent's module most likely opens the package to the unnamed module
>>> of the system class loader what invites to breaches of the JPMS
>>> encapsulation by code that does not ship with the agent.
>>>
>>> To offer a better solution, I would like to suggest one of the following:
>>>
>>> a) Add a method defineClass(ClassLoader, byte[], ProtectionDomain) to the
>>> Instrumentation interface that works similar to Unsafe::defineClass. This
>>> would provide a very simple migration path. Since agents have access to
>>> jdk.internal.misc, adding this method does not add any capabilities to
>>> the
>>> agent, it merley avoids using internal API that might change.
>>> b) Supply a MethodHandles.Lookup instance to the
>>> ClassFileTransformer::transform API where the instance represents the
>>> instrumented class's access rights. This does however carry the risk of
>>> invoking the lookupClass method which would either load the instrumented
>>> class prematurely causing a circularity error or return an unexpected
>>> value
>>> such as null. Since the lookup API generally relies on loaded types,
>>> there
>>> are probably other problems such as invoking Lookup::privateLookupIn
>>> before
>>> all involved types are loaded.
>>>
>>> For the sake of simlicity and since easy migration paths make a quick
>>> adoption easier, I would suggestion solution (a), also in the light that
>>> quick and dirty migrations might still choose option (b) to save time and
>>> also since (b) might cause problems when types are not yet loaded.
>>>
>>> 2. Java proxies cannot invoke default methods of proxied interfaces
>>>
>>> The Java proxy API does not currently allow the invocation of an
>>> overridden
>>> default method since
>>> the InvocationHandler API only supplies an instance of
>>> java.lang.reflection.Method. In Java 8, it was always possible to get
>>> hold
>>> of method handle of any method of the proxied interface and to specialize
>>> the handle on the interface type to invoke the default implementation.
>>> With
>>> the JPMS, even if an interface type is exported, this same type might not
>>> be opened to another module. This implies that if an InvocationHandler is
>>> defined by this module to which the interface is exported, this module's
>>> InvocationHandler cannot resolve a specialized method handle to a default
>>> method of the proxied interface. As a matter of fact, such a call is
>>> impossible in this scenario whereas the same call is possible if the
>>> proxy
>>> is implemented manually at compile time.
>>>
>>> As a solution, I suggest to provide an argument to the InvocationHandler
>>> that represents a lookup instance with the access rights of the proxy
>>> class. Using this lookup, the specialized method handles could be
>>> resolved.
>>>
>>> 3. Mocking and serialization libraries still require
>>> Unsafe::allocateInstance.
>>>
>>> For Mockito, it is required to instantiate any class without invoking a
>>> constructor with potential side-effects. This is of course a grose
>>> violation of Java's general contract for class instantiation but this
>>> feature is very useful.
>>>
>>> Using a Java agent, it is already possible to emulate this feature
>>> without
>>> internal API by instrumenting all constructors of all classes in the
>>> hierarchy of a mocked class by transforming all constructors into the
>>> following pseudo-code:
>>>
>>> SomeConstructor(Void arg) {
>>> if (MockitoBootHelper.THREAD_LOCAL.isMockInstantiatingMode()) {
>>> super(null); // any constructor of the super class with default
>>> values
>>> for all arguments
>>> } else {
>>> // original constructor code...
>>> }
>>> }
>>>
>>> Before instantiating a mock, the thread local value that is held by a
>>> bootstrap-loader injected class is set to true such that a side-effect
>>> free
>>> construction is achieved.
>>>
>>> This is of course too expensive and has negative effects on performance
>>> due
>>> to additional branching and JIT-byte code limits such that one would
>>> rather
>>> open jdk.internal.misc to access the Unsafe instantiation mechanism if a
>>> Java agent is already available.
>>>
>>> However, mocking and serialization libraries are not typically loaded as
>>> a
>>> Java agent. Also, I think that the actual requirements are different. My
>>> suggestion here is:
>>>
>>> a) For serialization libraries, I think that adding
>>> MethodHandles.Lookup::newInstance(Class<? extends Serializable>) with
>>> standard deserialization mechanics would be sufficient.
>>> b) For mocking libraries, this does not suffice as mocks can be of any
>>> class. I understand that this breaks encapsulation but for unit tests, I
>>> argue that the benefit of using these libraries outweights the benefit of
>>> full encapsulation within a unit test.
>>>
>>> As Mockito is typically run within a build which uses a JDK, we could
>>> attach to the current VM using the attachment API. Since Java 9, this is
>>> no
>>> longer possible to attach to the same VM but instead we start a helper
>>> JVM
>>> process that applies the attachment indirectly. Unfortunately, this is a
>>> rather costly operation what is especially problematic when running a
>>> single unit test. (The difference to this approach over Unsafe is about
>>> half a second on average.)
>>>
>>> To overcome this, I would like to suggest to:
>>>
>>> a) Add a method Instrumentation::allocateInstance(Class). Java agents
>>> can
>>> already emulate this privilege as described above, this is therefore
>>> merely
>>> a convenience.
>>> b) Add a module jdk.test to JDK 11 with a class
>>> JavaTest::getInstrumentation that returns an instrumentation instance for
>>> the current JVM. This module should not be resolved by default but only
>>> when requiring it explicitly similar to the EE modules after Java 9.
>>>
>>> I think this solution carries two benefits:
>>>
>>> a) Test libraries like Mockito can only be used in a testing scope. We
>>> experience regularly that Mockito is used in production environments. The
>>> library is not meant for that and we warn and document that this is not
>>> intended use but people regularly ignore this directive. By requiring
>>> this
>>> module, this form of abuse would no longer be possible and the JVM could
>>> even give a meaningful error message if this use was intended.
>>> b) The Instrumentation instance can be used for other meaningful testing
>>> approaches. For example, Mockito offers an inline mock maker where the
>>> mocking logic is inlined into a method body rather then creating a
>>> subclass. This approach mainly targets final classes which have become
>>> more
>>> common especially since the Kotlin language became popular. To supply
>>> this
>>> alternative mock maker, Mockito attempts attaching an agent to the
>>> current
>>> VM (directly or indirectly, depending on the VM's version) which suffers
>>> the additional costs for attaching an agent that I mentioned before.
>>>
>>> Thanks for reading this and I hope that you can consider these, as of
>>> today, very practiced use cases. The JPMS migration has gone quite well,
>>> I
>>> find. With these outstanding problems, JDK 11 could be the first release
>>> where a majority of Java programs would no longer need to rely on
>>> internal
>>> API.
>>>
>>> Best regards, Rafael
>>>
>>
>>
>
Rafael Winterhalter
2018-04-06 17:20:52 UTC
Permalink
As for final methods, this is a problem with delegating and subclassing
proxies. For delegation, I would even claim that this is a bigger problem
as the original code is invoked on an unprepared instance. With
subclassing, the final method is simply invoked in its non-proxied form
what should retain consistent state. Also, subclassing proxies can emulate
self-invocation without proxying by setting a flag on the instance that is
reset after the proxied instance completes its super method call. Finally,
subclassing proxies avoid confusion when using instances as monitors etc.

The problem with creating instances without invoking a constructor that I
see when using the lookup facility, for instance, is that this lookup would
need access to the entire class hierarchy. If I defined a class Foo extends
Bar extends Object and Foo and Bar resided in different packages and I
wanted to invoke an instance of Foo without a constructor call, my lookup
might have the right access for Foo but not for Bar. Should this then
require another lookup to skip Bar's constructor as well. And if not, could
I not just define a subclass of Foo, get a handle for it and now
instantiate a subclass of Foo that can be used as an instance of Foo? This
would break both Foo's and Bar's encapsulation without enforcing any
restriction.

I still find instantiating non-serializable classes without constructor
calls not meaningful for production environments, at least I could not
think of a good use case. For a stubbing server, I claim that the mocking
must work on a higher level then an object instance. Since such a server
needs deployment, I would also call this server a production application
even if it might never run in a production environment. As for application
tests, it can make sense for mocking and other unit testing frameworks and
I think a Java agent would be a good faciliator as owners of an
Instrumentation instance already can emulate this behavior as I had
described above.

Best regards, Rafael



2018-04-06 5:37 GMT+02:00 Henri Tremblay <***@gmail.com>:

> Answers inline
>
> On 4 April 2018 at 08:05, Rafael Winterhalter <***@gmail.com>
> wrote:
>
>> Hei Henri,
>>
>> for Maven, Gradle and IDEs I think it is sensible to expect that these
>> runners would resolve such a test module unless it is explicitly specified
>> otherwise. Already today, those runners patch the module under test in
>> order to include the test code. I would expect of these runners to adopt
>> similarly to a test module as they adopted to the module system in general.
>>
>> -> Yes. I agree.
>
>
>> As for Spring's use case, I believe that the approach was chosen such
>> that class proxies resemble interface proxies as much as possible. In
>> general however, I think that Spring could just generate a subclass proxy
>> and delegate to the overridden methods instead of delegating to an instance
>> of the proxied class. If the proxy class mimics the proxied class's
>> constructors' signatures, this proxy can be instantiated exactly like the
>> proxied class would have been. This way, using Objenesis and the like is no
>> longer necessary and I argue that one should avoid exposing this
>> functionality if there is a reasonable alternative that does not require
>> any derivation from Javas programming model. As a matter of fact, I often
>> find developers confused about how fields are not set on such Spring
>> proxies and I even found possible exploits in code because of it. For
>> example, imagine a class:
>>
>> -> I think extending and delegating to the super class was the case a
> long time ago but was causing all sorts of problems. I agree that proxying
> and delegating to the real class also causes other problems. We should ask
> them what caused then to pick the later. Doing that, we also need to be
> cautious about final methods.
>
> class Foo {
>> public final production;
>> Foo(boolean production) { this.production = production; } // Only call
>> from unit test
>> public Foo() { this(true); }
>> public void doSomethingSensible() {
>> if (production) { doSecurityCheck(); }
>> // do something ...
>> }
>> }
>>
>> With Objenesis, you can create an instance of the above class and set it
>> to test mode with the production field being false as the default value. In
>> contrast, for the case of serialization, I already argued that support for
>> serializable types should probably go into MethodHandles.Lookup similar to
>> how defineClass was added. This does not render an additional security
>> threat, if Foo was serializable I could just synthetically create a
>> serialized vale of an instance with production being false and deserialize
>> this value in a production environment. On a side note, Spring would create
>> a class with the above field value automatically if Foo was a bean, making
>> this exploit very easy even when using a security manager as the proxy
>> instance is normally available to all sorts of code.
>>
>> Finally, I disagree with the argument that this sort of API should be
>> accessible to "standard code" only shielded by a security manager because
>> this feature is useful today. The module system becomes useless the moment
>> you punch even a little hole into it. Therefore, I find it important to
>> remove sun.misc.Unsafe as quickly as possible as the module system can be
>> easily circumvented until it is removed and since the JPMS only show its
>> full benefit once this is no longer possible. But in order to keep testing
>> frameworks that have a good reason to break module boundaries up and
>> running, I think adding a test modules is a reasonable compromise. As an
>> additional benefit, it would allow libraries like Mockito that should not
>> be used in production to self-identify.
>>
>
> -> I don't see the breach in JPMS in particular. Reflection is
> restricted. Creating a class without calling a constructor is reflection.
> -> Using mockito in production is weird. Using Mockito in a stubbing
> server can make sense. But you can advocate it requires to add some
> parameters to the JVM to make it work.
>
>>
>> One could even argue that this ability to instantiate instance is nothing
>> that needs to be exposed to a Java agent so maybe this functionality could
>> also be part of the mentioned test module instead of being a part of
>> instrumentation. On the other hand, if there is a use case not being
>> considered for a Java agent, this agent might be tempted to open
>> jdk.internal.misc to gain this access, one might therefore just decide to
>> add the method to Instrumentation to offer an easy migration path to
>> applications that aim to migrate their code with a minimal delta.
>>
>> Best regards, Rafael
>>
>> 2018-04-03 21:26 GMT+02:00 Henri Tremblay <***@gmail.com>:
>>
>>> Hi Rafael,
>>>
>>> I don't like the idea to have to explicitly load a module to create
>>> mocks.
>>>
>>> Because Unsafe.allocateInstance is of course used to create mock but
>>> also proxies. For instance, Spring uses it extensively through Objenesis.
>>> And different frameworks doing serialization.
>>>
>>> Also, It means Gradle and Maven would need to be updated to load the
>>> module automatically in test context. And all IDE. A lot of annoyance for
>>> not much benefit since everybody is currently happy.
>>>
>>> I've been advocating that if creating a class without calling a
>>> constructor is useless in multiple contexts, it should be easily available
>>> through the API. I don't know exactly where to put it but it should be
>>>
>>> - Available to all (java.base)
>>> - Protected by the security manager
>>>
>>> For completeness, there are 4 ways to create a class without calling a
>>> constructor right now that I'm aware of:
>>>
>>> - Unsafe.allocateInstance
>>> - sun.reflect.ReflectionFactory.newConstructorForSerialization (my
>>> favorite because it's the fastest one)
>>> - Generate an extending class forgetting to call the super
>>> constructor (so it's not exactly that same class that is instantiated). It
>>> requires -Xverify:none
>>> - Generate a class extending MagicAccessorImpl that will then
>>> instantiates the wanted class but calling the wrong constructor
>>>
>>> Regards,
>>> Henri
>>>
>>>
>>>
>>> On 1 April 2018 at 17:02, Rafael Winterhalter <***@gmail.com>
>>> wrote:
>>>
>>>> Hello,
>>>>
>>>> I am the/a maintainer of the libraries Byte Buddy, cglib and Mockito
>>>> with
>>>> countless dependencies upstream and I wanted to give a summary of
>>>> adopting
>>>> the JPMS and migrating away from sun.misc.Unsafe.
>>>>
>>>> 1. Java agents cannot define auxiliary classes.
>>>>
>>>> Byte Buddy does support the JPMS fully, however, it still relies on
>>>> sun.misc.Unsafe::defineClass for its Java agent API and currently
>>>> breaks on
>>>> Java 11 as this method was removed in a recent EA build. The reason for
>>>> using Unsafe is that many instrumentations need to define auxiliary
>>>> classes
>>>> to aid an instrumentation similar to javac which sometimes needs to
>>>> define
>>>> anonymous classes or even synthetic classes. For example, if a Java
>>>> agent
>>>> wants to register an event listener to some framework, such listeners
>>>> often
>>>> declare multiple methods what makes it impossible to fullfil the
>>>> listener
>>>> contract using a lambda expression. Instead, one typically injects an
>>>> additional class into the same package as the instrumented class. In
>>>> this
>>>> case, it is not possible to use MethodHandles.Lookup::defineClass as
>>>> the
>>>> class file transformer does not necessarily have private access to the
>>>> lookup of the instrumented class.
>>>>
>>>> The current workarounds are:
>>>>
>>>> a) Open the package jdk.internal.misc to gain access to this package's
>>>> Unsafe class. This can be done via Instrumentation::redefineModule.
>>>> b) Open the java.lang package to access ClassLoader via reflection.
>>>> c) Open the java.lang package to access the internal lookup with global
>>>> access rights.
>>>>
>>>> Of these solutions only (b) relies on standard API and is guaranteed to
>>>> function in the future but the solution still feels hacky and does not
>>>> work
>>>> for instrumentations of classes on the bootstrap loader. Opening
>>>> packages
>>>> also implies a risk of being applied carelessly since opening the
>>>> package
>>>> to the agent's module most likely opens the package to the unnamed
>>>> module
>>>> of the system class loader what invites to breaches of the JPMS
>>>> encapsulation by code that does not ship with the agent.
>>>>
>>>> To offer a better solution, I would like to suggest one of the
>>>> following:
>>>>
>>>> a) Add a method defineClass(ClassLoader, byte[], ProtectionDomain) to
>>>> the
>>>> Instrumentation interface that works similar to Unsafe::defineClass.
>>>> This
>>>> would provide a very simple migration path. Since agents have access to
>>>> jdk.internal.misc, adding this method does not add any capabilities to
>>>> the
>>>> agent, it merley avoids using internal API that might change.
>>>> b) Supply a MethodHandles.Lookup instance to the
>>>> ClassFileTransformer::transform API where the instance represents the
>>>> instrumented class's access rights. This does however carry the risk of
>>>> invoking the lookupClass method which would either load the instrumented
>>>> class prematurely causing a circularity error or return an unexpected
>>>> value
>>>> such as null. Since the lookup API generally relies on loaded types,
>>>> there
>>>> are probably other problems such as invoking Lookup::privateLookupIn
>>>> before
>>>> all involved types are loaded.
>>>>
>>>> For the sake of simlicity and since easy migration paths make a quick
>>>> adoption easier, I would suggestion solution (a), also in the light that
>>>> quick and dirty migrations might still choose option (b) to save time
>>>> and
>>>> also since (b) might cause problems when types are not yet loaded.
>>>>
>>>> 2. Java proxies cannot invoke default methods of proxied interfaces
>>>>
>>>> The Java proxy API does not currently allow the invocation of an
>>>> overridden
>>>> default method since
>>>> the InvocationHandler API only supplies an instance of
>>>> java.lang.reflection.Method. In Java 8, it was always possible to get
>>>> hold
>>>> of method handle of any method of the proxied interface and to
>>>> specialize
>>>> the handle on the interface type to invoke the default implementation.
>>>> With
>>>> the JPMS, even if an interface type is exported, this same type might
>>>> not
>>>> be opened to another module. This implies that if an InvocationHandler
>>>> is
>>>> defined by this module to which the interface is exported, this module's
>>>> InvocationHandler cannot resolve a specialized method handle to a
>>>> default
>>>> method of the proxied interface. As a matter of fact, such a call is
>>>> impossible in this scenario whereas the same call is possible if the
>>>> proxy
>>>> is implemented manually at compile time.
>>>>
>>>> As a solution, I suggest to provide an argument to the InvocationHandler
>>>> that represents a lookup instance with the access rights of the proxy
>>>> class. Using this lookup, the specialized method handles could be
>>>> resolved.
>>>>
>>>> 3. Mocking and serialization libraries still require
>>>> Unsafe::allocateInstance.
>>>>
>>>> For Mockito, it is required to instantiate any class without invoking a
>>>> constructor with potential side-effects. This is of course a grose
>>>> violation of Java's general contract for class instantiation but this
>>>> feature is very useful.
>>>>
>>>> Using a Java agent, it is already possible to emulate this feature
>>>> without
>>>> internal API by instrumenting all constructors of all classes in the
>>>> hierarchy of a mocked class by transforming all constructors into the
>>>> following pseudo-code:
>>>>
>>>> SomeConstructor(Void arg) {
>>>> if (MockitoBootHelper.THREAD_LOCAL.isMockInstantiatingMode()) {
>>>> super(null); // any constructor of the super class with default
>>>> values
>>>> for all arguments
>>>> } else {
>>>> // original constructor code...
>>>> }
>>>> }
>>>>
>>>> Before instantiating a mock, the thread local value that is held by a
>>>> bootstrap-loader injected class is set to true such that a side-effect
>>>> free
>>>> construction is achieved.
>>>>
>>>> This is of course too expensive and has negative effects on performance
>>>> due
>>>> to additional branching and JIT-byte code limits such that one would
>>>> rather
>>>> open jdk.internal.misc to access the Unsafe instantiation mechanism if a
>>>> Java agent is already available.
>>>>
>>>> However, mocking and serialization libraries are not typically loaded
>>>> as a
>>>> Java agent. Also, I think that the actual requirements are different. My
>>>> suggestion here is:
>>>>
>>>> a) For serialization libraries, I think that adding
>>>> MethodHandles.Lookup::newInstance(Class<? extends Serializable>) with
>>>> standard deserialization mechanics would be sufficient.
>>>> b) For mocking libraries, this does not suffice as mocks can be of any
>>>> class. I understand that this breaks encapsulation but for unit tests, I
>>>> argue that the benefit of using these libraries outweights the benefit
>>>> of
>>>> full encapsulation within a unit test.
>>>>
>>>> As Mockito is typically run within a build which uses a JDK, we could
>>>> attach to the current VM using the attachment API. Since Java 9, this
>>>> is no
>>>> longer possible to attach to the same VM but instead we start a helper
>>>> JVM
>>>> process that applies the attachment indirectly. Unfortunately, this is a
>>>> rather costly operation what is especially problematic when running a
>>>> single unit test. (The difference to this approach over Unsafe is about
>>>> half a second on average.)
>>>>
>>>> To overcome this, I would like to suggest to:
>>>>
>>>> a) Add a method Instrumentation::allocateInstance(Class). Java agents
>>>> can
>>>> already emulate this privilege as described above, this is therefore
>>>> merely
>>>> a convenience.
>>>> b) Add a module jdk.test to JDK 11 with a class
>>>> JavaTest::getInstrumentation that returns an instrumentation instance
>>>> for
>>>> the current JVM. This module should not be resolved by default but only
>>>> when requiring it explicitly similar to the EE modules after Java 9.
>>>>
>>>> I think this solution carries two benefits:
>>>>
>>>> a) Test libraries like Mockito can only be used in a testing scope. We
>>>> experience regularly that Mockito is used in production environments.
>>>> The
>>>> library is not meant for that and we warn and document that this is not
>>>> intended use but people regularly ignore this directive. By requiring
>>>> this
>>>> module, this form of abuse would no longer be possible and the JVM could
>>>> even give a meaningful error message if this use was intended.
>>>> b) The Instrumentation instance can be used for other meaningful testing
>>>> approaches. For example, Mockito offers an inline mock maker where the
>>>> mocking logic is inlined into a method body rather then creating a
>>>> subclass. This approach mainly targets final classes which have become
>>>> more
>>>> common especially since the Kotlin language became popular. To supply
>>>> this
>>>> alternative mock maker, Mockito attempts attaching an agent to the
>>>> current
>>>> VM (directly or indirectly, depending on the VM's version) which suffers
>>>> the additional costs for attaching an agent that I mentioned before.
>>>>
>>>> Thanks for reading this and I hope that you can consider these, as of
>>>> today, very practiced use cases. The JPMS migration has gone quite
>>>> well, I
>>>> find. With these outstanding problems, JDK 11 could be the first release
>>>> where a majority of Java programs would no longer need to rely on
>>>> internal
>>>> API.
>>>>
>>>> Best regards, Rafael
>>>>
>>>
>>>
>>
>
Russell Gold
2018-04-03 19:48:41 UTC
Permalink
Hi Rafael,

This is the point of multi-release jars. There is a new supported way of creating classes. As of Java 9, you are supposed to do:

> return MethodHandles.privateLookupIn(anchorClass, MethodHandles.lookup())
> .dropLookupMode(MethodHandles.Lookup.PRIVATE);
> .defineClass(classBytes);

which doesn’t work before then, but does work in JDK 11. It requires some rework of your code, unfortunately, since you now have to pass in an anchor class to define the package in which your new class will live. I’ve just gotten this working in SimpleStub <http://simplestub.meterware.com/>, which also uses my easier way to create MR Jars.

- Russ


> On Apr 1, 2018, at 5:02 PM, Rafael Winterhalter <***@gmail.com> wrote:
>
> Hello,
>
> I am the/a maintainer of the libraries Byte Buddy, cglib and Mockito with
> countless dependencies upstream and I wanted to give a summary of adopting
> the JPMS and migrating away from sun.misc.Unsafe.
>
> 1. Java agents cannot define auxiliary classes.
>
> Byte Buddy does support the JPMS fully, however, it still relies on
> sun.misc.Unsafe::defineClass for its Java agent API and currently breaks on
> Java 11 as this method was removed in a recent EA build. The reason for
> using Unsafe is that many instrumentations need to define auxiliary classes
> to aid an instrumentation similar to javac which sometimes needs to define
> anonymous classes or even synthetic classes. For example, if a Java agent
> wants to register an event listener to some framework, such listeners often
> declare multiple methods what makes it impossible to fullfil the listener
> contract using a lambda expression. Instead, one typically injects an
> additional class into the same package as the instrumented class. In this
> case, it is not possible to use MethodHandles.Lookup::defineClass as the
> class file transformer does not necessarily have private access to the
> lookup of the instrumented class.
>
> The current workarounds are:
>
> a) Open the package jdk.internal.misc to gain access to this package's
> Unsafe class. This can be done via Instrumentation::redefineModule.
> b) Open the java.lang package to access ClassLoader via reflection.
> c) Open the java.lang package to access the internal lookup with global
> access rights.
>
> Of these solutions only (b) relies on standard API and is guaranteed to
> function in the future but the solution still feels hacky and does not work
> for instrumentations of classes on the bootstrap loader. Opening packages
> also implies a risk of being applied carelessly since opening the package
> to the agent's module most likely opens the package to the unnamed module
> of the system class loader what invites to breaches of the JPMS
> encapsulation by code that does not ship with the agent.
>
> To offer a better solution, I would like to suggest one of the following:
>
> a) Add a method defineClass(ClassLoader, byte[], ProtectionDomain) to the
> Instrumentation interface that works similar to Unsafe::defineClass. This
> would provide a very simple migration path. Since agents have access to
> jdk.internal.misc, adding this method does not add any capabilities to the
> agent, it merley avoids using internal API that might change.
> b) Supply a MethodHandles.Lookup instance to the
> ClassFileTransformer::transform API where the instance represents the
> instrumented class's access rights. This does however carry the risk of
> invoking the lookupClass method which would either load the instrumented
> class prematurely causing a circularity error or return an unexpected value
> such as null. Since the lookup API generally relies on loaded types, there
> are probably other problems such as invoking Lookup::privateLookupIn before
> all involved types are loaded.
>
> For the sake of simlicity and since easy migration paths make a quick
> adoption easier, I would suggestion solution (a), also in the light that
> quick and dirty migrations might still choose option (b) to save time and
> also since (b) might cause problems when types are not yet loaded.
>
> 2. Java proxies cannot invoke default methods of proxied interfaces
>
> The Java proxy API does not currently allow the invocation of an overridden
> default method since
> the InvocationHandler API only supplies an instance of
> java.lang.reflection.Method. In Java 8, it was always possible to get hold
> of method handle of any method of the proxied interface and to specialize
> the handle on the interface type to invoke the default implementation. With
> the JPMS, even if an interface type is exported, this same type might not
> be opened to another module. This implies that if an InvocationHandler is
> defined by this module to which the interface is exported, this module's
> InvocationHandler cannot resolve a specialized method handle to a default
> method of the proxied interface. As a matter of fact, such a call is
> impossible in this scenario whereas the same call is possible if the proxy
> is implemented manually at compile time.
>
> As a solution, I suggest to provide an argument to the InvocationHandler
> that represents a lookup instance with the access rights of the proxy
> class. Using this lookup, the specialized method handles could be resolved.
>
> 3. Mocking and serialization libraries still require
> Unsafe::allocateInstance.
>
> For Mockito, it is required to instantiate any class without invoking a
> constructor with potential side-effects. This is of course a grose
> violation of Java's general contract for class instantiation but this
> feature is very useful.
>
> Using a Java agent, it is already possible to emulate this feature without
> internal API by instrumenting all constructors of all classes in the
> hierarchy of a mocked class by transforming all constructors into the
> following pseudo-code:
>
> SomeConstructor(Void arg) {
> if (MockitoBootHelper.THREAD_LOCAL.isMockInstantiatingMode()) {
> super(null); // any constructor of the super class with default values
> for all arguments
> } else {
> // original constructor code...
> }
> }
>
> Before instantiating a mock, the thread local value that is held by a
> bootstrap-loader injected class is set to true such that a side-effect free
> construction is achieved.
>
> This is of course too expensive and has negative effects on performance due
> to additional branching and JIT-byte code limits such that one would rather
> open jdk.internal.misc to access the Unsafe instantiation mechanism if a
> Java agent is already available.
>
> However, mocking and serialization libraries are not typically loaded as a
> Java agent. Also, I think that the actual requirements are different. My
> suggestion here is:
>
> a) For serialization libraries, I think that adding
> MethodHandles.Lookup::newInstance(Class<? extends Serializable>) with
> standard deserialization mechanics would be sufficient.
> b) For mocking libraries, this does not suffice as mocks can be of any
> class. I understand that this breaks encapsulation but for unit tests, I
> argue that the benefit of using these libraries outweights the benefit of
> full encapsulation within a unit test.
>
> As Mockito is typically run within a build which uses a JDK, we could
> attach to the current VM using the attachment API. Since Java 9, this is no
> longer possible to attach to the same VM but instead we start a helper JVM
> process that applies the attachment indirectly. Unfortunately, this is a
> rather costly operation what is especially problematic when running a
> single unit test. (The difference to this approach over Unsafe is about
> half a second on average.)
>
> To overcome this, I would like to suggest to:
>
> a) Add a method Instrumentation::allocateInstance(Class). Java agents can
> already emulate this privilege as described above, this is therefore merely
> a convenience.
> b) Add a module jdk.test to JDK 11 with a class
> JavaTest::getInstrumentation that returns an instrumentation instance for
> the current JVM. This module should not be resolved by default but only
> when requiring it explicitly similar to the EE modules after Java 9.
>
> I think this solution carries two benefits:
>
> a) Test libraries like Mockito can only be used in a testing scope. We
> experience regularly that Mockito is used in production environments. The
> library is not meant for that and we warn and document that this is not
> intended use but people regularly ignore this directive. By requiring this
> module, this form of abuse would no longer be possible and the JVM could
> even give a meaningful error message if this use was intended.
> b) The Instrumentation instance can be used for other meaningful testing
> approaches. For example, Mockito offers an inline mock maker where the
> mocking logic is inlined into a method body rather then creating a
> subclass. This approach mainly targets final classes which have become more
> common especially since the Kotlin language became popular. To supply this
> alternative mock maker, Mockito attempts attaching an agent to the current
> VM (directly or indirectly, depending on the VM's version) which suffers
> the additional costs for attaching an agent that I mentioned before.
>
> Thanks for reading this and I hope that you can consider these, as of
> today, very practiced use cases. The JPMS migration has gone quite well, I
> find. With these outstanding problems, JDK 11 could be the first release
> where a majority of Java programs would no longer need to rely on internal
> API.
>
> Best regards, Rafael
Russell Gold
2018-04-03 20:05:39 UTC
Permalink
Meh. Answered without thinking it through.

I love this basic approach:

>>> b) Add a module jdk.test to JDK 11 with a class
>>> JavaTest::getInstrumentation that returns an instrumentation instance for
>>> the current JVM. This module should not be resolved by default but only
>>> when requiring it explicitly similar to the EE modules after Java 9.

Unit testing is a special case, and it seems more than reasonable to me that you should be able to do things when running unit tests that would never be permitted in a production environment. Since this module would only be resolved explicitly, it could contain capabilities customized for this special case, including things that currently use Unsafe or involve what appear to be hacks in the JDK.

That said, I do think that it is still possible to make a lot of unit testing libraries work, but it is getting harder.

> On Apr 3, 2018, at 3:48 PM, Russell Gold <***@oracle.com> wrote:
>
> Hi Rafael,
>
> This is the point of multi-release jars. There is a new supported way of creating classes. As of Java 9, you are supposed to do:
>
>> return MethodHandles.privateLookupIn(anchorClass, MethodHandles.lookup())
>> .dropLookupMode(MethodHandles.Lookup.PRIVATE);
>> .defineClass(classBytes);
>
> which doesn’t work before then, but does work in JDK 11. It requires some rework of your code, unfortunately, since you now have to pass in an anchor class to define the package in which your new class will live. I’ve just gotten this working in SimpleStub <http://simplestub.meterware.com/>, which also uses my easier way to create MR Jars.
>
> - Russ
>
>
>> On Apr 1, 2018, at 5:02 PM, Rafael Winterhalter <***@gmail.com> wrote:
>>
>> Hello,
>>
>> I am the/a maintainer of the libraries Byte Buddy, cglib and Mockito with
>> countless dependencies upstream and I wanted to give a summary of adopting
>> the JPMS and migrating away from sun.misc.Unsafe.
>>
>> 1. Java agents cannot define auxiliary classes.
>>
>> Byte Buddy does support the JPMS fully, however, it still relies on
>> sun.misc.Unsafe::defineClass for its Java agent API and currently breaks on
>> Java 11 as this method was removed in a recent EA build. The reason for
>> using Unsafe is that many instrumentations need to define auxiliary classes
>> to aid an instrumentation similar to javac which sometimes needs to define
>> anonymous classes or even synthetic classes. For example, if a Java agent
>> wants to register an event listener to some framework, such listeners often
>> declare multiple methods what makes it impossible to fullfil the listener
>> contract using a lambda expression. Instead, one typically injects an
>> additional class into the same package as the instrumented class. In this
>> case, it is not possible to use MethodHandles.Lookup::defineClass as the
>> class file transformer does not necessarily have private access to the
>> lookup of the instrumented class.
>>
>> The current workarounds are:
>>
>> a) Open the package jdk.internal.misc to gain access to this package's
>> Unsafe class. This can be done via Instrumentation::redefineModule.
>> b) Open the java.lang package to access ClassLoader via reflection.
>> c) Open the java.lang package to access the internal lookup with global
>> access rights.
>>
>> Of these solutions only (b) relies on standard API and is guaranteed to
>> function in the future but the solution still feels hacky and does not work
>> for instrumentations of classes on the bootstrap loader. Opening packages
>> also implies a risk of being applied carelessly since opening the package
>> to the agent's module most likely opens the package to the unnamed module
>> of the system class loader what invites to breaches of the JPMS
>> encapsulation by code that does not ship with the agent.
>>
>> To offer a better solution, I would like to suggest one of the following:
>>
>> a) Add a method defineClass(ClassLoader, byte[], ProtectionDomain) to the
>> Instrumentation interface that works similar to Unsafe::defineClass. This
>> would provide a very simple migration path. Since agents have access to
>> jdk.internal.misc, adding this method does not add any capabilities to the
>> agent, it merley avoids using internal API that might change.
>> b) Supply a MethodHandles.Lookup instance to the
>> ClassFileTransformer::transform API where the instance represents the
>> instrumented class's access rights. This does however carry the risk of
>> invoking the lookupClass method which would either load the instrumented
>> class prematurely causing a circularity error or return an unexpected value
>> such as null. Since the lookup API generally relies on loaded types, there
>> are probably other problems such as invoking Lookup::privateLookupIn before
>> all involved types are loaded.
>>
>> For the sake of simlicity and since easy migration paths make a quick
>> adoption easier, I would suggestion solution (a), also in the light that
>> quick and dirty migrations might still choose option (b) to save time and
>> also since (b) might cause problems when types are not yet loaded.
>>
>> 2. Java proxies cannot invoke default methods of proxied interfaces
>>
>> The Java proxy API does not currently allow the invocation of an overridden
>> default method since
>> the InvocationHandler API only supplies an instance of
>> java.lang.reflection.Method. In Java 8, it was always possible to get hold
>> of method handle of any method of the proxied interface and to specialize
>> the handle on the interface type to invoke the default implementation. With
>> the JPMS, even if an interface type is exported, this same type might not
>> be opened to another module. This implies that if an InvocationHandler is
>> defined by this module to which the interface is exported, this module's
>> InvocationHandler cannot resolve a specialized method handle to a default
>> method of the proxied interface. As a matter of fact, such a call is
>> impossible in this scenario whereas the same call is possible if the proxy
>> is implemented manually at compile time.
>>
>> As a solution, I suggest to provide an argument to the InvocationHandler
>> that represents a lookup instance with the access rights of the proxy
>> class. Using this lookup, the specialized method handles could be resolved.
>>
>> 3. Mocking and serialization libraries still require
>> Unsafe::allocateInstance.
>>
>> For Mockito, it is required to instantiate any class without invoking a
>> constructor with potential side-effects. This is of course a grose
>> violation of Java's general contract for class instantiation but this
>> feature is very useful.
>>
>> Using a Java agent, it is already possible to emulate this feature without
>> internal API by instrumenting all constructors of all classes in the
>> hierarchy of a mocked class by transforming all constructors into the
>> following pseudo-code:
>>
>> SomeConstructor(Void arg) {
>> if (MockitoBootHelper.THREAD_LOCAL.isMockInstantiatingMode()) {
>> super(null); // any constructor of the super class with default values
>> for all arguments
>> } else {
>> // original constructor code...
>> }
>> }
>>
>> Before instantiating a mock, the thread local value that is held by a
>> bootstrap-loader injected class is set to true such that a side-effect free
>> construction is achieved.
>>
>> This is of course too expensive and has negative effects on performance due
>> to additional branching and JIT-byte code limits such that one would rather
>> open jdk.internal.misc to access the Unsafe instantiation mechanism if a
>> Java agent is already available.
>>
>> However, mocking and serialization libraries are not typically loaded as a
>> Java agent. Also, I think that the actual requirements are different. My
>> suggestion here is:
>>
>> a) For serialization libraries, I think that adding
>> MethodHandles.Lookup::newInstance(Class<? extends Serializable>) with
>> standard deserialization mechanics would be sufficient.
>> b) For mocking libraries, this does not suffice as mocks can be of any
>> class. I understand that this breaks encapsulation but for unit tests, I
>> argue that the benefit of using these libraries outweights the benefit of
>> full encapsulation within a unit test.
>>
>> As Mockito is typically run within a build which uses a JDK, we could
>> attach to the current VM using the attachment API. Since Java 9, this is no
>> longer possible to attach to the same VM but instead we start a helper JVM
>> process that applies the attachment indirectly. Unfortunately, this is a
>> rather costly operation what is especially problematic when running a
>> single unit test. (The difference to this approach over Unsafe is about
>> half a second on average.)
>>
>> To overcome this, I would like to suggest to:
>>
>> a) Add a method Instrumentation::allocateInstance(Class). Java agents can
>> already emulate this privilege as described above, this is therefore merely
>> a convenience.
>> b) Add a module jdk.test to JDK 11 with a class
>> JavaTest::getInstrumentation that returns an instrumentation instance for
>> the current JVM. This module should not be resolved by default but only
>> when requiring it explicitly similar to the EE modules after Java 9.
>>
>> I think this solution carries two benefits:
>>
>> a) Test libraries like Mockito can only be used in a testing scope. We
>> experience regularly that Mockito is used in production environments. The
>> library is not meant for that and we warn and document that this is not
>> intended use but people regularly ignore this directive. By requiring this
>> module, this form of abuse would no longer be possible and the JVM could
>> even give a meaningful error message if this use was intended.
>> b) The Instrumentation instance can be used for other meaningful testing
>> approaches. For example, Mockito offers an inline mock maker where the
>> mocking logic is inlined into a method body rather then creating a
>> subclass. This approach mainly targets final classes which have become more
>> common especially since the Kotlin language became popular. To supply this
>> alternative mock maker, Mockito attempts attaching an agent to the current
>> VM (directly or indirectly, depending on the VM's version) which suffers
>> the additional costs for attaching an agent that I mentioned before.
>>
>> Thanks for reading this and I hope that you can consider these, as of
>> today, very practiced use cases. The JPMS migration has gone quite well, I
>> find. With these outstanding problems, JDK 11 could be the first release
>> where a majority of Java programs would no longer need to rely on internal
>> API.
>>
>> Best regards, Rafael
>
Alan Bateman
2018-04-09 07:33:27 UTC
Permalink
On 01/04/2018 22:02, Rafael Winterhalter wrote:
> :
>
> 1. Java agents cannot define auxiliary classes.
>
> :
> The reason for
> using Unsafe is that many instrumentations need to define auxiliary classes
> to aid an instrumentation similar to javac which sometimes needs to define
> anonymous classes or even synthetic classes. For example, if a Java agent
> wants to register an event listener to some framework, such listeners often
> declare multiple methods what makes it impossible to fullfil the listener
> contract using a lambda expression. Instead, one typically injects an
> additional class into the same package as the instrumented class.
This seems a reasonable requirement. As you know, JSR-163 created this
API (and JVM TI) for tools to instrument code in mostly benign ways
where any additional agent provided helper classes are made visibility
via the appendToXXXClassLoaderSearch methods. I don't think the use-case
of dynamically generated helper classes came up, I don't recall it
coming up on serviceability-dev in the intervening years either. In any
case, I think there should be a way to support this scenario, it amounts
to a ClassFileTransformer providing the class bytes of additional
classes to be defined in the same runtime package as the class being
loaded or transformed. There are a number of API choices and it's
probably best if we bring proposals to serviceability-dev as that is
where this API is maintained.


> :
>
> 2. Java proxies cannot invoke default methods of proxied interfaces
>
> The Java proxy API does not currently allow the invocation of an overridden
> default method since
> the InvocationHandler API only supplies an instance of
> java.lang.reflection.Method.
The issue of Proxies and default methods has come up on core-libs-dev a
few times. In the mean-time, JEP 274 added support for MethodHandles
that bind to non-abstract methods in interfaces. I just double checked
and I can create a proxy where the invocation handler creates a method
handle to the default method, binds it to proxy, and invokes it. I also
checked the case where the interface is public in an exported package of
a named module. Can you say a bit more, or provide an example, where you
run into issues? I'm wondering if you are running into an accessibility
issue or something else.

-Alan
Stephen Colebourne
2018-04-09 10:56:20 UTC
Permalink
On 9 April 2018 at 08:33, Alan Bateman <***@oracle.com> wrote:
>> 2. Java proxies cannot invoke default methods of proxied interfaces
>>
>> The Java proxy API does not currently allow the invocation of an
>> overridden
>> default method since
>> the InvocationHandler API only supplies an instance of
>> java.lang.reflection.Method.
>
> The issue of Proxies and default methods has come up on core-libs-dev a few
> times.

I hit this problem just today but am on Java 8, so Java 9 solutions
are no good. The standard Java 8 workaround requires setAccessible. I
found an alternative that is useful in some cases, but was worth
documenting (as I didn't find it described anywhere else):
https://stackoverflow.com/a/49730826/38896

I do think Proxy should be enhanced to handle default methods without
use of MethodHandle. Proxy is more of an entry level tool, whereas MH
is lower level. Requiring MH to solve default methods seems wrong for
the typical user of Proxy.

Stephen
Rafael Winterhalter
2018-04-09 19:48:42 UTC
Permalink
Hei Alan,
maybe I am doing it wrong but this is my example. I created a module with
two interfaces bar.Bar and qux.Qux that both define a default method String
foo() { return "foo"; }. The module exports bar and exports and opens qux.
From another module that reads that first module I run the following code:

package main;

import bar.Bar;
import qux.Qux;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Proxy;
import java.util.function.Consumer;

public class Main {

public static void main(String[] args) {
test(Foo.class, Foo::foo); // works: interface in same module
test(Bar.class, Bar::foo); // works not: interface in other
module, exported
test(Qux.class, Qux::foo); // works: interface in other
module, exported and opened

test(Bar.class, new Bar() { // works: explicit proxy
@Override
public String foo() {
return Bar.super.foo();
}
}, Bar::foo);

test(Bar.class, new Bar() { // works not: explicit proxy with
method handle
@Override
public String foo() {
try {
return (String) MethodHandles.lookup().findSpecial(
Bar.class,
"foo",
MethodType.methodType(String.class, new
Class<?>[0]),
Bar.class
).bindTo(this).invokeWithArguments();
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
}, Bar::foo);
}

private static <T> void test(Class<? extends T> iface, Consumer<T>
consumer) {
Object instance = Proxy.newProxyInstance(
Bar.class.getClassLoader(),
new Class<?>[]{iface},
(proxy, method, arguments) ->
MethodHandles.privateLookupIn(iface,
MethodHandles.lookup()).findSpecial(
iface,
"foo",
MethodType.methodType(String.class, new Class<?>[0]),
iface
).bindTo(proxy).invokeWithArguments()
);

test(iface, iface.cast(instance), consumer);
}

private static <T> void test(Class<? extends T> iface, T instance,
Consumer<T> consumer) {
try {
consumer.accept(instance);
System.out.println("Could invoke special method for " + iface);
} catch (Throwable throwable) {
System.out.println("Could NOT invoke special method for " + iface);
}
}
}

From the code comments, some approaches work and some do not.
What would I need to do differently?

Thank you and best regards, Rafael

2018-04-09 9:33 GMT+02:00 Alan Bateman <***@oracle.com>:

> On 01/04/2018 22:02, Rafael Winterhalter wrote:
>
>> :
>>
>> 1. Java agents cannot define auxiliary classes.
>>
>> :
>> The reason for
>> using Unsafe is that many instrumentations need to define auxiliary
>> classes
>> to aid an instrumentation similar to javac which sometimes needs to define
>> anonymous classes or even synthetic classes. For example, if a Java agent
>> wants to register an event listener to some framework, such listeners
>> often
>> declare multiple methods what makes it impossible to fullfil the listener
>> contract using a lambda expression. Instead, one typically injects an
>> additional class into the same package as the instrumented class.
>>
> This seems a reasonable requirement. As you know, JSR-163 created this API
> (and JVM TI) for tools to instrument code in mostly benign ways where any
> additional agent provided helper classes are made visibility via the
> appendToXXXClassLoaderSearch methods. I don't think the use-case of
> dynamically generated helper classes came up, I don't recall it coming up
> on serviceability-dev in the intervening years either. In any case, I think
> there should be a way to support this scenario, it amounts to a
> ClassFileTransformer providing the class bytes of additional classes to be
> defined in the same runtime package as the class being loaded or
> transformed. There are a number of API choices and it's probably best if we
> bring proposals to serviceability-dev as that is where this API is
> maintained.
>
>
> :
>>
>> 2. Java proxies cannot invoke default methods of proxied interfaces
>>
>> The Java proxy API does not currently allow the invocation of an
>> overridden
>> default method since
>> the InvocationHandler API only supplies an instance of
>> java.lang.reflection.Method.
>>
> The issue of Proxies and default methods has come up on core-libs-dev a
> few times. In the mean-time, JEP 274 added support for MethodHandles that
> bind to non-abstract methods in interfaces. I just double checked and I can
> create a proxy where the invocation handler creates a method handle to the
> default method, binds it to proxy, and invokes it. I also checked the case
> where the interface is public in an exported package of a named module. Can
> you say a bit more, or provide an example, where you run into issues? I'm
> wondering if you are running into an accessibility issue or something else.
>
> -Alan
>
>
Alan Bateman
2018-04-10 09:54:52 UTC
Permalink
On 09/04/2018 20:48, Rafael Winterhalter wrote:
> Hei Alan,
> maybe I am doing it wrong but this is my example. I created a module
> with two interfaces bar.Bar and qux.Qux that both define a default
> method String foo() { return "foo"; }. The module exports bar and
> exports and opens qux.
> From another module that reads that first module I run the following code:
Thanks for the test.

I think the issue is that findSpecial can never work when invoked on a
Lookup to a lookup class in a named module and the proposed caller class
for invokespecial (specialCaller) is in a different module. It's not an
issue when the lookup class in an unnamed module, or when the lookup
class in a named module and specialCaller is in the same module.

I'm sure John will jump in but I think if JDK-8173978 is implemented
then it will go a long way to address this anomaly.

-Alan

[1] https://bugs.openjdk.java.net/browse/JDK-8173978
Rafael Winterhalter
2018-04-10 11:15:59 UTC
Permalink
Thanks for the reference.

As for the proxy API, I think that this should be reworked anyways as the
lookup on each call would be rather expensive.

I also see the other issues that I mentioned as more pressing.

Thank you and best regards, Rafael

2018-04-10 11:54 GMT+02:00 Alan Bateman <***@oracle.com>:

> On 09/04/2018 20:48, Rafael Winterhalter wrote:
>
>> Hei Alan,
>> maybe I am doing it wrong but this is my example. I created a module with
>> two interfaces bar.Bar and qux.Qux that both define a default method String
>> foo() { return "foo"; }. The module exports bar and exports and opens qux.
>> From another module that reads that first module I run the following code:
>>
> Thanks for the test.
>
> I think the issue is that findSpecial can never work when invoked on a
> Lookup to a lookup class in a named module and the proposed caller class
> for invokespecial (specialCaller) is in a different module. It's not an
> issue when the lookup class in an unnamed module, or when the lookup class
> in a named module and specialCaller is in the same module.
>
> I'm sure John will jump in but I think if JDK-8173978 is implemented then
> it will go a long way to address this anomaly.
>
> -Alan
>
> [1] https://bugs.openjdk.java.net/browse/JDK-8173978
>
Alan Bateman
2018-04-11 07:26:44 UTC
Permalink
On 10/04/2018 12:15, Rafael Winterhalter wrote:
> Thanks for the reference.
>
> As for the proxy API, I think that this should be reworked anyways as
> the lookup on each call would be rather expensive.
In the mean-time, I think you should be able to make progress with
findSpecial for the common case where the interface is public and in an
exported package.

For this case, the proxy will be generated into an unnamed module (the
"Package and Module Membership of Proxy Class" section of the Proxy API
spec has all the wonderful details on this). The proxy dispatches to
your invocation handler, module "main" in your test. The invocation
handler can invoke Module::addReads to update module "main" to read the
proxy's module. It can then create a full power Lookup on the proxy
class and invoke findSpecial with the proxy class ("iface" should work
too) as the specialCaller. Bind that to the proxy instance and perform
the invoke as you are doing already.

-Alan.
Rafael Winterhalter
2018-04-11 20:07:16 UTC
Permalink
I do not think that this is possible. If the module containing the
interface does not open a package, I cannot change the privileges of the
main module such that I can resolve a method handle for invoking the
special invocation.

I just tried this out too and I did not find a way, could you suggest how
to change my code for being able to do so?

Best regards, Rafael

2018-04-11 9:26 GMT+02:00 Alan Bateman <***@oracle.com>:

> On 10/04/2018 12:15, Rafael Winterhalter wrote:
>
>> Thanks for the reference.
>>
>> As for the proxy API, I think that this should be reworked anyways as the
>> lookup on each call would be rather expensive.
>>
> In the mean-time, I think you should be able to make progress with
> findSpecial for the common case where the interface is public and in an
> exported package.
>
> For this case, the proxy will be generated into an unnamed module (the
> "Package and Module Membership of Proxy Class" section of the Proxy API
> spec has all the wonderful details on this). The proxy dispatches to your
> invocation handler, module "main" in your test. The invocation handler can
> invoke Module::addReads to update module "main" to read the proxy's module.
> It can then create a full power Lookup on the proxy class and invoke
> findSpecial with the proxy class ("iface" should work too) as the
> specialCaller. Bind that to the proxy instance and perform the invoke as
> you are doing already.
>
> -Alan.
>
>
>
Alan Bateman
2018-04-12 08:40:28 UTC
Permalink
On 11/04/2018 21:07, Rafael Winterhalter wrote:
> I do not think that this is possible. If the module containing the
> interface does not open a package, I cannot change the privileges of
> the main module such that I can resolve a method handle for invoking
> the special invocation.
>
> I just tried this out too and I did not find a way, could you suggest
> how to change my code for being able to do so?
If the interface is public in an exported package (no need for the
package to be open) then the proxy will be generated into the unnamed
module. So easy to get a Lookup to the proxy class and you can use this
as the special caller. Can you change your invocation handler to the
following and try it:

Class<?> proxyClass = proxy.getClass();
Main.class.getModule().addReads(proxyClass.getModule());
Lookup lookup = MethodHandles.privateLookupIn(proxyClass,
MethodHandles.lookup());
MethodType mt = MethodType.methodType(String.class);
return lookup.findSpecial(iface, "foo", mt,
proxyClass).bindTo(proxy).invokeWithArguments();

-Alan
Rafael Winterhalter
2018-04-12 16:06:13 UTC
Permalink
I have not thought of that but you are of course right, that works. The
solution is however far from pretty as it needs a case basis since the
resolution of the proxy class is only possible if the proxy is in fact
loaded into an unnamed module what is not always the case. This does
however cover all cases for Java 9 and Java 10 proxies. I do however hope
Java 11 can ship with something more convenient then this:
https://gist.github.com/raphw/c1faf2f40e80afce6f13511098cfb90f

Thanks for guiding me through that!
Best regards, Rafael

2018-04-12 10:40 GMT+02:00 Alan Bateman <***@oracle.com>:

> On 11/04/2018 21:07, Rafael Winterhalter wrote:
>
>> I do not think that this is possible. If the module containing the
>> interface does not open a package, I cannot change the privileges of the
>> main module such that I can resolve a method handle for invoking the
>> special invocation.
>>
>> I just tried this out too and I did not find a way, could you suggest how
>> to change my code for being able to do so?
>>
> If the interface is public in an exported package (no need for the package
> to be open) then the proxy will be generated into the unnamed module. So
> easy to get a Lookup to the proxy class and you can use this as the special
> caller. Can you change your invocation handler to the following and try it:
>
> Class<?> proxyClass = proxy.getClass();
> Main.class.getModule().addReads(proxyClass.getModule());
> Lookup lookup = MethodHandles.privateLookupIn(proxyClass,
> MethodHandles.lookup());
> MethodType mt = MethodType.methodType(String.class);
> return lookup.findSpecial(iface, "foo", mt, proxyClass).bindTo(proxy).invo
> keWithArguments();
>
> -Alan
>
Peter Levart
2018-04-13 06:48:10 UTC
Permalink
On 04/12/18 10:40, Alan Bateman wrote:
> On 11/04/2018 21:07, Rafael Winterhalter wrote:
>> I do not think that this is possible. If the module containing the
>> interface does not open a package, I cannot change the privileges of
>> the main module such that I can resolve a method handle for invoking
>> the special invocation.
>>
>> I just tried this out too and I did not find a way, could you suggest
>> how to change my code for being able to do so?
> If the interface is public in an exported package (no need for the
> package to be open) then the proxy will be generated into the unnamed
> module. So easy to get a Lookup to the proxy class and you can use
> this as the special caller. Can you change your invocation handler to
> the following and try it:
>
> Class<?> proxyClass = proxy.getClass();
> Main.class.getModule().addReads(proxyClass.getModule());
> Lookup lookup = MethodHandles.privateLookupIn(proxyClass,
> MethodHandles.lookup());
> MethodType mt = MethodType.methodType(String.class);
> return lookup.findSpecial(iface, "foo", mt,
> proxyClass).bindTo(proxy).invokeWithArguments();
>
> -Alan

This works logically, but performance wise I would 1st create a proxy
class, lookup the direct method handles I want to invoke, transform them
to take Object 'proxy' instance as 1st argument and the rest of
arguments as an Object[] and return an Object. I would cache the
resulting MHs in the specific (constant if possible) InvocationHandler.
Each invocation would then only select the right cached MH as quickly as
possible and do return mh.invokeExact(proxy, args);

Peter
Rafael Winterhalter
2018-04-13 19:49:54 UTC
Permalink
Yes, of course. This was merely a proof of concept to show that it works.

2018-04-13 8:48 GMT+02:00 Peter Levart <***@gmail.com>:

>
>
> On 04/12/18 10:40, Alan Bateman wrote:
>
> On 11/04/2018 21:07, Rafael Winterhalter wrote:
>
> I do not think that this is possible. If the module containing the
> interface does not open a package, I cannot change the privileges of the
> main module such that I can resolve a method handle for invoking the
> special invocation.
>
> I just tried this out too and I did not find a way, could you suggest how
> to change my code for being able to do so?
>
> If the interface is public in an exported package (no need for the package
> to be open) then the proxy will be generated into the unnamed module. So
> easy to get a Lookup to the proxy class and you can use this as the special
> caller. Can you change your invocation handler to the following and try it:
>
> Class<?> proxyClass = proxy.getClass();
> Main.class.getModule().addReads(proxyClass.getModule());
> Lookup lookup = MethodHandles.privateLookupIn(proxyClass,
> MethodHandles.lookup());
> MethodType mt = MethodType.methodType(String.class);
> return lookup.findSpecial(iface, "foo", mt, proxyClass).bindTo(proxy).invokeWithArguments();
>
>
> -Alan
>
>
> This works logically, but performance wise I would 1st create a proxy
> class, lookup the direct method handles I want to invoke, transform them to
> take Object 'proxy' instance as 1st argument and the rest of arguments as
> an Object[] and return an Object. I would cache the resulting MHs in the
> specific (constant if possible) InvocationHandler. Each invocation would
> then only select the right cached MH as quickly as possible and do return
> mh.invokeExact(proxy, args);
>
> Peter
>
>
mandy chung
2018-04-15 06:33:13 UTC
Permalink
Hi Rafael,

FYI.  I have sent a proposal [1] for item #1 to serviceability-dev and
you for discussion.  JBS issue is:
   https://bugs.openjdk.java.net/browse/JDK-8200559

Mandy
[1]
http://mail.openjdk.java.net/pipermail/serviceability-dev/2018-April/023529.html

On 4/2/18 5:02 AM, Rafael Winterhalter wrote:
> 1. Java agents cannot define auxiliary classes.
>
> Byte Buddy does support the JPMS fully, however, it still relies on
> sun.misc.Unsafe::defineClass for its Java agent API and currently breaks on
> Java 11 as this method was removed in a recent EA build. The reason for
> using Unsafe is that many instrumentations need to define auxiliary classes
> to aid an instrumentation similar to javac which sometimes needs to define
> anonymous classes or even synthetic classes. For example, if a Java agent
> wants to register an event listener to some framework, such listeners often
> declare multiple methods what makes it impossible to fullfil the listener
> contract using a lambda expression. Instead, one typically injects an
> additional class into the same package as the instrumented class. In this
> case, it is not possible to use MethodHandles.Lookup::defineClass as the
> class file transformer does not necessarily have private access to the
> lookup of the instrumented class.
>
> The current workarounds are:
>
> a) Open the package jdk.internal.misc to gain access to this package's
> Unsafe class. This can be done via Instrumentation::redefineModule.
> b) Open the java.lang package to access ClassLoader via reflection.
> c) Open the java.lang package to access the internal lookup with global
> access rights.
>
> Of these solutions only (b) relies on standard API and is guaranteed to
> function in the future but the solution still feels hacky and does not work
> for instrumentations of classes on the bootstrap loader. Opening packages
> also implies a risk of being applied carelessly since opening the package
> to the agent's module most likely opens the package to the unnamed module
> of the system class loader what invites to breaches of the JPMS
> encapsulation by code that does not ship with the agent.
> To offer a better solution, I would like to suggest one of the following:
>
> a) Add a method defineClass(ClassLoader, byte[], ProtectionDomain) to the
> Instrumentation interface that works similar to Unsafe::defineClass. This
> would provide a very simple migration path. Since agents have access to
> jdk.internal.misc, adding this method does not add any capabilities to the
> agent, it merley avoids using internal API that might change.
> b) Supply a MethodHandles.Lookup instance to the
> ClassFileTransformer::transform API where the instance represents the
> instrumented class's access rights. This does however carry the risk of
> invoking the lookupClass method which would either load the instrumented
> class prematurely causing a circularity error or return an unexpected value
> such as null. Since the lookup API generally relies on loaded types, there
> are probably other problems such as invoking Lookup::privateLookupIn before
> all involved types are loaded.
>
> For the sake of simlicity and since easy migration paths make a quick
> adoption easier, I would suggestion solution (a), also in the light that
> quick and dirty migrations might still choose option (b) to save time and
> also since (b) might cause problems when types are not yet loaded.
>
Rafael Winterhalter
2018-04-15 20:43:15 UTC
Permalink
Thanks Mandy, I appreciate it!
Best regards, Rafael

2018-04-15 8:33 GMT+02:00 mandy chung <***@oracle.com>:

> Hi Rafael,
>
> FYI. I have sent a proposal [1] for item #1 to serviceability-dev and you
> for discussion. JBS issue is:
> https://bugs.openjdk.java.net/browse/JDK-8200559
>
> Mandy
> [1] http://mail.openjdk.java.net/pipermail/serviceability-dev/
> 2018-April/023529.html
>
>
> On 4/2/18 5:02 AM, Rafael Winterhalter wrote:
>
> 1. Java agents cannot define auxiliary classes.
>
> Byte Buddy does support the JPMS fully, however, it still relies on
> sun.misc.Unsafe::defineClass for its Java agent API and currently breaks on
> Java 11 as this method was removed in a recent EA build. The reason for
> using Unsafe is that many instrumentations need to define auxiliary classes
> to aid an instrumentation similar to javac which sometimes needs to define
> anonymous classes or even synthetic classes. For example, if a Java agent
> wants to register an event listener to some framework, such listeners often
> declare multiple methods what makes it impossible to fullfil the listener
> contract using a lambda expression. Instead, one typically injects an
> additional class into the same package as the instrumented class. In this
> case, it is not possible to use MethodHandles.Lookup::defineClass as the
> class file transformer does not necessarily have private access to the
> lookup of the instrumented class.
>
> The current workarounds are:
>
> a) Open the package jdk.internal.misc to gain access to this package's
> Unsafe class. This can be done via Instrumentation::redefineModule.
> b) Open the java.lang package to access ClassLoader via reflection.
> c) Open the java.lang package to access the internal lookup with global
> access rights.
>
> Of these solutions only (b) relies on standard API and is guaranteed to
> function in the future but the solution still feels hacky and does not work
> for instrumentations of classes on the bootstrap loader. Opening packages
> also implies a risk of being applied carelessly since opening the package
> to the agent's module most likely opens the package to the unnamed module
> of the system class loader what invites to breaches of the JPMS
> encapsulation by code that does not ship with the agent.
>
> To offer a better solution, I would like to suggest one of the following:
>
> a) Add a method defineClass(ClassLoader, byte[], ProtectionDomain) to the
> Instrumentation interface that works similar to Unsafe::defineClass. This
> would provide a very simple migration path. Since agents have access to
> jdk.internal.misc, adding this method does not add any capabilities to the
> agent, it merley avoids using internal API that might change.
> b) Supply a MethodHandles.Lookup instance to the
> ClassFileTransformer::transform API where the instance represents the
> instrumented class's access rights. This does however carry the risk of
> invoking the lookupClass method which would either load the instrumented
> class prematurely causing a circularity error or return an unexpected value
> such as null. Since the lookup API generally relies on loaded types, there
> are probably other problems such as invoking Lookup::privateLookupIn before
> all involved types are loaded.
>
> For the sake of simlicity and since easy migration paths make a quick
> adoption easier, I would suggestion solution (a), also in the light that
> quick and dirty migrations might still choose option (b) to save time and
> also since (b) might cause problems when types are not yet loaded.
>
>
>
Andrew Dinn
2018-04-16 09:29:37 UTC
Permalink
On 15/04/18 07:33, mandy chung wrote:
> FYI.  I have sent a proposal [1] for item #1 to serviceability-dev and
> you for discussion.  JBS issue is:
>    https://bugs.openjdk.java.net/browse/JDK-8200559
>
> Mandy
> [1]
> http://mail.openjdk.java.net/pipermail/serviceability-dev/2018-April/023529.html
Thanks, Mandy. The proposed API looks great. Good luck with those
corner-cases in the implementation :-)

regards,


Andrew Dinn
-----------
Loading...