Varargs bindings do not work in java 19 on the macbook pro M1 MAX
Mark Hammons
markehammons at gmail.com
Mon Oct 24 15:24:55 UTC 2022
Hi Jorn, Maurizio,
When I make the change suggested by Jorn, the bindings work as expected.
Thanks,
Mark
On Mon, Oct 24, 2022 at 5:10 PM Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> wrote:
> Do you get the error even if you add the asVariadic call, as noted by Jorn?
>
> Maurizio
> On 24/10/2022 15:38, Mark Hammons wrote:
>
> Sorry, yes there's an error there. However, this was not the first place I
> noticed the error. Rather, I noticed that my binding to sprintf was
> returning halfway malformed strings like `1810529328 hello:
> �_�����{���\���^��@xA�!$@�!@����@Q@ 922798592` when the following test
> code was run:
> ```
> val format = Ptr.copy("%i hello: %s %i")
> val buffer = Ptr.blankArray[Byte](256)
> assertEquals(format.copyIntoString(200), "%i hello: %s %i")
> Cstd.sprintf(buffer, format, 1, Ptr.copy("hello"), 2)
> assertEquals(buffer.copyIntoString(256), "1 hello: hello 2")
> ```
>
> With the fixed code above, I get the following result from add_var:
>
> -1884714972
>
> ~Mark
>
> On Mon, Oct 24, 2022 at 3:23 PM Maurizio Cimadamore <
> maurizio.cimadamore at oracle.com> wrote:
>
>> Hi Mark,
>> interesting. On M1 we have currently few issues - see also this:
>>
>> https://bugs.openjdk.org/browse/JDK-8275584
>>
>> Stack spilling doesn't always occur correctly, however I note that your
>> example should not spill on the stack, so it should be immune.
>>
>> 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?
>>
>> I'd rewrite it as:
>>
>> ```
>> for(int i = 0; i < n; i++) {
>> int i = va_arg(ptr, int);
>> printf("lala %d", i);
>> Sum += i;
>> }
>> ```
>>
>> And test again.
>>
>> Maurizio
>> On 24/10/2022 13:40, Mark Hammons wrote:
>>
>> Hi all,
>>
>> I was testing panama on java 19 with my work issued macbook pro, and my
>> varargs method bindings were returning invalid data.
>>
>> 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:
>>
>> test("manual varargs") {
>> val linker = Linker.nativeLinker().nn
>> val sl = SymbolLookup.loaderLookup().nn
>> val mh = linker
>> .downcallHandle(
>> sl.lookup("add_var").nn.orElseThrow(),
>> FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT)
>> )
>> .nn;
>> val res = MethodHandleFacade.callVariadic(mh, 2, 1, 2).asInstanceOf[Int]
>> assertEquals(res, 3)
>> }
>> #include <stdarg.h>
>> #include <stdio.h>
>> int add_var(int n, ...) {
>> int Sum = 0;
>> va_list ptr;
>> va_start(ptr, n);
>> for(int i = 0; i < n; i++) {
>> int i = va_arg(ptr, int);
>> printf("lala %d", i);
>> Sum += va_arg(ptr, int);
>> }
>> va_end(ptr);
>> return Sum;
>> } The output of the test code is consistent with my autogenerated code,
>> the Ints are not reaching the add_var function in a proper state, and
>> therefore
>> add_var is returning something like 840370212 instead of 3.
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20221024/e72148f2/attachment-0001.htm>
More information about the panama-dev
mailing list