theLink 10.0
Loading...
Searching...
No Matches
MqCall_jv.c
Go to the documentation of this file.
1
9/* LABEL-NO */
10
11#include <jni.h>
12#include "LibMqMsgque_private_jv.h"
13
14void MK_DECL
15NS(ServiceCopy) ( MQ_SERVICE_COPY_ARGS )
16{
17 struct MkProcCallS * old = (struct MkProcCallS *) *dataP;
18 JavaErrorCheckNULL(*dataP = MK(ProcCreate) (MK_RT_CALL old->env, old->object, old->class, old->method, old->hasArg));
19 return;
20error:
21 MkPanicC_4M(mqctx, __func__, -1, "unable to copy proc data");
22}
23
24void MK_DECL
25NS(ServiceFree) ( MQ_SERVICE_FREE_ARGS)
26{
27 MK(ProcFree) ((struct MkProcCallS **) dataP);
28}
29
31NS(ServiceCall) ( MQ_SERVICE_CALL_ARGS )
32{
34
35 struct MkProcCallS const * const call = (struct MkProcCallS const * const) __data__;
36 JNIEnv *env = call->env;
37
38 // clean JAVA and libmqmsgque error
39 //(*env)->ExceptionClear(env);
40 //MkErrorReset(mqctx);
41
42//printXV(mqctx "%s: self=%p, class=%p, object=%p, method=%p", MqServiceTokenGet(mqctx), self, call->class, call->object, call->method);
43 if (call->hasArg == false) {
44
45 // call the function
46 if (call->class == NULL) {
47 // method call of the same class
48 (*env)->CallVoidMethod(env, call->object, call->method);
49 } else if (call->object == NULL) {
50 // static method call returning an object
51 (*env)->CallStaticObjectMethod(env, call->class, call->method);
52 } else {
53 // method call of an other class
54 (*env)->CallNonvirtualVoidMethod(env, call->object, call->class, call->method);
55 }
56
57 } else {
58
59 jobject self = MqContextC_ObjNewOrNull(env,mqctx);
60
61 // call the function
62 if (call->class == NULL) {
63 // method call of the same class
64 //(*env)->CallVoidMethod(env, call->object, call->method, call->data);
65 (*env)->CallVoidMethod(env, call->object, call->method, self);
66 } else if (call->object == NULL) {
67 // static method call returning an object
68 (*env)->CallStaticObjectMethod(env, call->class, call->method, self);
69 } else {
70 // method call of an other class
71 (*env)->CallNonvirtualVoidMethod(env, call->object, call->class, call->method, self);
72 }
73 }
74
75 // check on error
76 if((*env)->ExceptionCheck(env) != JNI_FALSE) {
77 // is the ERROR from "java" or "jvmkkernel"
78 OT_ERROR_LNG_2_META(mqctx);
79 }
80 return MkErrorStack_0E_Check();
81}
82
83/*
84void printObj (JNIEnv *env, jobject obj) {
85 (*env)->CallStaticObjectMethod(env, NS(Class_MkObjectC), NS(MID_MkObjectC_printObj), obj);
86 JavaErrorReport
87}
88*/
89
90// ************************************************************************************
91
92/*
93#
94# A fatal error has been detected by the Java Runtime Environment:
95#
96# SIGSEGV (0xb) at pc=0x00000000063f681d, pid=47793, tid=0x000000002ebba700
97#
98# JRE version: OpenJDK Runtime Environment (8.0_191-b12) (build 1.8.0_191-b12)
99# Java VM: OpenJDK 64-Bit Server VM (25.191-b12 interpreted mode linux-amd64 compressed oops)
100# Derivative: IcedTea 3.10.0
101# Distribution: Custom build (Thu Jan 10 19:41:37 UTC 2019)
102# Problematic frame:
103# V [libjvm.so+0x5d581d] jni_ReleaseByteArrayElements+0x6d
104#
105# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
106#
107# An error report file with more information is saved as:
108# .../NHI1/theLink/tests/hs_err_pid47793.log
109
110
111The code described in your link makes the invalid assumption that Java will never move the underlying byte array.
112Astonishing that it ever worked, if indeed it ever did, and even more astonishing that it got published under the IBM banner.
113– user207421 Jan 27 '17 at 21:10
114
115*/
116
117/*
118struct Jalloc{
119 int sig;
120 jbyteArray jba;
121 jobject ref;
122 size_t siz;
123};
124
125static MK_PTR jv_malloc(size_t t)
126{
127 if (MK(cached_jvm) != NULL) {
128 JNIEnv* env = NULL;
129 jboolean isCopy = false;
130 MkErrorCheck(MK(GetJniEnv)(&env));
131
132 jbyteArray jba=(*env)->NewByteArray(env, (int)t+sizeof(struct Jalloc));
133 if ((*env)->ExceptionOccurred(env)) goto error;
134
135 void* jbuffer = (void*) (*env)->GetByteArrayElements(env,jba,&isCopy);
136 if ((*env)->ExceptionOccurred(env)) goto error;
137
138//printV("env=%p, buffer=%p, isCopy=%s\n", env, jbuffer, isCopy?"true":"false")
139
140 struct Jalloc *pJalloc = (struct Jalloc*) jbuffer;
141 pJalloc->sig = 0x39F6F1A8;
142 pJalloc->jba = jba;
143 pJalloc->ref = (*env)->NewGlobalRef(env, jba);
144 pJalloc->siz = t;
145 if((*env)->ExceptionOccurred(env)) goto error;
146
147 return (MK_PTR) (((char*)jbuffer)+sizeof(struct Jalloc));
148 } else {
149 struct Jalloc* pJalloc= (struct Jalloc*) malloc((int)t+sizeof(struct Jalloc));
150 if (!pJalloc) goto error;
151 pJalloc->sig = 0x39F6F1A8;
152 pJalloc->ref = 0;
153 pJalloc->siz = t;
154 return (MK_PTR) (((char*)((void*)pJalloc))+sizeof(struct Jalloc));
155 }
156error:
157 return NULL;
158}
159
160static void jv_free(void* v)
161{
162 if (v != NULL) {
163 void* buffer = (void*) (((char*)v)-sizeof(struct Jalloc));
164 struct Jalloc* pJalloc = (struct Jalloc*) buffer;
165 if(pJalloc->sig == 0x39F6F1A8 && pJalloc->ref) {
166 JNIEnv* env = NULL;
167 pJalloc->sig = 0x00000000;
168 MkErrorCheck(MK(GetJniEnv)(&env));
169
170//printV("env=%p, buffer=%p\n", env, buffer)
171
172 (*env)->DeleteGlobalRef(env, pJalloc->ref);
173 if ((*env)->ExceptionCheck(env)) {
174 (*env)->ExceptionDescribe(env);
175 }
176 (*env)->ReleaseByteArrayElements(env, pJalloc->jba, (jbyte*)buffer, JNI_ABORT);
177 } else{
178 free(buffer);
179 }
180 }
181error:
182 return;
183}
184
185static MK_PTR jv_calloc (size_t nelem, size_t elsize)
186{
187 register MK_PTR ptr;
188
189 if (nelem == 0 || elsize == 0) nelem = elsize = 1;
190
191 ptr = jv_malloc (nelem * elsize);
192 if (ptr) memset(ptr,'\0', nelem * elsize);
193
194 return ptr;
195}
196
197static MK_PTR jv_realloc (MK_PTR oldPtr, size_t newsize)
198{
199 if (oldPtr == NULL) return jv_malloc (newsize);
200
201 void* buffer = (void*) (((char*)oldPtr)-sizeof(struct Jalloc));
202 struct Jalloc* pJalloc = (struct Jalloc*) buffer;
203
204 if (newsize < pJalloc->siz) return oldPtr;
205
206 if(pJalloc->ref) {
207 MK_PTR newPtr = jv_malloc (newsize);
208 if (newPtr == NULL) goto error;
209 bcopy(oldPtr, newPtr, pJalloc->siz);
210 jv_free(oldPtr);
211 return newPtr;
212 } else{
213 return realloc(oldPtr,newsize);
214 }
215error:
216 return NULL;
217}
218
219static MK_STR jv_strdup (MK_STRN s)
220{
221 MK_STR result = (MK_STR) jv_malloc (strlen(s) + 1);
222 if (result == (MK_STR)0)
223 return (MK_STR)0;
224 strcpy(result, s);
225 return result;
226}
227
228static MK_STR jv_strndup (MK_STRN s, MK_SIZE l)
229{
230 const MK_SIZE len = l+1;
231 MK_STR result = (MK_STR) jv_malloc (len);
232 if (result == (MK_STR)0)
233 return (MK_STR)0;
234 strncpy(result, s, len);
235 return result;
236}
237*/
238
239//void JavaSetup (void) {
240/*
241
242 → total shit → https://www.usenix.org/legacy/event/usenix07/posters/sorensen.pdf
243
244 MkLal.SysCalloc = jv_calloc;
245 MkLal.SysMalloc = jv_malloc;
246 MkLal.SysRealloc = jv_realloc;
247 MkLal.SysFree = jv_free;
248 MkLal.SysStrDup = jv_strdup;
249 MkLal.SysStrNDup = jv_strndup;
250*/
251//}
#define mqctx
#define MkErrorStack_0E_Check()
#define MkPanicC_4M(errobj, callfunc, errnum, message)
#define MK_DECL
MkErrorE
#define MK_RT_CALL
#define MQ_SERVICE_CALL_CHECK
#define MQ_SERVICE_FREE_ARGS
the MqDataFreeF arguments with default names
#define MQ_SERVICE_CALL_ARGS
the MqTokenF arguments with default names
#define MQ_SERVICE_COPY_ARGS
the MqDataFreeF arguments with default names