java - 为什么JVM在进入无限递归时不会崩溃?
问题描述
我正在编写一个要加载到 JVM 中的共享库,而下面的行为让我陷入了困境。这是我的Java类:
package com.test;
public class UnixUtil {
static {
System.loadLibrary("myfancylibrary");
}
static native int openReadOnlyFd(String path);
static native int closeFd(int fd);
}
public class Main {
public static void main(String[] args){
int fd = UnixUtil.openReadOnlyFd("/tmp/testc");
UnixUtil.closeFd(fd);
}
}
要加载的库如下所示:
test_jni.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_test_UnixUtil */
#ifndef _Included_com_test_UnixUtil
#define _Included_com_test_UnixUtil
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_test_UnixUtil
* Method: openReadOnlyFd
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_test_UnixUtil_openReadOnlyFd
(JNIEnv *, jclass, jstring);
/*
* Class: com_test_UnixUtil
* Method: closeFd
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_test_UnixUtil_closeFd
(JNIEnv *, jclass, jint);
#ifdef __cplusplus
}
#endif
#endif
test_jni.c
#include "test_jni.h"
#include "fs.h"
JNIEXPORT jint JNICALL Java_com_test_UnixUtil_openReadOnlyFd
(JNIEnv *e, jclass jc, jstring path){
const char *const native_path = ((*e) -> GetStringUTFChars)(e, path, NULL);
int fd = read_only_open(native_path);
((*e) -> ReleaseStringUTFChars)(e, path, native_path);
return fd;
}
JNIEXPORT jint JNICALL Java_com_test_UnixUtil_closeFd
(JNIEnv *e, jclass jc, jint fd){
printf("Closing files descriptord %d... \n", fd);
return close(fd);
}
fs.h
#ifndef FS_H
#define FS_H
int read_only_open(const char *path);
int close(int fd);
#endif //FS_H
fs.c
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sys/fcntl.h>
#include "fs.h"
int read_only_open(const char *path){
printf("Entering %s.%s:%d\n", __FILE__, __func__, __LINE__);
int fd = open(path, O_RDONLY);
return fd;
}
int close(int fd){ //Java_com_test_UnixUtil_closeFd does not invoke this function
printf("Entering %s.%s:%d\n", __FILE__, __func__, __LINE__);
int close_result = close(fd);
return close_result;
}
编译和运行此类时Main
,JVM不会崩溃。它根本不进入函数fs.h::close(int)
。相反, 在 GDB 中可以看到stdlib
's被调用:close
Thread 2 "java" hit Breakpoint 1, Java_com_test_UnixUtil_closeFd (e=<optimized out>,
jc=<optimized out>, fd=4) at /home/rjlomov/test_jni/src/main/java/com/test/lib/test_jni.c:17
17 return close(fd);
(gdb) step
18 }
(gdb)
17 return close(fd);
(gdb)
__close (fd=4) at ../sysdeps/unix/sysv/linux/close.c:27
27 ../sysdeps/unix/sysv/linux/close.c: No such file or directory.
(gdb)
26 in ../sysdeps/unix/sysv/linux/close.c
运行objdump -dS libmyfancylibrary.so
表明
JNIEXPORT jint JNICALL Java_com_test_UnixUtil_closeFd
(JNIEnv *e, jclass jc, jint fd){
7d0: 53 push %rbx
}
//...
return close(fd);
7e9: e9 62 fe ff ff jmpq 650 <close@plt> // <--- PLT section,
// resolved by linker to stdlib::close?
7ee: 66 90 xchg %ax,%ax
问题:为什么stdlib::close
调用 inJava_com_test_UnixUtil_closeFd
而不是fs.c::close(int)
?我唯一能想象的是JVM有自己的动态链接器来完成这项工作......
解决方案
由于您正在编译共享库,而函数close()
不是static
,编译器通过过程链接表 (PLT) 进行间接调用。反汇编库时,您可能会看到一条说明
call <close@plt>
加载时myfancylibrary
,进程已经有了close
from libc的实现,所以动态liker更新PLT指向libc的close()
.
推荐阅读
- javascript - 简单的函数只能工作一次(显示/隐藏 div) - vanilla JavaScript
- docker - 为什么 docker 在构建镜像时会创建容器?
- tensorflow - 有效地从 TensorFlow 中的张量中删除零图像
- pointers - 返回由 x86 NASM 中的指针指向的值
- swift - 如何创建符合 ObservableObject 的数组?
- google-chrome - “噢,Snap!显示此网页时出现问题。”
- python - 使用 Python 将事件发送到 Azure 事件中心
- c++ - 如何在 Octave 编码中访问 C++ 库
- java - 将 PostgreSQLIntervalType 与本机 SQL 一起使用时,Hibernate Types 框架抛出“PSQLException: ERROR: cannot cast type bigint to interval”
- .net-core - 如何添加用户以使用 Novell 库进行交换