Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to detect system property mangling prior to loading ByteBuddy. #3164

Merged

Conversation

ascopes
Copy link
Contributor

@ascopes ascopes commented Oct 29, 2023

If the user stubs their System properties (e.g. to test code that handles System properties to determine the platform), then they may unexpectedly hit the issue that ByteBuddy will attempt to fork a subprocess upon being installed, and that relies on the JVM Process class. Internally, the Process class makes a call to 'System.getProperty(os.name)', which results in a NullPointerException being raised if the property does not exist.

When this exception occurs, it bubbles up to Mockito in the shape of a very confusing error message, which we should ideally try to avoid.

This change adds a check for this edge case prior to loading ByteBuddy so that a meaningful error message can be raised that instructs the user on how to mitigate this issue.


Example reproduction:

static class SomeClass {}

@Test
void something() {
  System.getProperties().clear();

  try (var mock = Mockito.mockStatic(SomeClass.class)) {
    // Do nothing
  }
}

Current behaviour:


java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)

	at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:84)
	at com.sun.proxy.$Proxy19.createStaticMock(Unknown Source)
	at org.mockito.internal.util.MockUtil.createStaticMock(MockUtil.java:202)
	at org.mockito.internal.MockitoCore.mockStatic(MockitoCore.java:134)
	at org.mockito.Mockito.mockStatic(Mockito.java:2325)
	at org.mockito.Mockito.mockStatic(Mockito.java:2283)
	at org.example.SomeTest.something(SomeTest.java:280)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
Caused by: java.lang.IllegalStateException: Internal problem occurred, please report it. Mockito is unable to load the default implementation of class that is a part of Mockito distribution. Failed to load interface org.mockito.plugins.MockMaker
	at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.create(DefaultMockitoPlugins.java:105)
	at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.getDefaultPlugin(DefaultMockitoPlugins.java:79)
	at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:75)
	at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:50)
	at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:27)
	at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:22)
	at org.mockito.internal.MockitoCore.<clinit>(MockitoCore.java:73)
	at org.mockito.Mockito.<clinit>(Mockito.java:1683)
	... 5 more
Caused by: java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.create(DefaultMockitoPlugins.java:103)
	... 12 more
Caused by: org.mockito.exceptions.base.MockitoInitializationException: 
Could not initialize inline Byte Buddy mock maker.

It appears as if your JDK does not supply a working agent attachment mechanism.
Java               : null
JVM vendor name    : null
JVM vendor version : null
JVM name           : null
JVM version        : null
JVM info           : null
OS name            : null
OS version         : null

	at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.<init>(InlineDelegateByteBuddyMockMaker.java:244)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:23)
	... 17 more
Caused by: java.lang.ExceptionInInitializerError
	at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
	at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
	at net.bytebuddy.agent.ByteBuddyAgent.installExternal(ByteBuddyAgent.java:705)
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:636)
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:616)
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:568)
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:545)
	at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.<clinit>(InlineDelegateByteBuddyMockMaker.java:115)
	... 18 more
Caused by: java.lang.NullPointerException
	at java.base/java.lang.ProcessImpl$Platform.get(ProcessImpl.java:161)
	at java.base/java.lang.ProcessImpl.<clinit>(ProcessImpl.java:170)
	... 26 more

Using FernFlower, the code at java.base/java.lang.ProcessImpl.<clinit>(ProcessImpl.java:170) is the following:

    static Platform get() {
        String osName = GetPropertyAction.privilegedGetProperty("os.name");

        if (osName.equals("Linux")) { return LINUX; }
        if (osName.contains("OS X")) { return BSD; }
        if (osName.equals("SunOS")) { return SOLARIS; }
        if (osName.equals("AIX")) { return AIX; }

        throw new Error(osName + " is not a supported OS platform.");
    }

This is probably technically a JRE bug, as it is making the expectation that a mutable global singleton is not being mutated in a specific way, but it probably makes sense to check for it here for now to prevent confusing error messages on Mockito's side of things and to prevent spurious issues being raised against the project.

If the user stubs their System properties (e.g. to test code that handles System properties
to determine the platform), then they may unexpectedly hit the issue that ByteBuddy will
attempt to fork a subprocess upon being installed, and that relies on the JVM Process class.
Internally, the Process class makes a call to 'System.getProperty(os.name)', which results
in a NullPointerException being raised if the property does not exist.

When this exception occurs, it bubbles up to Mockito in the shape of a very confusing
error message, which we should ideally try to avoid.

This change adds a check for this edge case prior to loading ByteBuddy so that a meaningful
error message can be raised that instructs the user on how to mitigate this issue.
@codecov-commenter
Copy link

codecov-commenter commented Oct 29, 2023

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (a8adbf5) 85.53% compared to head (04b4202) 85.50%.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3164      +/-   ##
============================================
- Coverage     85.53%   85.50%   -0.03%     
  Complexity     2914     2914              
============================================
  Files           334      334              
  Lines          8861     8864       +3     
  Branches       1096     1097       +1     
============================================
  Hits           7579     7579              
- Misses          993      995       +2     
- Partials        289      290       +1     
Files Coverage Δ
...on/bytebuddy/InlineDelegateByteBuddyMockMaker.java 76.79% <0.00%> (-0.65%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

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

Thanks!

@TimvdLippe TimvdLippe merged commit f522f49 into mockito:main Oct 29, 2023
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants