<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>Hi Mark,<br>
      interesting. On M1 we have currently few issues - see also this:</p>
    <p><a class="moz-txt-link-freetext" href="https://bugs.openjdk.org/browse/JDK-8275584">https://bugs.openjdk.org/browse/JDK-8275584</a></p>
    <p>Stack spilling doesn't always occur correctly, however I note
      that your example should not spill on the stack, so it should be
      immune.</p>
    <p>Is your C code correct? I see you do _two_ va_arg per loop
      iteration, which surely would leave you accessing invalid data.
      E.g. in this case n = 2, so the loop would do two iterations, and
      va_arg will be called _four_ times? Am I missing something?</p>
    <p>I'd rewrite it as:</p>
    <p>```<br>
          for(int i = 0; i < n; i++) { <br>
              int i = va_arg(ptr, int);<br>
              printf("lala %d", i);<br>
              Sum += i;<br>
          }<br>
      ```</p>
    <p>And test again.<br>
    </p>
    <p>Maurizio<br>
    </p>
    <div class="moz-cite-prefix">On 24/10/2022 13:40, Mark Hammons
      wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CA+QhayqHiyBT4Tru8huE4xgeeuv9kBVmGhkSFph-9d4EP7ye0A@mail.gmail.com">
      
      <div dir="ltr">
        <div>Hi all,  <br>
        </div>
        <div><br>
        </div>
        <div>I was testing panama on java 19 with my work issued macbook
          pro, and my varargs method bindings were returning invalid
          data.</div>
        <div><br>
        </div>
        <div>I thought that maybe it was a failure in my own code, but I
          couldn't work around it so I created a test instance using
          manually written code: <br>
        </div>
        <div><br>
        </div>
        <div>
          <div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Menlo,Monaco,"Courier New",monospace;font-weight:normal;font-size:12px;line-height:18px;white-space:pre-wrap"><div><span style="color:rgb(212,212,212)">  test(</span><span style="color:rgb(206,145,120)">"manual varargs"</span><span style="color:rgb(212,212,212)">) {</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">val</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">linker</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(78,201,176)">Linker</span><span style="color:rgb(212,212,212)">.nativeLinker().nn</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">val</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">sl</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(78,201,176)">SymbolLookup</span><span style="color:rgb(212,212,212)">.loaderLookup().nn</span></div>
<div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">val</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">mh</span><span style="color:rgb(212,212,212)"> = linker</span></div><div><span style="color:rgb(212,212,212)">      .downcallHandle(</span></div><div><span style="color:rgb(212,212,212)">        sl.lookup(</span><span style="color:rgb(206,145,120)">"add_var"</span><span style="color:rgb(212,212,212)">).nn.orElseThrow(),</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(78,201,176)">FunctionDescriptor</span><span style="color:rgb(212,212,212)">.of(</span><span style="color:rgb(78,201,176)">JAVA_INT</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(78,201,176)">JAVA_INT</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(78,201,176)">JAVA_INT</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(78,201,176)">JAVA_INT</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)">      )</span></div><div><span style="color:rgb(212,212,212)">      .nn;</span></div>
<div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">val</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">res</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(78,201,176)">MethodHandleFacade</span><span style="color:rgb(212,212,212)">.callVariadic(mh, </span><span style="color:rgb(181,206,168)">2</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">1</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">2</span><span style="color:rgb(212,212,212)">).</span><span style="color:rgb(220,220,170)">asInstanceOf</span><span style="color:rgb(212,212,212)">[</span><span style="color:rgb(78,201,176)">Int</span><span style="color:rgb(212,212,212)">]</span></div><div><span style="color:rgb(212,212,212)">    assertEquals(res, </span><span style="color:rgb(181,206,168)">3</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)">  }</span></div>
<div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Menlo,Monaco,"Courier New",monospace;font-weight:normal;font-size:12px;line-height:18px;white-space:pre-wrap"><div><span style="color:rgb(197,134,192)">#include</span><span style="color:rgb(86,156,214)"> </span><span style="color:rgb(206,145,120)"><stdarg.h></span></div><div><span style="color:rgb(197,134,192)">#include</span><span style="color:rgb(86,156,214)"> </span><span style="color:rgb(206,145,120)"><stdio.h></span></div>
<div><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">add_var</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">n</span><span style="color:rgb(212,212,212)">, ...) {</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)"> Sum = </span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)">;</span></div>
<div><span style="color:rgb(212,212,212)">    va_list ptr;</span></div>
<div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(220,220,170)">va_start</span><span style="color:rgb(212,212,212)">(ptr, n);</span></div>
<div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(197,134,192)">for</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)"> i = </span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)">; i < n; i++) { </span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)"> i = </span><span style="color:rgb(220,220,170)">va_arg</span><span style="color:rgb(212,212,212)">(ptr, </span><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)">);</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(220,220,170)">printf</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(206,145,120)">"lala </span><span style="color:rgb(156,220,254)">%d</span><span style="color:rgb(206,145,120)">"</span><span style="color:rgb(212,212,212)">, i);</span></div><div><span style="color:rgb(212,212,212)">        Sum += </span><span style="color:rgb(220,220,170)">va_arg</span><span style="color:rgb(212,212,212)">(ptr, </span><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)">);</span></div><div><span style="color:rgb(212,212,212)">    }</span></div></div><div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Menlo,Monaco,"Courier New",monospace;font-weight:normal;font-size:12px;line-height:18px;white-space:pre-wrap">
<div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(220,220,170)">va_end</span><span style="color:rgb(212,212,212)">(ptr);</span></div>
<div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(197,134,192)">return</span><span style="color:rgb(212,212,212)"> Sum;</span></div>}

<span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)">The output of the test code is consistent with my autogenerated code, </span></span></div><div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Menlo,Monaco,"Courier New",monospace;font-weight:normal;font-size:12px;line-height:18px;white-space:pre-wrap"><span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)">the Ints are not reaching the add_var function in a proper state, and therefore </span></span></div><div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Menlo,Monaco,"Courier New",monospace;font-weight:normal;font-size:12px;line-height:18px;white-space:pre-wrap"><span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)">add_var is returning something like 840370212 instead of 3. 
</span></span></div></div>
        </div>
      </div>
    </blockquote>
  </body>
</html>