Passing an object pointer

25 views
Skip to first unread message

Mahmut Orak

unread,
Mar 4, 2022, 4:00:19 PM3/4/22
to NativeLibs4Java
Hello all,
I have a jnaerator generated(runtime=BridJ) code like this:

@Library("somelib")
@Runtime(CRuntime.class)
public class Key_storeLibrary {
    ...
    public static abstract class sym128_key_provider extends     Callback<sym128_key_provider> {
        @Ptr
        abstract public long apply(Pointer<Pointer<Byte>> keybuf, Pointer<Pointer<Byte>>            key_id, int key_slot, Pointer<?> data_passback);
    }
    ...
    public static void set_sym128_key_provider(Pointer<sym128_key_provider>       sym128_key_provider1) {
    set_sym128_key_provider(Pointer.getPeer(sym128_key_provider1));
   }

   protected native static      void set_sym128_key_provider(@Ptr long sym128_key_provider1);
    ...
}

Now i'm trying to call this generated code like this:

KEY_PROVIDER_SETTING_CALL
Pointer<Key_storeLibrary.sym128_key_provider> sym128KeyProvider = Pointer.allocate((Type) sym128_key_providerImpl.class);

Key_storeLibrary.set_sym128_key_provider(sym128KeyProvider);

Here is our sym128_key_providerImpl implementation, which implements generated Key_storeLibrary.sym128_key_provider abstract class.

public class sym128_key_providerImpl extends Key_storeLibrary.sym128_key_provider {

private Long KEY_PROVIDER_SIZE = 16L;

@Override
public long apply(Pointer<Pointer<Byte>> key, Pointer<Pointer<Byte>> key_id, int key_slot, Pointer<?> data_passback) {

  if (key_slot != 0 && key_slot != 1)
   return 0L;

   byte[][] keys = {
      {
         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
         0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
      },
      {
          0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
          0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
      }
   };

   key.get().setBytes(keys[key_slot]);
   return KEY_PROVIDER_SIZE;
  }
}

Above KEY_PROVIDER_SETTING_CALL  returns ok. Btw this sym128KeyProvider is a C callback, it is used whenever our api tries to encode some input. C code calls this key_provider. Whenever C Api tries to call our key_provider's apply() function, it is crashing the JVM and causing a core dump.

Any ideas?
Thank you in advance.
 

Olivier Chafik

unread,
Mar 4, 2022, 4:21:02 PM3/4/22
to nativel...@googlegroups.com
Hi Mahmut,

Pointer<Key_storeLibrary.sym128_key_provider> sym128KeyProvider = Pointer.allocate((Type) sym128_key_providerImpl.class);

This syntax works to allocate pointers to structs, but since this is a callback you probably should use this instead:

sym128_key_providerImpl callback = new sym128_key_providerImpl();
// keep a pointer to that callback or call  BridJ.protectFromGC(callback), as otherwise the callback will be garbage-collected. 
Pointer<Key_storeLibrary.sym128_key_provider> sym128KeyProvider = Pointer.pointerTo(callback);

(also if you face further issues it would be worth posting relevant sections of the original C API and example C code you're trying to port)


--
You received this message because you are subscribed to the Google Groups "NativeLibs4Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nativelibs4ja...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nativelibs4java/7133f8a5-9a5f-40ab-a2a0-45750a0a46d5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages