[foreign-jextract] Function pointer typedef name clashes with existing function name

Filip Krakowski krakowski at hhu.de
Wed Feb 10 14:05:05 UTC 2021


Hi,

I think I found another bug regarding name clashes with typedefs for 
function pointers.

The library I use declares a function pointer within a struct's field 
named "read" [1]. I think you see where this is going...

    typedef struct ucs_config_parser {
         int                      (*read) (const char *buf, void *dest,
    const void *arg);
         int                      (*write)(char *buf, size_t max,
                                           const void *src, const void
    *arg);
         ucs_status_t             (*clone)(const void *src, void *dest,
    const void *arg);
         void                     (*release)(void *ptr, const void *arg);
         void                     (*help)(char *buf, size_t max, const
    void *arg);
         const void               *arg;
    } ucs_config_parser_t;


Since "unistd.h" also declares a "read" function [2], jextract then 
(correctly) generates a MethodHandle for it
but uses the wrong FunctionDescriptor belonging to ucx's "read" in the 
process.

    static final FunctionDescriptor read$FUNC_ =
    FunctionDescriptor.of(C_INT,
             C_POINTER,
             C_POINTER,
             C_POINTER
    );

    static final MethodHandle read$MH_ = RuntimeHelper.downcallHandle(
             LIBRARIES, "read",
             "(ILjdk/incubator/foreign/MemoryAddress;J)J",
             read$FUNC_, false
    );


As you can see the generated FunctionDescriptor and the descriptor 
string ("(ILjdk/incubator/foreign/MemoryAddress;J)J")
do not match. On startup, I immediately get the following Exception 
originating from the downcallHandle call.

    Caused by: java.lang.IllegalArgumentException: Carrier size
    mismatch: int != b64[abi/kind=POINTER]

My guess is that the FunctionDescriptor is generated correctly at first, 
but gets overridden once the parser reaches the
function pointer declared within the struct in [1].


Best regards
Filip

[1] 
https://github.com/openucx/ucx/blob/16ad2cc56b6c63a0fdc39bac3dce03463d6d137f/src/ucs/config/parser.h#L40-L48
[2] https://linux.die.net/man/2/read


More information about the panama-dev mailing list