unreferencing pointers

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Tue Dec 17 11:37:48 UTC 2019


On 17/12/2019 11:23, Ty Young wrote:
>
> On 12/17/19 5:07 AM, Maurizio Cimadamore wrote:
>>
>> On 17/12/2019 11:03, Ty Young wrote:
>>>
>>> On 12/17/19 4:31 AM, Maurizio Cimadamore wrote:
>>>>
>>>> On 17/12/2019 08:32, Ty Young wrote:
>>>>> Hi,
>>>>>
>>>>>
>>>>> Thanks to the LLVM 9 commit I've been able to create bindings for 
>>>>> jextract. Problem is that the libusb API utilizes "unreference" 
>>>>> variables to store the list of USB devices at it's entry pointer.
>>>>>
>>>>>
>>>>> From the "In a nutshell..." section:
>>>>>
>>>>>
>>>>> libusb_device **list;
>>>>>
>>>>> libusb_device *found = NULL;
>>>>>
>>>>> ssize_t cnt = libusb_get_device_list(NULL, &list);
>>>>>
>>>>>
>>>>> ...which -unless I'm missing something- doesn't work in Java since 
>>>>> variables have to be declared as *something* and declaring it as 
>>>>> null hits an internal NullPointerException check.
>>>>>
>>>>>
>>>>> Is this an unsupported edge case or am I missing something?
>>>>
>>>> What is the Java code that corresponds to this?
>>>
>>>
>>> In Java the function signature looks like:
>>>
>>>
>>> long libusb_lib.libusb_get_device_list(Pointer<libusb_context> 
>>> context, Pointer<Pointer<Pointer<libusb_h.libusb_device>>> devices);
>>>
>>>
>>> Where long is the size of the devices list. First argument can be 
>>> null, as documented.
>>
>> Have you tried Pointer.ofNull() ?
>
>
> Pointer.ofNull() just causes a hard segfault.
>
>
> Usually doing Pointer.ofNull() would be required for this, but unlike 
> other functions I've seen, this is returning the entire "list" itself 
> and not individual elements(Pointer<libusb_h.libusb_devic> in this 
> case). The Pointer<Pointer<Pointer<>>> madness makes no sense.

I don't know what you mean. To me, looking at the C signature you shared 
(although you don't paste full signature - but I can infer from usage), 
what jextract does seems ok - you have one parameter that is a 
*listusb_device, and another parameter that seems to be a pointer to a 
**listusb_device (hence the triple pointer in the jextract signature).

If you pass Pointer.ofNull as first parameter and the function 
segfaults, it seems to me that, maybe, there is some misunderstanding in 
how the function should be called? (e.g. maybe a NULL parameter is not 
valid?). Or, perhaps the segfault is not related to the first parameter, 
but rather to the second (likely), for which I'm still not clear what 
exactly you're doing.

To be clear, I'd expect the second parameter to be something like:

Pointer<Pointer<Pointer<libusb_h.libusb_device>>> devices = 
scope.allocate(LayoutType.ofStruct(libusb_h.libusb_device.class).pointer().pointer());
...
libusb_get_device_list(Pointer.ofNull(), devices)

Maurizio

>
>
>>
>> Maurizio
>>
>>>
>>>
>>>> The one you are playing with. Is 'list' the variable you'd like to 
>>>> start off with null? Note that that is not really what C does - 
>>>> when C sees a blank declaration like that it would still allocate 
>>>> the space to hold a pointer - so you must allocate, even if you 
>>>> don't see the allocation in the corresponding C code.
>>>>
>>>> Maurizio
>>>>
>>>>>
>>>>>
>>>>> Thanks.
>>>>>


More information about the panama-dev mailing list