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

8315678: Classfile API ConstantPool::entryCount and ConstantPool::entryByIndex methods are confusing #15567

Closed
wants to merge 4 commits into from

Conversation

asotona
Copy link
Member

@asotona asotona commented Sep 5, 2023

Classfile API ConstantPool::entryCount and ConstantPool::entryByIndex methods are confusing and unsafe to use without knowledge of constant pool specification.
This patch renames ConstantPool::entryCount to ConstantPool::size and adds checks to ConstantPool::entryByIndex for invalid or unusable indexes.
ConstantPool newly extends Iterable<PoolEntry> for simplified user iteration over all pool entries.
Range checks for invalid index have been added also to ConstantPoo::bootstrapMethodEntry methods and several @jvms javadoc tags have been added to pool entries.

Please review.

Thanks,
Adam


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8315678: Classfile API ConstantPool::entryCount and ConstantPool::entryByIndex methods are confusing (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 15567

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 5, 2023

👋 Welcome back asotona! 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 openjdk bot added the rfr Pull request is ready for review label Sep 5, 2023
@openjdk
Copy link

openjdk bot commented Sep 5, 2023

@asotona The following label will be automatically applied to this pull request:

  • core-libs

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

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Sep 5, 2023
@mlbridge
Copy link

mlbridge bot commented Sep 5, 2023

Webrevs

Comment on lines +59 to +76
default Iterator<PoolEntry> iterator() {
return new Iterator<>() {
int index = 1;

@Override
public boolean hasNext() {
return index < size();
}

@Override
public PoolEntry next() {
if (!hasNext()) throw new NoSuchElementException();
var e = entryByIndex(index);
index += e.width();
return e;
}
};
}
Copy link

Choose a reason for hiding this comment

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

This iterator isn’t entirely correct, because if the constant pool is modified between the call to hasNext() and next(), then it may throw NoSuchElementException.

Additionally, hasNext() can go from returning false to returning true.

Copy link
Member Author

Choose a reason for hiding this comment

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

The size of the pool can never go down so the iterator should behave correctly even when underlying pool is modified.

Primary purpose of the iterator is to replace not very intuitive (and frequently coded wrong - even in our own tests) for loop iteration:

for(int index = 1; index < pool.size(); index += pool.entryByIndex(index).width()) 

*/
PoolEntry entryByIndex(int index);

/**
* {@return the number of entries in the constant pool}
* {@return the size of the constant pool}
Copy link
Contributor

Choose a reason for hiding this comment

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

There is still some confusion over the meaning of this method, as "size" (as well as "entry count") could refer to either (a) the number of slots in the constant pool or (b) the number of actual entries in the constant pool, since Constant_{Long,Double} can use two slots. I agree with the name "size" but we should further clarify that this is the number of slots, but that (a) not all slots necessarily correspond to a valid entry (and therefore entryByIndex may fail) and (b) that iterating the pool may yield fewer entries than the size.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll mention it in the javadoc

@openjdk
Copy link

openjdk bot commented Sep 14, 2023

@asotona This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8315678: Classfile API ConstantPool::entryCount and ConstantPool::entryByIndex methods are confusing

Reviewed-by: briangoetz

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 87 new commits pushed to the master branch:

  • 5cea53d: 8315810: Reimplement sun.reflect.ReflectionFactory::newConstructorForSerialization with method handles
  • eb1f67b: 8315937: Enable parallelism in vmTestbase/nsk/stress/numeric tests
  • a57b9da: 8316242: Opensource SwingGraphics manual test
  • 3abe798: 8316115: Parallel: Fix -Wconversion warnings around NUMA node ID
  • 83dca62: 8313638: Add test for dump of resolved references
  • cfa8901: 8315338: RISC-V: Change flags for stable extensions to non-experimental
  • edd454b: 8315766: Parallelize gc/stress/TestStressIHOPMultiThread.java test
  • de9b971: 8315933: Serial: Remove empty Generation::ensure_parsability
  • b48dbf6: 8316181: Move the fast locking implementation out of the .ad files
  • 8f4dfc4: 8316164: Opensource JMenuBar manual test
  • ... and 77 more: https://git.openjdk.org/jdk/compare/3c258ac907287850d690375bae07c0575d1f1463...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Sep 14, 2023
@asotona
Copy link
Member Author

asotona commented Sep 14, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Sep 14, 2023

Going to push as commit ca747f0.
Since your change was applied there have been 90 commits pushed to the master branch:

  • 6d47fc6: 8313258: RuntimeInvisibleTypeAnnotationsAttribute.annotations() API Index out of Bound error
  • c7d306c: 8315541: Classfile API TypeAnnotation.TargetInfo factory methods accept null labels
  • c11f835: 8315824: Open source several Swing Text/HTML related tests
  • 5cea53d: 8315810: Reimplement sun.reflect.ReflectionFactory::newConstructorForSerialization with method handles
  • eb1f67b: 8315937: Enable parallelism in vmTestbase/nsk/stress/numeric tests
  • a57b9da: 8316242: Opensource SwingGraphics manual test
  • 3abe798: 8316115: Parallel: Fix -Wconversion warnings around NUMA node ID
  • 83dca62: 8313638: Add test for dump of resolved references
  • cfa8901: 8315338: RISC-V: Change flags for stable extensions to non-experimental
  • edd454b: 8315766: Parallelize gc/stress/TestStressIHOPMultiThread.java test
  • ... and 80 more: https://git.openjdk.org/jdk/compare/3c258ac907287850d690375bae07c0575d1f1463...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Sep 14, 2023
@openjdk openjdk bot closed this Sep 14, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Sep 14, 2023
@openjdk
Copy link

openjdk bot commented Sep 14, 2023

@asotona Pushed as commit ca747f0.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

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 integrated Pull request has been integrated
3 participants