JNA 调用中的间歇性内存损坏错误
Intermittent memory corruption errors in JNA calls
我们有一个非常简单的要求,即从 Java 调用几个本机函数。我们正在使用 JNA 进行这些本机调用。
编辑:我们没有任何自定义本机代码。我们正在调用 Linux 内核 C 库函数。
我们收到非常奇怪的内存损坏错误,例如
- `/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/bin/java' 错误:malloc():内存损坏:0x00007f9b7849fc40
- `/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/bin/java' 中的错误:损坏的大小与 prev_size:0x00007f253c4470f0
- SIGSEGV (0xb)
程序有时甚至会挂起。这些错误是间歇性的。
一些关于在 JNA 调用中使用结构的标准 examples/docs 会有所帮助。
这是我们的具有本机功能的库包装器:
这些是映射到 C:
结构的原生模型
这就是我们访问库函数的方式:
val timeVal = new NTPTimeVal()
TimeLibrary.ntp_gettimex(timeVal)
println(timeVal.tai)
您可以参考 TimeServiceImpl.scala 更清楚。
有人能告诉我们到底哪里做错了吗?
ntptimeval
和相关结构中有一些保留字段:
struct ntptimeval
{
struct timeval time; /* current time (ro) */
long int maxerror; /* maximum error (us) (ro) */
long int esterror; /* estimated error (us) (ro) */
long int tai; /* TAI offset (ro) */
long int __glibc_reserved1;
long int __glibc_reserved2;
long int __glibc_reserved3;
long int __glibc_reserved4;
};
你在 your code 中没有的:
public class NTPTimeVal extends Structure {
public TimeVal time; /* Current time */
public Long maxerror; /* Maximum error */
public Long esterror;
public int tai;
}
如果这些保留字段恰好在您的 glibc
版本中使用,则可以解释堆损坏。
我也会仔细检查你取回的数据。如果某些字段包含奇怪的值,则可能意味着字段 size/alignment 有问题,这也可能表明结构比需要的要短。
我们有一个非常简单的要求,即从 Java 调用几个本机函数。我们正在使用 JNA 进行这些本机调用。
编辑:我们没有任何自定义本机代码。我们正在调用 Linux 内核 C 库函数。
我们收到非常奇怪的内存损坏错误,例如
- `/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/bin/java' 错误:malloc():内存损坏:0x00007f9b7849fc40
- `/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/bin/java' 中的错误:损坏的大小与 prev_size:0x00007f253c4470f0
- SIGSEGV (0xb)
程序有时甚至会挂起。这些错误是间歇性的。
一些关于在 JNA 调用中使用结构的标准 examples/docs 会有所帮助。
这是我们的具有本机功能的库包装器:
这些是映射到 C:
结构的原生模型这就是我们访问库函数的方式:
val timeVal = new NTPTimeVal()
TimeLibrary.ntp_gettimex(timeVal)
println(timeVal.tai)
您可以参考 TimeServiceImpl.scala 更清楚。
有人能告诉我们到底哪里做错了吗?
ntptimeval
和相关结构中有一些保留字段:
struct ntptimeval
{
struct timeval time; /* current time (ro) */
long int maxerror; /* maximum error (us) (ro) */
long int esterror; /* estimated error (us) (ro) */
long int tai; /* TAI offset (ro) */
long int __glibc_reserved1;
long int __glibc_reserved2;
long int __glibc_reserved3;
long int __glibc_reserved4;
};
你在 your code 中没有的:
public class NTPTimeVal extends Structure {
public TimeVal time; /* Current time */
public Long maxerror; /* Maximum error */
public Long esterror;
public int tai;
}
如果这些保留字段恰好在您的 glibc
版本中使用,则可以解释堆损坏。
我也会仔细检查你取回的数据。如果某些字段包含奇怪的值,则可能意味着字段 size/alignment 有问题,这也可能表明结构比需要的要短。