【问题标题】:Pragma Pack used C library causing the jvm to crashPragma Pack 使用 C 库导致 jvm 崩溃
【发布时间】:2016-04-28 16:42:13
【问题描述】:

我在使用 JNA 的 Java 代码中使用了 C 库。我有这些 C 结构,我需要用 Java 打印它们的成员。由于内存对齐和填充,大小与我实际预期的不同。因此我使用了 pragma pack,这就是现在的结构。

#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

当我从 Visual Studio 运行代码时,我没有收到任何错误、异常或崩溃。但是当我从 Java 调用它时,jvm 崩溃了。为什么会这样? C 代码中还有其他未打包的结构。这可能是一个原因吗?请指教。

我上面展示的结构是用在C中的,C代码返回的就是这个结构,经过所有的处理。

#define RX_MAX_SIZE     405
#pragma pack(push, 1)
typedef struct _report
{
    ipj_report_id report_id;
    uint32_t data_size;
    uint32_t data[RX_MAX_SIZE];
 } report;
 #pragma pack(pop)

已经使用的等效JNA结构如下。

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[405]; 

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

}

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");
    }

}

以下是我在调试 JNA 代码时得到的结构。

    _report$ByValue(auto-allocated@0x18918cc0 (1628 bytes)) {
   _ipj_report_id reportid@0=_ipj_report_id(allocated@0x18918cc0 (4 bytes) (shared from auto-allocated@0x18918cc0 (1628 bytes))) {
    int ipj_report_id@0=0
   }
   int data_size@4=195
   int data[405]@8=[I@78bb0ecb
 }

以下是我在 JNA 中如何调用此方法。

 rfidlib rlib = (rfidlib) Native.loadLibrary("rfidlib", rfidlib.class);
_report.ByValue report = new _report.ByValue();
 report = rlib.get_next_reports();

【问题讨论】:

  • 你能告诉我们你可能有任何错误输出吗?一种可能是您使用的 JVM 版本(例如 32 位与 64 位)。
  • 它没有显示任何错误。我确实在控制台上打印了输出。然后突然JVM崩溃了。当我删除包装代码时,它工作正常。它是一个 64 位 JVM 和 64 位 dll。请指教。
  • THIS 可能会有所帮助。
  • 我尝试在我的结构中将 setAlignmentType 设置为无对齐。仍然没有帮助。

标签: java c memory-management jvm jna


【解决方案1】:

JNA 假定当前目标平台(在本例中为 MS 编译器)的默认结构布局。

您可以在使用-Djna.dump_memory=true 调用JVM 后调用Structure.toString() 打印JNA 生成的内存布局的布局。

然后,您可以将所需的结构布局与 JNA 生成的实际布局进行比较,以寻找差异。如果您在 Java 结构中一般使用boolean,则默认分配一个本机int,因此如果它与您的本机bool 大小不同,则需要在Java 端使用byte 或向您的Structure 显式提供TypeMapper 以指示应将boolean 转换为与本机bool 兼容的大小(请注意,本机bool 的大小未由C 标准定义)。

我建议不要在您的代码中使用#pragma pack,除非您明确需要它。

请使用您的 Java Structure 实现及其调试输出更新您的问题。

【讨论】:

  • 我会在星期一检查这个并让你知道输出。我现在没有设备来运行程序:(
  • 我已经用所需的信息伙伴编辑了问题。你能帮我解决这个问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
  • 2016-06-25
相关资源
最近更新 更多