Skip to content

Agent opens access to java.base to unnamed module #1328

@DanielThomas

Description

@DanielThomas

We're migrating to JDK 17 and found that we were unable to run tests with JaCoCo to verify code was not accessing encapsulated Java internals in the java.base package.

Steps to reproduce

  • JaCoCo version: 0.8.8
  • Operating system: macOS 10.4
  • Tool integration: Command-line Agent

Given class IllegalAccess.java and JDK 17:

import java.lang.Throwable;

public class IllegalAccess {
	public static void main(String[] args) throws Exception {
		Throwable.class.getDeclaredMethod("setCause", Throwable.class).setAccessible(true);
	}
}

Run without Jacoco and note the exception:

$ java IllegalAccess
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make final void java.lang.Throwable.setCause(java.lang.Throwable) accessible: module java.base does not "opens java.lang" to unnamed module @a803f94
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
	at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
	at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
	at IllegalAccess.main(IllegalAccess.java:5)

Run with Jacoco and no exception is thrown:

$ java -javaagent:/Users/dannyt/Downloads/jacoco-0.8.8/lib/jacocoagent.jar IllegalAccess

Expected behaviour

The agent should not cause runtime side effects to code under test. This in particular makes it impossible to verify production code doesn't perform illegal accesses at test time causing this code to fail when deployed.

Actual behaviour

The agent adds java.base to the exported modules for the unnamed module causing the entire classpath to be granted access.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions