<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Aptos;
        panose-1:2 11 0 4 2 2 2 2 2 4;}
@font-face
        {font-family:"Helvetica Neue";
        panose-1:2 0 5 3 0 0 0 2 0 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:12.0pt;
        font-family:"Aptos",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:Consolas;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;
        mso-ligatures:none;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt">Hi,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">You are suggesting very specific operations with significant performance effects to be applied for all ClassFile API use cases.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Instead of wiring a specific code inside all the code building I suggest you to look at
<a href="https://download.java.net/java/early_access/jdk22/docs/api/java.base/java/lang/classfile/components/CodeStackTracker.html">
java.lang.classfile.components.CodeStackTracker</a> for inspiration.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">This way you can (conditionally) plug any custom transformation into your code building. For example a transformation that would print out a debug output you asked in another thread, or you may track origin
 of labels, verify actual stack content, print out any additional info, all optional and without affecting the core performance.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Adam<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<div id="mail-editor-reference-message-container">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="margin-bottom:12.0pt"><b><span style="color:black">From:
</span></b><span style="color:black">classfile-api-dev <classfile-api-dev-retn@openjdk.org> on behalf of Øystein Myhre Andersen <o.myhre@gmail.com><br>
<b>Date: </b>Sunday, March 10, 2024 at 16:04<br>
<b>To: </b>liangchenblue@gmail.com <liangchenblue@gmail.com><br>
<b>Cc: </b>classfile-api-dev@openjdk.org <classfile-api-dev@openjdk.org><br>
<b>Subject: </b>Re: Checking for 'Stack size mismatch'<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Sorry again: Now I mess up: it's possible like this:<br>
<br>
 - At labelBinding what is in the  'come-from' list  is checked<br>
 - With a later backward jump to this label, it is checked again</p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Sun, Mar 10, 2024 at 3:54<span style="font-family:"Arial",sans-serif"> </span>PM Øystein Myhre Andersen <<a href="mailto:o.myhre@gmail.com">o.myhre@gmail.com</a>> wrote:</p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<p class="MsoNormal">Sorry: This does not handle backward jumps.</p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">But   a 'come-from' list  might be useful for the <span style="color:#500050">StackMapGenerator.</span></p>
</div>
<div>
<p class="MsoNormal">And, maybe speed it up.</p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">P.S. In the byte code of Simula, we had the rule that a label only had one and only one associated goto.</p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Sun, Mar 10, 2024 at 3:21<span style="font-family:"Arial",sans-serif"> </span>PM Øystein Myhre Andersen <<a href="mailto:o.myhre@gmail.com" target="_blank">o.myhre@gmail.com</a>> wrote:</p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<p class="MsoNormal"><br>
Yes, I know which method builder is called.<br>
I have a class file builder like:<br>
<br>
byte[] bytes = ClassFile.of().build(currentClassDesc(),<br>
      classBuilder -> {<br>
            classBuilder<br>
                  ....<br>
                  .withMethodBody("_STM", MTD_STM, ClassFile.ACC_PUBLIC,<br>
                        codeBuilder -> buildMethod_STM(codeBuilder));<br>
      }<br>
);<br>
<br>
Within 'buildMethod_STM' I traverse a syntax tree building code from each node.<br>
Somewhere there a mistake is made that leads to 'stack size mismatch'.<br>
<br>
When I return from 'buildMethod_STM', the method builder will finalize its work.<br>
It generates the bytecode and builds the stack map table.<br>
I assume that it checks the stack size at all labels, it must do so to build a correct stack map table.<br>
<br>
Along with the error message comes a complete dump of the entire method.<br>
<br>
Checking for 'Stack size mismatch' is currently done in 'java.base/jdk.internal.classfile.impl.StackMapGenerator'.<br>
It would have been nice if this was tested earlier by Codebuilder when the labelBinding pseudo instruction was created.</p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">One way it might be done is to build a 'come-from' list within labelBinding pseudo instruction.</p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Sat, Mar 9, 2024 at 10:10<span style="font-family:"Arial",sans-serif"> </span>PM - <<a href="mailto:liangchenblue@gmail.com" target="_blank">liangchenblue@gmail.com</a>> wrote:</p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<p class="MsoNormal">What are you trying to do? So you are writing a class and there's a stack size error, but you don't know which method builder this stack size error is from?</p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Sat, Mar 9, 2024 at 2:04<span style="font-family:"Arial",sans-serif"> </span>PM Øystein Myhre Andersen <<a href="mailto:o.myhre@gmail.com" target="_blank">o.myhre@gmail.com</a>> wrote:</p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<p class="MsoNormal">You said:  as this error can only be detected at run time.</p>
<div>
<p class="MsoNormal">That is wrong. It occurs when building stack-map.</p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Sat, Mar 9, 2024 at 7:38<span style="font-family:"Arial",sans-serif"> </span>PM - <<a href="mailto:liangchenblue@gmail.com" target="_blank">liangchenblue@gmail.com</a>> wrote:</p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<p class="MsoNormal">Hi Øystein,</p>
<div>
<p class="MsoNormal">ClassFile API does not always ensure the created bytecode is valid, and allowing creation of code with invalid stack size is a feature instead of a bug. A stack size mismatch problem, in essence, is closer to the error which a user forgot
 to call a builder method. And putting this check earlier doesn't really help, as this error can only be detected at run time. You can always use ClassFile.verify to verify your generated class files.</p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">In addition, CodeBuilder supports chaining; if we add the size checking in each item of the chain, it will introduce a performance penalty.</p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Chen</p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Sat, Mar 9, 2024 at 8:42<span style="font-family:"Arial",sans-serif"> </span>AM Øystein Myhre Andersen <<a href="mailto:o.myhre@gmail.com" target="_blank">o.myhre@gmail.com</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<pre style="margin-bottom:7.5pt;word-break:break-all;box-sizing:border-box;overflow:auto"><span style="font-size:10.5pt;font-family:"Helvetica Neue";color:#333333">Checking for 'Stack size mismatch' is currently done in 'java.base/jdk.internal.classfile.impl.StackMapGenerator'.<o:p></o:p></span></pre>
<pre style="margin-bottom:7.5pt;word-break:break-all"><span style="font-size:10.5pt;font-family:"Helvetica Neue";color:#333333">It would have been nice if this was tested earlier by Codebuilder when the instructions was created.<o:p></o:p></span></pre>
<pre style="margin-bottom:7.5pt;word-break:break-all"><span style="font-size:10.5pt;font-family:"Helvetica Neue";color:#333333"><o:p> </o:p></span></pre>
<pre style="margin-bottom:7.5pt;word-break:break-all"><span style="font-size:10.5pt;font-family:"Helvetica Neue";color:#333333">For example, by keeping track of the stack size curing code building.<o:p></o:p></span></pre>
<pre style="margin-bottom:7.5pt;word-break:break-all"><span style="font-size:10.5pt;font-family:"Helvetica Neue";color:#333333"><o:p> </o:p></span></pre>
<pre style="margin-bottom:7.5pt;word-break:break-all"><span style="font-size:10.5pt;font-family:"Helvetica Neue";color:#333333">It is good programming practice to catch errors as early as possible.<o:p></o:p></span></pre>
</div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</body>
</html>