2

I have the below structures in JNA. I want to remove the padding and alignment in C using pragma pack. When I run it from C it runs fine. I'm using this DLL to be called from Java. When I call it the JVM is crashing. Would my settings I did in my DLLs apply when its called from Java? I tried adding setAlignment type to None. Still no help. In C this structure is weighing 405. It adds up perfectly meaning there is no padding or alignment. In java when I hover over the _report value while debugging, it shows the size as 405 but it crashes. It doesn't crash when I specify the size to be 480. This is the size with padding and alignment. What am I doing wrong here?

#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) )

PACK(
typedef struct 
{
    size_t size;
    uint8_t bytes[48];
} ipj_tid_t);


PACK(
typedef struct 
{
    bool has_epc; //1
    ipj_epc_t epc; //64+8
    bool has_tid; //1
    ipj_tid_t tid; //48+8
    bool has_pc; //1
    uint32_t pc; //4
    bool has_xpc; //1
    uint32_t xpc; //4
    bool has_crc; //1
    uint32_t crc; //4
    bool has_timestamp; //1
    uint64_t timestamp; //8
    bool has_rssi; //1
    int32_t rssi; //4
    bool has_phase; //1
    int32_t phase; //4
    bool has_channel; //1
    uint32_t channel; //4
    bool has_antenna; //1
    uint32_t antenna; //4
} ipj_tag); //total size= 174


PACK(
typedef struct 
{
    bool has_error;
    ipj_error error;
    bool has_test_id;
    uint32_t test_id;
    bool has_result_1;
    uint32_t result_1;
    bool has_result_2;
    uint32_t result_2;
    bool has_result_3;
    uint32_t result_3;
    size_t data_count;
    uint32_t data[16];
    bool has_timestamp;
    uint64_t timestamp;
    size_t lt_buffer_count;
    uint32_t lt_buffer[21];
} ipj_test_report);


PACK(
typedef struct
{
    size_t size;
    uint8_t bytes[64];
} ipj_tag_operation_data_t);


PACK(
typedef  struct
{
    bool has_error;  //1
    ipj_error error; //4
    bool has_tag; //1
    ipj_tag tag; //174
    bool has_tag_operation_type; //1
    ipj_tag_operation_type tag_operation_type; //4
    bool has_tag_operation_data; //1
    ipj_tag_operation_data_t tag_operation_data; //72
    bool has_retries; //1
    uint32_t retries; //4
    bool has_diagnostic; //1
    uint32_t diagnostic; //4
    bool has_timestamp; //1
    uint64_t timestamp; //8
    size_t lt_buffer_count; //8
    uint32_t lt_buffer[30]; //120
} ipj_tag_operation_report); //405

Below are the JNA equivalent structures.

public class _ipj_report_id extends Structure {

    public int ipj_report_id;

    public _ipj_report_id() {
        super();
        setAlignType(ALIGN_NONE);
    }

    @Override
    protected List getFieldOrder() {
        return Arrays.asList("ipj_report_id");
    }

}

    public class _report extends Structure {

    public static class ByValue extends _report implements Structure.ByValue {

    }

    public _report() {
        super();
        setAlignType(ALIGN_NONE);
    }

    public _ipj_report_id reportid;
    public int data_size;
    public int[] data = new int[420]; // earlier was 138

    @Override
    protected List getFieldOrder() {
        return Arrays.asList("reportid", "data_size", "data");
    }

}
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
  • It would help if you actually named your Java classes after the corresponding native `struct`s. Assuming you meant `_report` to map onto `_ipj_tid_t`, your declared fields don't match up. – technomage Jan 22 '16 at 22:26
  • Possible duplicate of [Pragma Pack used C library causing the jvm to crash](http://stackoverflow.com/questions/34940652/pragma-pack-used-c-library-causing-the-jvm-to-crash) – technomage Jan 25 '16 at 11:39

0 Answers0