<div dir="ltr">Hi Adam and the list,<div>I have since created JDK-8332614 and opened <a href="https://github.com/openjdk/jdk/pull/19330">https://github.com/openjdk/jdk/pull/19330</a> pull request. Feel free to take a look and comment!</div><div><br></div><div>Thanks, Chen</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, May 21, 2024 at 7:19 AM Adam Sotona <<a href="mailto:adam.sotona@oracle.com">adam.sotona@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg6966297401307272877">





<div lang="EN-US" style="overflow-wrap: break-word;">
<div class="m_6966297401307272877WordSection1">
<p class="MsoNormal"><span style="font-size:11pt">Hi Chen,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">Internally it is already resolved, and it does not bring much of a value to change only internal implementation.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">I'm OK with the proposed API addition, however real use cases would give the proposal more weight.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">Priority is to clean the Class-File API and additions to the API should be backed by real use cases or visible benefits in the existing code.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">Procedurally, feel free to go ahead and propose it to 23.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">Thanks,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">Adam<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<div id="m_6966297401307272877mail-editor-reference-message-container">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0in 0in">
<p class="MsoNormal" style="margin-bottom:12pt"><b><span style="color:black">From:
</span></b><span style="color:black">Chen Liang <<a href="mailto:liangchenblue@gmail.com" target="_blank">liangchenblue@gmail.com</a>><br>
<b>Date: </b>Tuesday, 21 May 2024 at 13:54<br>
<b>To: </b>classfile-api-dev <<a href="mailto:classfile-api-dev@openjdk.org" target="_blank">classfile-api-dev@openjdk.org</a>>, <a href="mailto:asotona@openjdk.org" target="_blank">asotona@openjdk.org</a> <<a href="mailto:asotona@openjdk.org" target="_blank">asotona@openjdk.org</a>><br>
<b>Subject: </b>Re: Type-checked entryByIndex and readEntryOrNull<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal">Hi Adam,<u></u><u></u></p>
<div>
<p class="MsoNormal">This patch is simple; since this can prevent a lot of bugs around malicious CP references in crafted classfiles, should we consider this enhancement for JDK 23, or should we only have this as internal APIs in ClassReaderImpl?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">- Chen<u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Mon, May 20, 2024 at 9:08<span style="font-family:Arial,sans-serif"> </span>AM - <<a href="mailto:liangchenblue@gmail.com" target="_blank">liangchenblue@gmail.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<p class="MsoNormal">Hi ClassFile API list,<u></u><u></u></p>
<div>
<p class="MsoNormal">I call for addition of a type-checked entryByIndex in ConstantPool, with a signature:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><T extends PoolEntry> T entryByIndex(int index, Class<T> cls)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">and a type-checked readEntryOrNull in ClassReader, with a signature:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><T extends PoolEntry> T readEntryOrNull(int offset, Class<T> cls)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Which will throw ConstantPoolException if the entry is of a mismatched type, much like the type-checked readClassEntry in ClassBuilder.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">A search for existing generic ConstantPool::entryByIndex and ClassReader::readEntryOrNull in JDK reveals that most of their usages within the jdk.internal.classfile.impl and its subpackages involve a direct cast right after retrieving the
 result. These casts are susceptible to malformed classfiles putting entries of wrong type, such as a Utf8 at the cursor of superclass entry, throwing ClassCastException, which is out of spec with the Classfile API.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I recommend adding these 2 methods for user convenience, and migrating all existing entryByIndex/readEntryOrNull with casts to these 2 new methods, to enhance the robustness of the ClassFile API. (On a side note, we can promote ClassReader::utf8EntryByIndex
 to ConstantPool too)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Please feel free to comment or critique this proposal.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Chen Liang<u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>

</div></blockquote></div>