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

8327624: Remove VM implementation that bypass verification for core reflection #21571

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mlchung
Copy link
Member

@mlchung mlchung commented Oct 17, 2024

The old core reflection implementation generates dynamic classes that are special cases in the VM to bypass bytecode verification to workaround various issues [1] [2] [3].

The old core reflection implementation was removed in JDK 22. It's time to remove these VM hacks along with the old implementation of sun.reflect.ReflectionFactory::newConstructorForSerialization.

After this change, jdk.internal.reflect.DelegatingClassLoader no longer exists. Hence the special metaspace for reflection is no longer needed. GTests will need to be updated when Metaspace::ReflectionMetaspaceType is removed. Such clean up can be done separately (JDK-8342561).

[1] JDK-4486457
[2] JDK-4474172
[3] JDK-6790209


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change requires CSR request JDK-8342574 to be approved
  • Change must be properly reviewed (2 reviews required, with at least 2 Reviewers)

Issues

  • JDK-8327624: Remove VM implementation that bypass verification for core reflection (Enhancement - P4)
  • JDK-8342574: Remove VM implementation that bypass verification for core reflection (CSR)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/21571/head:pull/21571
$ git checkout pull/21571

Update a local copy of the PR:
$ git checkout pull/21571
$ git pull https://git.openjdk.org/jdk.git pull/21571/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 21571

View PR using the GUI difftool:
$ git pr show -t 21571

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/21571.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 17, 2024

👋 Welcome back mchung! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Oct 17, 2024

@mlchung This change is no longer ready for integration - check the PR body for details.

@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 17, 2024
@openjdk
Copy link

openjdk bot commented Oct 17, 2024

@mlchung The following labels will be automatically applied to this pull request:

  • core-libs
  • hotspot

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added hotspot hotspot-dev@openjdk.org core-libs core-libs-dev@openjdk.org labels Oct 17, 2024
@mlbridge
Copy link

mlbridge bot commented Oct 17, 2024

Webrevs

@liach
Copy link
Member

liach commented Oct 17, 2024

It seems we can now remove AccessorGenerator, ByteVector*, ClassFile*, Label, and MagicAccessorImpl Java files from jdk.internal.reflect. Do we plan to do that in another RFE?

@mlchung
Copy link
Member Author

mlchung commented Oct 17, 2024

Good catch! Patch updated.

Copy link
Member

@liach liach left a comment

Choose a reason for hiding this comment

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

/reviewers 2 reviewer

The jdk.internal.reflect changes look good; need other reviewers to review hotspot changes.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 17, 2024
@openjdk
Copy link

openjdk bot commented Oct 17, 2024

@liach
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 2 Reviewers).

@openjdk openjdk bot added csr Pull request needs approved CSR before integration and removed ready Pull request is ready to be integrated labels Oct 17, 2024
Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

Nice cleanup! Great to see all this old code go. Just one mistake ...

Thanks

Comment on lines -4079 to -4098
// If the loader is not the boot loader then throw an exception if its
// superclass is in package jdk.internal.reflect and its loader is not a
// special reflection class loader
if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) {
PackageEntry* super_package = super->package();
if (super_package != nullptr &&
super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 &&
!java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) {
ResourceMark rm(THREAD);
Exceptions::fthrow(
THREAD_AND_LOCATION,
vmSymbols::java_lang_IllegalAccessError(),
"class %s loaded by %s cannot access jdk/internal/reflect superclass %s",
this_klass->external_name(),
this_klass->class_loader_data()->loader_name_and_id(),
super->external_name());
return;
}
}

Copy link
Member

Choose a reason for hiding this comment

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

This code should not be removed. The spec for this code should now be:

  // If the loader is not the boot loader then throw an exception if its
  // superclass is in package jdk.internal.reflect 

All we need do is remove the check:

&& !java_lang_ClassLoader::is_reflection_class_loader

Copy link
Contributor

@coleenp coleenp Oct 18, 2024

Choose a reason for hiding this comment

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

Oh that's a good find. Maybe we should write a test for this, if as I assume there isn't one already.
Edit: not with this RFE, just in general.

Copy link
Member Author

@mlchung mlchung Oct 18, 2024

Choose a reason for hiding this comment

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

// If the loader is not the boot loader then throw an exception if its
// superclass is in package jdk.internal.reflect

This should not be needed. jdk.internal.reflect is a non-exported package in java.base module. If another module M defines a class whose superclass is in jdk.internal.reflect package, java.base must export jdk.internal.reflect package to M for access. Otherwise, it will fail the super access check, as done in the check below this deleted code.

Reflection::VerifyClassAccessResults vca_result =
     Reflection::verify_class_access(this_klass, InstanceKlass::cast(super), false);

Copy link
Member Author

Choose a reason for hiding this comment

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

test/hotspot/jtreg/runtime/AccessCheckSuper.java is one test that fails the super class access check. @lfoltan may know whether there are more tests besides this one.

Choose a reason for hiding this comment

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

This code was necessary back when this was the sun.reflect package and it was not able to be encapsulated (before modules) to prevent untrusted class loaders from leaking the MagicAccessorImpl class hierarchy.

Copy link
Member

Choose a reason for hiding this comment

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

Okay ... so such subclass was and remains illegal and is now detected slightly later. That suggests to me this special case was in the wrong place and should have been inside Reflection::verify_class_access.

Anyway thanks for clarifying full removal is in fact what we want.

Copy link
Contributor

@rose00 rose00 left a comment

Choose a reason for hiding this comment

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

This is a most impressive example of C.D.E. (code deletion engineering).

I remember when this was written, as an accelerator for the JNI methods, long long before we had method handles to do the same thing more flexibly. I think it was Ken Russell that did the work. And of course I remember Mandy's newer work (3 years ago) fitting method handles into reflection (a move I applauded of course).

As it happens I was just reviewing the reflection implementation this week, to understand its interaction with upcoming Leyden changes to bootstrap sequences. Of course I wondered, "when will we ever retire this older implementation?" Happily, that day has come.

Thanks Mandy!

Copy link
Contributor

@AlanBateman AlanBateman left a comment

Choose a reason for hiding this comment

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

Good cleanup.

I searched the tests as I expected to find some tests exercising -Djdk.reflect.useOldSerializableConstructor but nothing found.

@mlchung
Copy link
Member Author

mlchung commented Oct 18, 2024

Happily, that day has come.

Yes, feeling happy! I have been waiting to get rid of this last piece!

Copy link

@ExE-Boss ExE-Boss left a comment

Choose a reason for hiding this comment

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

Does this mean that JDK‑4486457 will finally be able to be made public?

Comment on lines -4079 to -4098
// If the loader is not the boot loader then throw an exception if its
// superclass is in package jdk.internal.reflect and its loader is not a
// special reflection class loader
if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) {
PackageEntry* super_package = super->package();
if (super_package != nullptr &&
super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 &&
!java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) {
ResourceMark rm(THREAD);
Exceptions::fthrow(
THREAD_AND_LOCATION,
vmSymbols::java_lang_IllegalAccessError(),
"class %s loaded by %s cannot access jdk/internal/reflect superclass %s",
this_klass->external_name(),
this_klass->class_loader_data()->loader_name_and_id(),
super->external_name());
return;
}
}

Choose a reason for hiding this comment

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

This code was necessary back when this was the sun.reflect package and it was not able to be encapsulated (before modules) to prevent untrusted class loaders from leaking the MagicAccessorImpl class hierarchy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org csr Pull request needs approved CSR before integration hotspot hotspot-dev@openjdk.org rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

7 participants