JNA Native.loadLibrary генерирует ошибку памяти: __memmove_avx_unaligned_erms - PullRequest
0 голосов
/ 27 июня 2018

Я разрабатываю приложение на Java, которое загружает разделяемую библиотеку C ++ с использованием JNA. Подробная процедура следующая.

  1. Найти библиотеку в банке
  2. использовать System.load( C_LIBRARY_PATH ) с результатом шага 1
  3. Загрузка библиотеки с использованием инструментов JNA: Wrapper INSTANCE = Native.loadLibrary( C_LIBRARY_PATH, Wrapper.class );

Эта процедура используется для создания оболочки библиотеки c ++ и, следовательно, создает библиотеку java, которая используется для других проектов. Пункты 1 и 2 вдохновлены работой adamheinrich . Наличие файла проверяется перед вызовом функции load.

При использовании java-оболочки в проекте у меня случайно появляется ошибка, как показано ниже. Не могли бы вы помочь мне в отладке и попытаться контролировать этот случайный сбой?

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007ff754e1cf9b, pid=8922, tid=9159
#
# JRE version: OpenJDK Runtime Environment (10.0.1+10) (build 10.0.1+10)
# Java VM: OpenJDK 64-Bit Server VM (10.0.1+10, mixed mode, tiered, compressed oops, g1 gc, linux-amd64)
# Problematic frame:
# C  [libc.so.6+0x15cf9b]  __memmove_avx_unaligned_erms+0x8b
#
# Core dump will be written. Default location: Core dumps may be processed with "/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h %e" (or dumping to /home/core.8922)
#
# 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.
#
---------------  S U M M A R Y ------------

Command Line: -Drebel.base=/home/.jrebel -Drebel.env.ide.plugin.version=2018.1.2 -Drebel.env.ide.version=2018.1 -Drebel.env.ide.product=IU -Drebel.env.ide=intellij -Drebel.notification.url=http://localhost:17434 -agentpath:/home/.IntelliJIdea2018.1/config/plugins/jr-ide-idea/lib/jrebel6/lib/libjrebel64.so -Dvisualvm.id=1716948864493 -Dmaven.multiModuleProjectDirectory=/home/ -Dmaven.home=/home/Documents/ideaIU-2018.1/idea-IU-181.4203.550/plugins/maven/lib/maven3 -Dclassworlds.conf=/home/Documents/ideaIU-2018.1/idea-IU-181.4203.550/plugins/maven/lib/maven3/bin/m2.conf -javaagent:/home/ideaIU-2018.1/idea-IU-181.4203.550/lib/idea_rt.jar=36953:/home/ideaIU-2018.1/idea-IU-181.4203.550/bin -Dfile.encoding=UTF-8 org.codehaus.classworlds.Launcher -Didea.version=2018.1 -P tomcat clean tomcat7:run -f pom.xml

Host: Intel(R) Core(TM) i7-6560U CPU @ 2.20GHz, 4 cores, 15G, Fedora release 28 (Twenty Eight)
Time: Wed Jun 27 08:35:16 2018 CEST elapsed time: 140 seconds (0d 0h 2m 20s)

---------------  T H R E A D  ---------------

Current thread (0x00007ff74efc1000):  JavaThread "http-bio-8080-exec-7" daemon [_thread_in_native, id=9159, stack(0x00007ff694588000,0x00007ff694689000)]

Stack: [0x00007ff694588000,0x00007ff694689000],  sp=0x00007ff694683ae8,  free space=1006k
Native frames: (J=compiled Java code, A=aot compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [libc.so.6+0x15cf9b]  __memmove_avx_unaligned_erms+0x8b

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  java.lang.ClassLoader$NativeLibrary.load0(Ljava/lang/String;Z)Z+0 java.base@10.0.1
j  java.lang.ClassLoader$NativeLibrary.load()Z+63 java.base@10.0.1
j  java.lang.ClassLoader$NativeLibrary.loadLibrary(Ljava/lang/Class;Ljava/lang/String;Z)Z+239 java.base@10.0.1
j  java.lang.ClassLoader.loadLibrary0(Ljava/lang/Class;Ljava/io/File;)Z+46 java.base@10.0.1
j  java.lang.ClassLoader.loadLibrary(Ljava/lang/Class;Ljava/lang/String;Z)V+50 java.base@10.0.1
j  java.lang.Runtime.load0(Ljava/lang/Class;Ljava/lang/String;)V+77 java.base@10.0.1
j  java.lang.System.load(Ljava/lang/String;)V+7 java.base@10.0.1
j  *hiddenPackageName*.NativeUtils.loadLibraryFromJar(Ljava/lang/String;Ljava/lang/Class;)V+359
j  *hiddenPackageName*.LibraryInitializer.<clinit>()V+13
v  ~StubRoutines::call_stub

Чтобы дать полный обзор вопроса, я копирую код пунктов 1 и 2 следующей процедуры:

public static <T> void loadLibraryFromJar(String path, Class<T> clazz) throws IOException {
    if( !path.startsWith( "/" ) ){
        throw new IllegalArgumentException( "The path has to be absolute (start with '/')." );
    }

    // Obtain filename from path
    String[] parts = path.split( "/" );
    String filename = (parts.length > 1) ? parts[parts.length - 1] : null;

    // Split filename to prefix and suffix (extension)
    String prefix = "";
    String suffix = null;
    if( filename != null ){
        parts = filename.split( "\\.", 2 );
        prefix = parts[0];
        suffix = (parts.length > 1) ? "." + parts[parts.length - 1] : null; // Thanks, davs! :-)
    }

    // Check if the filename is okay
    if( filename == null || prefix.length() < 3 ){
        throw new IllegalArgumentException( "The filename has to be at least 3 characters long." );
    }

    // Prepare temporary file
    File temp = File.createTempFile( prefix, suffix );
    temp.deleteOnExit();

    if( !temp.exists() ){
        throw new FileNotFoundException( "File " + temp.getAbsolutePath() + " does not exist." );
    }

    // Prepare buffer for data copying
    byte[] buffer = new byte[1024];
    int readBytes;

    // Open and check input stream
    try( InputStream is = clazz.getResourceAsStream( path ) ){
        if( is == null ){
            throw new FileNotFoundException( "File " + path + " was not found inside JAR." );
        }

        // Open output stream and copy data between source file in JAR and the temporary file
        try( OutputStream os = new FileOutputStream( temp ) ){
            while( (readBytes = is.read( buffer )) != -1 ){
                os.write( buffer, 0, readBytes );
            }
        }
    }

    // Finally, load the library
    System.load( temp.getAbsolutePath() );
}

Edit: Значение temp.getAbsolutePath() впоследствии равно:

/tmp/libgcc_s7394566251608466486.so.1

/tmp/libgomp14105897271936182307.so.1

/tmp/libpthread18366551914953688272.so.0

/tmp/libstdc++3062153806840733749.so.6

/tmp/libfitting1621020429668741777.so.1.0

1 Ответ

0 голосов
/ 28 июня 2018

Я случайно загружал дважды одну и ту же библиотеку в нескольких классах Java. Точнее, вызов Wrapper INSTANCE = Native.loadLibrary( C_LIBRARY_PATH, Wrapper.class ); был сделан во вложенном интерфейсе, который я реплицировал во все различные объекты, которые использовали одну и ту же библиотеку c ++. Это, как напоминает @cubrr, делается статически при создании первого объекта. Следовательно, библиотека была загружена несколько раз. Ниже спудокод, иллюстрирующий мою ошибку.

class A {
    protected interface Wrapper extends Library {
        Wrapper INSTANCE = Native.loadLibrary( C_LIBRARY_PATH, Wrapper.class ); 
        double functionA();
    }
    public double callA() {
        return Wrapper.INSTANCE.functionA();
    }
}

class B {
    protected interface Wrapper extends Library {
        Wrapper INSTANCE = Native.loadLibrary( C_LIBRARY_PATH, Wrapper.class ); 
        double functionB();
    }
    public double callB() {
        return Wrapper.INSTANCE.functionB();
    }
}

что должно быть сделано так

class Parent {
    protected interface Wrapper extends Library {
        Wrapper INSTANCE = Native.loadLibrary( C_LIBRARY_PATH, Wrapper.class ); 
        double functionA();
        double functionB();
    }
}

class A extends Parent {
    public double callA() {
        return Wrapper.INSTANCE.functionA();
    }
}

class B extends Parent {
    public double callB() {
        return Wrapper.INSTANCE.functionB();
    }
}
...