<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>Hi Mark,</p>
    <p>It looks like you are missing a call to
      `FunctionDescriptor::asVariadic`. This doesn't matter on every
      platform, but on M1 it does, since it affects how arguments are
      passed.<br>
      <br>
      Looking at the C code you have, the function descriptor you use
      should be `FunctionDescriptor.of(JAVA_INT,
      JAVA_INT).asVariadic(JAVA_INT, JAVA_INT)`</p>
    <p>Jorn<br>
    </p>
    <div class="moz-cite-prefix">On 24/10/2022 14: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>