提问者:小点点

在Linux上调用JNI代码时出现奇怪的崩溃;我是否正确编译了共享库?


这似乎只发生在使用Java 1.7时,无论是Oracle还是OpenJDK JRE。使用Java 1.6(Oracle和OpenJDK)时,一切都可以找到。

不久前,我尝试在Linux下编译一个JNI共享库(用于Java电影播放器的LibVLC包装器)。然而,当调用某些本机函数时,JVM在零星和任意时刻崩溃,错误日志将libc.so中的函数_IO_file_underflow作为有问题的框架。

最终,我发现我在编译时只是缺少了一些GCC标志,而这些问题可能是由某些优化或JVM没有预料到的原因引起的。在遵循本指南和这个问题的答案后,我在编译命令中添加了一些标志,如-fno严格别名-phread,一切都很顺利。

至少,这在OpenSuSe 12.1和OpenJDK 1.6下有效。不幸的是,在升级到OpenSuse 12.2和OpenJDK 1.7后,应用程序再次开始崩溃,并出现以下熟悉的错误消息:

#
[thread -1984603328 also had an error]# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0xb770ff38, pid=19354, tid=2309835584
#
# JRE version: 7.0_09-b30
# Java VM: OpenJDK Client VM (23.2-b09 mixed mode linux-x86 )
# Problematic frame:
# C  [libc.so.6+0x127f38]  _IO_file_underflow+0x68
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /usr/fids/build_vlcplayer/hs_err_pid19354.log

#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
#   http://icedtea.classpath.org/bugzilla
#

我使用以下命令进行编译(许多标志可能是多余的):

gcc -I/usr/lib/jvm/java-1.7.0-openjdk-1.7.0/include/ -I/usr/lib/jvm/java-1.7.0-openjdk-1.7.0/include/linux/ -fno-strict-aliasing -fno-omit-frame-pointer -fPIC -pthread -static-libgcc -D_REENTRANT -Di586 -DARCH='"I586"' -DLINUX -D_LARGEFILE64_SOURCE -D_GNU_SOURCE -D_LITTLE_ENDIAN  -Wall -Wno-unused-parameter -DDEBUG_MODE -shared -lvlc -lc -lX11 movieplayer.c -Wl,-soname,movieplayer.so -o movieplayer.so

我在GCC 3.3版和4.7版上都试过。

由于相同的代码在Windows(使用Oracle JDK 1.6或1.7)或OpenSuSe 12.1(使用OpenJDK 1.6)下运行良好,我认为movieplayer. c的实际源代码不相关;尽管如此,以下是发生崩溃的部分:

/** The LibVLC instance.*/
static libvlc_instance_t * vlcInstance = NULL;

/** The media object representing the video. */
static libvlc_media_t * media = NULL;

/** The media player. */
static libvlc_media_player_t * mediaPlayer = NULL;

...

const char * fname = str + 1;

DEBUGMSG("Opening file: '%s'\n", fname);

// Create a VLC media object for this file.
media = libvlc_media_new_path (vlcInstance, fname);

if(media)
{
    // Parse the media. This allows fetching duration and track info.
    DEBUGMSG("Parsing media.");
    libvlc_media_parse(media);  // <-- Crash occurs at this point.

    // Create the media player.
    DEBUGMSG("Creating a media player.");
    mediaPlayer = libvlc_media_player_new_from_media(media);

    ...
}

...

当我修改这段代码时,在调用其他LibVLC函数时,或者在调用POSIX sleep函数时,同样的崩溃会在不同的时刻发生。

基本上,我的问题是:我认为这些崩溃是因为我没有正确编译应用程序而导致的,这正确吗?如果是这样,我做错了什么?


共1个答案

匿名用户

这里有一个关于同一个问题的讨论:https://github.com/caprica/vlcj/issues/62的变通办法:禁用VLC的LUA。不知何故,Ubuntu的包装打破了它。还有更多讨论它的参考文献。另请参见https://bugs.launchpad.net/ubuntu/源代码/lua5.1/ bug/1136432。