<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
Some review comments on this patch.  
<div class=""><br class="">
</div>
<div class="">Attributes with the LABELS stability need only be exploded and rewritten when the Code array is perturbed.  In practicality, LABELS attributes will also contain CP refs, so the doc should really suggest “labels *and* CP refs”.  </div>
<div class=""><br class="">
</div>
<div class="">HAZMAT can be renamed to UNSTABLE (yeah, I know HAZMAT is a cool name.)</div>
<div class=""><br class="">
</div>
<div class="">Doc on AttributeProcessingOPtion should make it clear that this is only during _transformation_; during reading/writing, all attributes are passed normally.  What should be the default here?  PASS, or DROP_UNSTABLE?  </div>
<div class=""><br class="">
</div>
<div class="">Now that the choices are simpler, I think you can add (constant) transforms for {CLASS,METHOD,FIELD,CODE}_drop{Unknown,Unstable}, and compose these in when the user calls transform() and the option setting is not PASS_ALL?</div>
<div class=""><br class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On Sep 5, 2023, at 5:17 AM, Adam Sotona <<a href="mailto:adam.sotona@oracle.com" class="">adam.sotona@oracle.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">I’ve updated<span class="Apple-converted-space"> </span><a href="https://github.com/openjdk/jdk/pull/15101" style="color: blue; text-decoration: underline;" class="">https://github.com/openjdk/jdk/pull/15101</a><span class="Apple-converted-space"> </span>so
 it filters on read and write based on the Classfile.AttributesProcessingOption context option.<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">Or I can prepare a patch with filtering transformations (as proposed below).<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">Or is there any other way?<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">Thanks,<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">Adam<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 11pt;" class=""><o:p class=""> </o:p></span></div>
<div id="mail-editor-reference-message-container" class="">
<div class="">
<div style="border-style: solid none none; border-top-width: 1pt; border-top-color: rgb(181, 196, 223); padding: 3pt 0cm 0cm;" class="">
<p class="MsoNormal" style="margin: 0cm 0cm 12pt; font-size: 10pt; font-family: Calibri, sans-serif;">
<b class=""><span style="font-size: 12pt;" class="">From:<span class="Apple-converted-space"> </span></span></b><span style="font-size: 12pt;" class="">Brian Goetz <<a href="mailto:brian.goetz@oracle.com" class="">brian.goetz@oracle.com</a>><br class="">
<b class="">Date:<span class="Apple-converted-space"> </span></b>Wednesday, 23 August 2023 19:26<br class="">
<b class="">To:<span class="Apple-converted-space"> </span></b>Adam Sotona <<a href="mailto:adam.sotona@oracle.com" class="">adam.sotona@oracle.com</a>>,
<a href="mailto:classfile-api-dev@openjdk.org" class="">classfile-api-dev@openjdk.org</a> <<a href="mailto:classfile-api-dev@openjdk.org" class="">classfile-api-dev@openjdk.org</a>><br class="">
<b class="">Subject:<span class="Apple-converted-space"> </span></b>Re: Attribute safety<o:p class=""></o:p></span></p>
</div>
<p class="MsoNormal" style="margin: 0cm 0cm 12pt; font-size: 10pt; font-family: Calibri, sans-serif;">
<span style="font-size: 13.5pt; font-family: "Courier New";" class="">Just a further thought on this: we can further focus our lens on _bound_ attributes, because these are the ones that have come from another classfile.  If the user creates a RVAA during a
 transform, we should assume that is fine, just as we do with writing.</span><span style="font-size: 11pt;" class=""><o:p class=""></o:p></span></p>
<div class="">
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 11pt;" class="">On 8/7/2023 6:46 AM, Adam Sotona wrote:<o:p class=""></o:p></span></div>
</div>
<blockquote style="margin-top: 5pt; margin-bottom: 5pt;" class="">
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">That makes perfect sense, attribute safety is more exactly an attribute transformation safety.</span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">I agree that introduction of special read/write filters (in a form of context options) is confusing and non-systematic.</span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class=""> </span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">When we focus on the implementation of the attributes transformation safety, I think the safety switch is less of a global context option but rather individual transformation immediate feature (a filtering
 feature).</span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class=""> </span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">If we implement it as a global context option, we would have to insert a filtering layer before each transformation (on read side) or after (on write side) of each transformation. I think it would be pretty
 much the same as filtering on read/write, except for the fact it will affect transformations only (so maybe even more confusing). Classfile::transform would then behave differently than its expanded form using Classfile::build.</span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class=""> </span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">However if we implement attribute transformation safety as specific transformations (doing the filtering job) – it should work in harmony with the rest of the API.</span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">For example in addition to ClassTransform.ACCEPT_ALL we can add ClassTransform.ACCEPT_ALL_KNOWN_ATTRIBUTES (dropping UNKNOWN) and ClassTransform.ACCEPT_ALL_SAFE_ATTRIBUTES (dropping HAZMAT).</span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class=""> </span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">As an interesting expansion of the ClassfileTransform features we can provide factories like for example ClassfileTransform::dropingAll(Predicate<ClassfileElement> filter) – where the “All” (or “Deep” or
 similar suffix) should indicate forced expansion of the whole tree, so the filter is really applied on all levels and filtered element never appears in the target class. It can be used to implement the global filtering transformations.</span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class=""> </span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">I propose to add following set of filtering transformations:</span><o:p class=""></o:p></div>
<ol start="1" type="1" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<li class="MsoListParagraph" style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="font-size: 11pt;" class="">ClassTransform.ACCEPT_ALL_KNOWN_ATTRIBUTES</span><o:p class=""></o:p></li><li class="MsoListParagraph" style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="font-size: 11pt;" class="">ClassTransform.ACCEPT_ALL_SAFE_ATTRIBUTES</span><o:p class=""></o:p></li><li class="MsoListParagraph" style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="font-size: 11pt;" class="">FieldTransform.ACCEPT_ALL_KNOWN_ATTRIBUTES</span><o:p class=""></o:p></li><li class="MsoListParagraph" style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="font-size: 11pt;" class="">FieldTransform.ACCEPT_ALL_SAFE_ATTRIBUTES</span><o:p class=""></o:p></li><li class="MsoListParagraph" style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="font-size: 11pt;" class="">MethodTransform.ACCEPT_ALL_KNOWN_ATTRIBUTES</span><o:p class=""></o:p></li><li class="MsoListParagraph" style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="font-size: 11pt;" class="">MethodTransform.ACCEPT_ALL_SAFE_ATTRIBUTES</span><o:p class=""></o:p></li><li class="MsoListParagraph" style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="font-size: 11pt;" class="">CodeTransform.ACCEPT_ALL_KNOWN_ATTRIBUTES</span><o:p class=""></o:p></li><li class="MsoListParagraph" style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="font-size: 11pt;" class="">CodeTransform.ACCEPT_ALL_SAFE_ATTRIBUTES</span><o:p class=""></o:p></li></ol>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class=""> </span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">Thanks,</span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-US" style="font-size: 11pt;" class="">Adam</span><o:p class=""></o:p></div>
<div style="margin: 0cm; font-size: 10pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 11pt;" class=""> </span><o:p class=""></o:p></div>
<div id="mail-editor-reference-message-container" class="">
<div class="">
<div style="border-style: solid none none; border-top-width: 1pt; border-top-color: rgb(181, 196, 223); padding: 3pt 0cm 0cm;" class="">
<p class="MsoNormal" style="margin: 0cm 0cm 12pt; font-size: 10pt; font-family: Calibri, sans-serif;">
<b class=""><span style="font-size: 12pt;" class="">From:<span class="Apple-converted-space"> </span></span></b><span style="font-size: 12pt;" class="">Brian Goetz<span class="Apple-converted-space"> </span><a href="mailto:brian.goetz@oracle.com" style="color: blue; text-decoration: underline;" class=""><brian.goetz@oracle.com></a><br class="">
<b class="">Date:<span class="Apple-converted-space"> </span></b>Saturday, 5 August 2023 3:01<br class="">
<b class="">To:<span class="Apple-converted-space"> </span></b>Adam Sotona<span class="Apple-converted-space"> </span><a href="mailto:adam.sotona@oracle.com" style="color: blue; text-decoration: underline;" class=""><adam.sotona@oracle.com></a>,<span class="Apple-converted-space"> </span><a href="mailto:classfile-api-dev@openjdk.org" style="color: blue; text-decoration: underline;" class="">classfile-api-dev@openjdk.org</a><span class="Apple-converted-space"> </span><a href="mailto:classfile-api-dev@openjdk.org" style="color: blue; text-decoration: underline;" class=""><classfile-api-dev@openjdk.org></a><br class="">
<b class="">Subject:<span class="Apple-converted-space"> </span></b>Re: Attribute safety</span><o:p class=""></o:p></p>
</div>
<p class="MsoNormal" style="margin: 0cm 0cm 12pt; font-size: 10pt; font-family: Calibri, sans-serif;">
<span style="font-size: 13.5pt; font-family: "Courier New ,serif";" class="">OK, I thought about this some more while sitting in the sauna ...<br class="">
<br class="">
I think the locus of attribute safety is not reading or writing, but transforming.  If I am just going to read a classfile, there is no need to drop anything, if I find an attribute I don't recognize, I'll just skip over it and keep going -- that's how attributes
 are designed to work.  No need to drop anything on read, ever.  WHen the library finds an unknown attribute, it wraps it with an UnknownAttribute element, whose understanding to the attribute is limited to name, size, and byte[] of the payload.  Nothing so
 dangerous here that the user needs protection.<br class="">
<br class="">
Similarly, if a user is _writing_ a classfile, again, we should trust them that the classfile they are putting together is sensible.  We shouldn't second guess at "oh, that's a type annotations attribute, those are so brittle, please sign here." <span class="Apple-converted-space"> </span><br class="">
<br class="">
Where there is potentially a problem is when we are _transforming_ a classfile, because for a HAZMAT or UNKNOWN attribute, we can't guarantee its integrity if we've changed anything else about the classfile (including reordering the constant pool.)  So the
 "what do we do with brittle attributes" question applies only to transformation, where we are taking an attribute from one classfile (a bound attribute) and writing it to another.  This is where the user can shoot themselves in the foot, because they might
 change something else about the classfile and subtly (or not subtly) undermine the integrity of the annotation they don't understand.  And this is why we want to classify attributes according to their sensitivity to environmental change:<span class="Apple-converted-space"> </span><br class="">
<br class="">
  - A stateless attribute is sensitive to no environmental changes.  A transform can always safely bulk-copy the attribute directly.<br class="">
<br class="">
  - An attribute with CP dependencies is sensitive to restructuring of the constant pool (no CP sharing), but the mapper contains enough information to survive CP restructuring.  A transform can safely bulk-copy the attribute directly if the CP is shared between
 the original and new classfile, and can otherwise safely copy the attribute by inflating it and deflating it via the readAttribute/writeAttribute behavior of the mapper. <span class="Apple-converted-space"> </span><br class="">
<br class="">
 - An attribute with label dependencies is sensitive to changes to the contents or structure of the bytecode array.  A transform can safely bulk-copy the attribute directly if code array is unchanged, and can otherwise safely copy the attribute by inflating
 it and deflating it via the readAttribute/writeAttribute behavior of the mapper.  However, there are currently no attributes that have label dependencies only but are not already treated specially by the classfile API, so this category may not be that interesting.<br class="">
<br class="">
 - An attribute with unpredictable dependencies is sensitive to any change to the contents of the entity of which it is an attribute.  It can be safely bulk-copied if nothing else in that entity has changed, but otherwise there is no safe way to copy it. <span class="Apple-converted-space"> </span><br class="">
<br class="">
 - An unknown attribute is sensitive to all of the above, and so takes on the union of the copying risks of all of the above.<br class="">
<br class="">
So I think the Option we want governs what to do with various attributes when _transforming_ a CompoundElement in which they appear.  And the problematic cases are those with unpredictable dependencies, and unknowns.  So I think the options we want are:<br class="">
<br class="">
 - When transforming, always keep HAZMAT and UNKNOWN attributes; for safety, lift and lower HAZMAT attributes. <span class="Apple-converted-space"> </span><br class="">
 - When transforming, keep HAZMAT attributes (lifting and lowering), but always drop UNKNOWN attributes.<br class="">
 - When transforming, always drop HAZMAT and UNKNOWN attributes. <span class="Apple-converted-space"> </span></span></p>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>