I'm trying to connect my Java code with a C program (translated using GnuCobol) using JNI. Unfortunately, I can't access the C source code and rewrite in Java, so I've created a wrapper C program to call it.
Help with the following ERROR
ProgramX.cbl:112: Attempt to reference unallocated memory (Signal SIGSEGV)
The error occurs on the second attempt to get some response from the C program.
Log generated using -Xcheck:jni
ReleasePrimitiveArrayCritical: release array failed bounds check, incorrect pointer returned ? array: 0x00007fd10a6514c0 carray: 0x00007fd104a93400
GuardedMemory(0x00007fd10a651330) base_addr=0x00007fd104a933e0 tag=0x00007fd104a93290 user_size=324 user_data=0x00007fd104a93400
Header guard @0x00007fd104a933e0 is OK
Trailer guard @0x00007fd104a93544 is BROKEN
User data appears to be in use
FATAL ERROR in native method: ReleasePrimitiveArrayCritical: failed bounds check
And I need help to figure out the cause of this.
Sources
The java snippet
static {
System.loadLibrary("gnucobollib");
}
public native void callGnuCobol(byte[] request, byte[] response);
private void invokeNativeCall(AbstractData... args) {
// Changed size from 16162 to the correct size 16164
// as suggested in comment by Alex Cohn
byte[] responseBytes = new byte[16164];
String request = "Personal sensitive data";
callGnuCobol(utm1.getBytes(), responseBytes);
String resonse = new String(responseBytes);
}
The gnucobollib
main C source
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "cpy/utm1.h"
#include "cpy/utm2.h"
#include "jni/testJavaJni.h"
static int cobc_runtime_initialized = 0;
static void init_cobc_runtime() {
if (!cobc_runtime_initialized) {
printf("initializing cobc runtime\n");
cob_init(0, NULL);
cobc_runtime_initialized = 1;
}
}
JNIEXPORT void JNICALL Java_com_att_ub_ub2l_cbct545_Cbct545Program_callGnuCobol(JNIEnv *env, jobject thisObj, jbyteArray request, jbyteArray response)
{
init_cobc_runtime();
jbyte* bufferRequest = (*env)->GetByteArrayElements(env, request, NULL);
jbyte* bufferResponse = (*env)->GetByteArrayElements(env, response, NULL);
CALLBANANA((struct utm1*)&bufferRequest[0], (struct utm2*)&bufferResponse[0]);
(*env)->ReleaseByteArrayElements(env, request, bufferRequest, 0);
(*env)->ReleaseByteArrayElements(env, response, bufferResponse, 0);
}
The method CALLBANANA
is from a translated Cobol code using GNUCobol.
Temporary Solution
Handle all signals direct on C wrapper program. Despite of the error message, everything works fine and million of calls to GnuCobol happened without any issue.
Still pending the recompile of the code to latest version of GnuCobol