2

I have a key value database which is written in C with following functions to set and get values.

typedef uint64_t ARK;
#define ARC ARK

int ark_set(ARK *ark, uint64_t klen, void *key, uint64_t vlen, void *val, int64_t *rval)

int ark_get(ARK *ark, uint64_t klen, void *key, uint64_t vbuflen, 
            void *vbuf, uint64_t voff, int64_t *res);

And some other functions to create the ARK, count, allocated and used bytes. They work as expected however whenever I'm trying to set or get a value JVM crashes.

Here is my JNR interface

public interface ARKInterface {
    int ark_create(String file, @u_int64_t Pointer ark, long flags);

    int ark_count(@u_int64_t Pointer ark, @Out NumberByReference count);

    int ark_set(@u_int64_t Pointer ark, @u_int64_t NativeLong klen, Pointer key, 
       @u_int64_t NativeLong vlen, Pointer val, @Out NumberByReference res);

   int ark_get(@u_int64_t Pointer ark, long klen, Pointer key, long vbuflen, 
       Pointer vbuf, long voff, @Out NumberByReference res);
}

And here what I'm trying to Set the value

public static void main(String[] args) {
        ARKInterface libc = LibraryLoader.create(ARKInterface.class).load("arkdb-0");

        Pointer ark = Memory.allocateDirect(Runtime.getRuntime(libc), TypeAlias.u_int64_t);
        int _create = libc.ark_create(null, ark, 1);

        System.out.println("ARK Creation return " + _create);
        System.out.println(ark.toString());

        NumberByReference count = new NumberByReference(TypeAlias.int64_t);
        int _count = libc.ark_count(ark, count);

        System.out.println("Count method return value " + _count);

        System.out.println("Total Count is " + count.longValue());

        byte[] _key = "key".getBytes();
        byte[] _value = "value".getBytes();


        Pointer kp = Memory.allocateDirect(Runtime.getRuntime(libc), _key.length);
        kp.put(0, _key, 0, _key.length);

        Pointer vp = Memory.allocateDirect(Runtime.getRuntime(libc), _value.length);
        vp.put(0, _value, 0, _value.length);

        int set_result = libc.ark_set(ark, new NativeLong(_key.length), kp, new NativeLong(_value.length), vp, count);
        System.out.println("ARK Sets and the result is - " + set_result);
}

After running the jar I'm getting following fatal error, nothing much in coredumps.

ARK Creation return 0
jnr.ffi.provider.BoundedMemoryIO[address=0x7f12e8026000 size=8]
Count method return value 0
Total Count is 0
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGFPE (0x8) at pc=0x00007f12b1886133, pid=83423, tid=0x00007f12ec663700
#
# JRE version: OpenJDK Runtime Environment (8.0_181-b13) (build 1.8.0_181-b13)
# Java VM: OpenJDK 64-Bit Server VM (25.181-b13 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [libarkdb-0.so+0x23133]  ark_enq_cmd+0x533
#
# Core dump written. Default location: /tmp/capi/core or core.83423
#
# An error report file with more information is saved as:
# /tmp/capi/hs_err_pid83423.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Aborted (core dumped)

Is there anything wrong related to mapping or while passing values.

Riyaz
  • 120
  • 7
  • Are you sure you have the right encoding? `getBytes()` uses the system default encoding. – Jorn Vernee Oct 14 '18 at 15:14
  • I modified the code to `byte[] _key = "key".getBytes(StandardCharsets.US_ASCII); byte[] _value = "value".getBytes(StandardCharsets.US_ASCII);` but still same error. – Riyaz Oct 14 '18 at 15:27
  • Well, you're code looks fine to me, as you can see the crash doesn't happen in VM code, but in the library you're calling: `# Problematic frame: # C [libarkdb-0.so+0x23133] ark_enq_cmd+0x533`. That usually either means that the arguments you're passing are illegal (doesn't look like that to me), or there is a bug in the library itself. – Jorn Vernee Oct 14 '18 at 15:32
  • I took the library from here [link](https://github.com/open-power/capiflash) I build it and run `make tests` it runs as expected the output of the samples are good. – Riyaz Oct 14 '18 at 15:34
  • I found the solution actually while creating ark the definition of JNR mapping should be like `int ark_create(String file, @u_int64_t PointerReference ark, long flags);` – Riyaz Oct 18 '18 at 15:00

0 Answers0