astonbitecode / j4rs

Java for Rust
Apache License 2.0
643 stars 36 forks source link

The program crashes after running for a long time(SIGSEGV) #122

Open lngex opened 4 months ago

lngex commented 4 months ago

Current thread (0x000055e5f95f7ae0):  JavaThread "main" [_thread_in_native, id=11797, stack(0x00007ffc63e29000,0x00007ffc63f29000)]

Stack: [0x00007ffc63e29000,0x00007ffc63f29000],  sp=0x00007ffc63f22100,  free space=996k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)

[error occurred during error reporting (printing native stack), id 0xb, SIGSEGV (0xb) at pc=0x00007fb2f055f64a]

siginfo: si_signo: 11 (SIGSEGV), si_code: 1 (SEGV_MAPERR), si_addr: 0x0000000000000000

Register to memory mapping:

RAX=0x0 is NULL
RBX=0x00007fb2ef1ad018 points into unknown readable memory: 0x755c223a2230227b | 7b 22 30 22 3a 22 5c 75
RCX=0x00007fb2ef015020 points into unknown readable memory: 0x00007fb2ef0a4058 | 58 40 0a ef b2 7f 00 00
RDX=0x000055e5f94cefc0 points into unknown readable memory: 0x000055e5f9496590 | 90 65 49 f9 e5 55 00 00
RSP=0x00007ffc63f220f8 is pointing into the stack for thread: 0x000055e5f95f7ae0
RBP=0x00007ffc63f222b0 is pointing into the stack for thread: 0x000055e5f95f7ae0
RSI=0x000055e5fbc7b5c0 points into unknown readable memory: 0x000055e5fbc7b3c0 | c0 b3 c7 fb e5 55 00 00
RDI=0x00007fb2ef1ad018 points into unknown readable memory: 0x755c223a2230227b | 7b 22 30 22 3a 22 5c 75
R8 =0x0 is NULL
R9 =0x4025f2b7cffb2000 is an unknown value
R10=0x00007ffc63f22200 is pointing into the stack for thread: 0x000055e5f95f7ae0
R11=
[error occurred during error reporting (printing register info), id 0xb, SIGSEGV (0xb) at pc=0x00007fb2f055f64a]

Registers:
RAX=0x0000000000000000, RBX=0x00007fb2ef1ad018, RCX=0x00007fb2ef015020, RDX=0x000055e5f94cefc0
RSP=0x00007ffc63f220f8, RBP=0x00007ffc63f222b0, RSI=0x000055e5fbc7b5c0, RDI=0x00007fb2ef1ad018
R8 =0x0000000000000000, R9 =0x4025f2b7cffb2000, R10=0x00007ffc63f22200, R11=0x00007fb29c58486a
R12=0x0000000000000008, R13=0x00007ffc63f222b0, R14=0x00007ffc63f223f0, R15=0x00007ffc63f223d0
RIP=0x0000000000000000, EFLAGS=0x0000000000010202, CSGSFS=0x0000000000000033, ERR=0x0000000000000014
  TRAPNO=0x000000000000000e

Top of Stack: (sp=0x00007ffc63f220f8)
0x00007ffc63f220f8:   00007fb29c5820f7 0000000000000000
0x00007ffc63f22108:   00007fb29c584880 00007fb2ef000040
0x00007ffc63f22118:   000000000a258778 00007fb2e4b17320
0x00007ffc63f22128:   00007fb2e4b171a0 00007ffc63f22490 

Instructions: (pc=0x00007fb29c5820f7)
0x00007fb29c581ff7:   48 8d 3d 7a d0 27 00 55 48 29 f8 48 89 e5 48 83
0x00007fb29c582007:   f8 0e 77 02 5d c3 48 8b 05 24 cd 27 00 48 85 c0
0x00007fb29c582017:   74 f2 5d ff e0 0f 1f 40 00 48 8d 05 51 d0 27 00
0x00007fb29c582027:   48 8d 3d 4a d0 27 00 55 48 29 f8 48 89 e5 48 c1
0x00007fb29c582037:   f8 03 48 89 c2 48 c1 ea 3f 48 01 d0 48 d1 f8 75
0x00007fb29c582047:   02 5d c3 48 8b 15 57 cf 27 00 48 85 d2 74 f2 5d
0x00007fb29c582057:   48 89 c6 ff e2 0f 1f 40 00 80 3d 11 d0 27 00 00
0x00007fb29c582067:   75 27 48 83 3d 5f cf 27 00 00 55 48 89 e5 74 0c
0x00007fb29c582077:   48 8d 3d da 83 27 00 e8 dd d4 ff ff e8 68 ff ff
0x00007fb29c582087:   ff 5d c6 05 e8 cf 27 00 01 f3 c3 0f 1f 40 00 66
0x00007fb29c582097:   2e 0f 1f 84 00 00 00 00 00 48 83 3d a8 83 27 00
0x00007fb29c5820a7:   00 74 26 48 8b 05 b7 ce 27 00 48 85 c0 74 1a 55
0x00007fb29c5820b7:   48 8d 3d 92 83 27 00 48 89 e5 ff d0 5d e9 57 ff
0x00007fb29c5820c7:   ff ff 0f 1f 80 00 00 00 00 e9 4b ff ff ff 48 8b
0x00007fb29c5820d7:   7e 20 48 8b 46 28 48 8b 40 18 48 8d 35 60 76 05
0x00007fb29c5820e7:   00 6a 08 5a ff e0 53 48 89 fb ff 15 b9 cc 27 00
0x00007fb29c5820f7:   48 8d 50 01 48 89 d8 5b c3 8b 46 34 a8 10 75 10
0x00007fb29c582107:   a8 20 0f 84 a1 49 00 00 48 8b 3f e9 59 8d 00 00
0x00007fb29c582117:   48 8b 3f e9 f1 69 00 00 83 3f 04 77 10 48 8b 47
0x00007fb29c582127:   08 48 8b 77 10 48 89 c7 e9 49 94 00 00 c3 41 57
0x00007fb29c582137:   41 56 41 54 53 50 48 89 fb 48 8b 07 48 83 f8 01
0x00007fb29c582147:   74 12 48 85 c0 75 52 48 83 7b 10 00 74 4b 48 8b
0x00007fb29c582157:   7b 08 eb 3f 48 8b 43 08 89 c1 83 e1 03 48 8d 51
0x00007fb29c582167:   fe 48 83 fa 02 72 32 48 85 c9 74 2d 4c 8d 70 ff
0x00007fb29c582177:   4c 8b 78 ff 4c 8b 60 07 4c 89 ff 41 ff 14 24 49
0x00007fb29c582187:   83 7c 24 08 00 74 09 4c 89 ff ff 15 71 cb 27 00
0x00007fb29c582197:   4c 89 f7 ff 15 68 cb 27 00 48 89 df 48 83 c4 08
0x00007fb29c5821a7:   5b 41 5c 41 5e 41 5f ff 25 54 cb 27 00 53 48 89
0x00007fb29c5821b7:   fb 0f b6 07 48 85 c0 74 18 83 f8 01 75 13 48 8b
0x00007fb29c5821c7:   7b 08 48 8b 73 10 e8 ab 93 00 00 48 83 c3 20 eb
0x00007fb29c5821d7:   0d 48 8d 7b 08 e8 64 f0 00 00 48 83 c3 30 48 8b
0x00007fb29c5821e7:   3b 48 8b 73 08 5b e9 8b 93 00 00 48 85 ff 0f 85 

Stack slot to memory mapping:
stack at sp + 0 slots: 0x0 is NULL
stack at sp + 1 slots: 
[error occurred during error reporting (inspecting top of stack), id 0xb, SIGSEGV (0xb) at pc=0x00007fb2f055f64a]
astonbitecode commented 4 months ago

Can you please give some more details, or even better, some code to reproduce this?

I personally have applications running for days and did not see any issue.

lngex commented 4 months ago

The program mainly calls "segmenter json" for word segmentation,after the word segmentation is completed, call "free memory" to release the memory.The program crash occurs when calling "segmenter json"


pub extern "C" fn segmenter_json(text: *const c_char) -> *mut c_char {
{
check_init();
}
let c_str = unsafe { CStr::from_ptr(text) };
let text = c_str.to_str().unwrap();
let json_str: String;
let jvm = Jvm::attach_thread().unwrap();
// 创建分词对象
let result = jvm.invoke_static("com.farseer.utils.IKUtil",
"segmenterJson",
&[InvocationArg::try_from(text).unwrap()])
.unwrap();
// 获取分词结果
json_str = jvm.to_rust(result).unwrap();
let c_string = CString::new(json_str);
let x = c_string.unwrap().into_raw();
x
}

[no_mangle]

pub extern "C" fn free_memory(text: *mut cchar) { unsafe { let = CString::from_raw(text); } }


Also attached is the jvm complete log and code.
Sorry, the system-generated stack snapshot was deleted and cannot be provided here.
[code link](http://124.221.251.108:9080/code)
[log link](http://124.221.251.108:9080/log)
lngex commented 4 months ago

The calling programs are written in php and they create a lot of jvm's. I'm not sure if it's for this reason

astonbitecode commented 4 months ago

Creating many Jvms should not be a problem.

My guess is that you fall into some race condition while manipulating the *const c_char input. Could it maybe freed while generating the InvocationArg? InvocationArg generation for &str internally implies the creation of a CString using cesu8, so, I would propose to make sure for such issues.

In order to verify if the error happens indeed during the InvocationArg generation, you could create it before calling the Jvm:

let ia = InvocationArg::try_from(text).unwrap();
let result = jvm.invoke_static("com.farseer.utils.IKUtil",
                                   "segmenterJson",
                                   &[ia])
        .unwrap();
lngex commented 4 months ago

Creating many Jvms should not be a problem.

My guess is that you fall into some race condition while manipulating the *const c_char input. Could it maybe freed while generating the InvocationArg? InvocationArg generation for &str internally implies the creation of a CString using cesu8, so, I would propose to make sure for such issues.

In order to verify if the error happens indeed during the InvocationArg generation, you could create it before calling the Jvm:

let ia = InvocationArg::try_from(text).unwrap();
let result = jvm.invoke_static("com.farseer.utils.IKUtil",
                                   "segmenterJson",
                                   &[ia])
        .unwrap();

I'll give it a try.

astonbitecode commented 2 months ago

@lngex, do you have anything new to share regarding this?